【问题标题】:JsPDF scale to size?JsPDF缩放到大小?
【发布时间】:2021-01-31 07:50:33
【问题描述】:

我目前有以下问题:

我有一个特定的 DIV 容器,我想从中生成 PDF 文件。 我正在使用来自 ekoopmans (https://www.npmjs.com/package/html2pdf.js/v/0.9.0) 的 HTML2PDF.JS 脚本

唯一的事情是,我希望它看起来像普通的印刷品。

打印视图:https://i.stack.imgur.com/s9ap7.jpg

PDF 导出:https://i.stack.imgur.com/hDeMW.jpg

PDF 导出好像被放大了。

我的代码中是否缺少任何规则?还是有解决方法? 代码:

<script>
    function generatePDF(invoiceprint) {
        //prints only with div id printinvoice.
     var printContents = document.getElementById(invoiceprint).innerHTML;
     var originalContents = document.body.innerHTML;

     document.body.innerHTML = printContents;
      // work but not to be expected.
     var element = document.body.innerHTML;
        var opt = {
        margin:       10,
        filename:     'Factuur <?php echo $factuurnaam ?>.pdf',
        image:        { type: 'jpeg', quality: 1 },
        html2canvas:  { dpi: 300, letterRendering: true},
        jsPDF:        { unit: 'mm', format: 'a4', orientation: 'portrait' },
        pagebreak:    { mode: ['avoid-all', 'css'] }
        };
        // Choose the element and save the PDF for our user.
        html2pdf().from(element).set(opt).save();
     document.body.innerHTML = originalContents;
}
    </script>

<button onclick="generatePDF('invoiceprint')" class="btn btn-warning"> <i class="fas fa-file-pdf"></i> Download factuur (PDF)</button>

<div id="invoiceprint">             
    <div class="invoice-box" style="min-width:750px; min-height: 1000px;">
SOME DATA
    </div>
</div>

非常感谢所有帮助!

【问题讨论】:

    标签: javascript php css pdf


    【解决方案1】:

    已解决问题!

    必须更改 HTML2Canvas 大小,然后 JsPDF 完成其余的工作。

    <script>
        function generatePDF(invoiceprint) {
            //prints only with div id printinvoice.
         var printContents = document.getElementById(invoiceprint).innerHTML;
         var originalContents = document.body.innerHTML;
    
         document.body.innerHTML = printContents;
          // work but not to be expected.
         var element = document.body.innerHTML;
            var opt = {
            margin:       10,
            filename:     'Factuur <?php echo $factuurnaam ?>.pdf',
            image:        { type: 'jpeg', quality: 1 },
            html2canvas:  { dpi: 300, letterRendering: true, width: 1080, height: 1920},
            jsPDF:        { unit: 'mm', format: 'a4', orientation: 'portrait' },
            pagebreak:    { mode: ['avoid-all', 'css'] }
            };
            // Choose the element and save the PDF for our user.
            html2pdf().from(element).set(opt).save();
         document.body.innerHTML = originalContents;
    }
        </script>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-28
      • 2023-03-18
      • 1970-01-01
      • 2014-10-30
      • 1970-01-01
      相关资源
      最近更新 更多