【问题标题】:Generate PDF from HTML in Wordpress and get blank PDF在 Wordpress 中从 HTML 生成 PDF 并获得空白 PDF
【发布时间】:2021-06-13 01:43:29
【问题描述】:

我有一个 WordPress 站点,我想用jsPDF 生成一个 PDF。它可以工作,但PDF是空白的。我已经设置了另一个没有 WordPress 的页面,并且它使用相同的代码工作。我用来编码的示例在codepen,但我不是作者。现在我的问题是,jsPDF 不能与 WordPress 一起使用吗?如果还没有,我的错误在哪里?

functions.php

wp_enqueue_script( 'pdf', get_stylesheet_directory_uri() . '/generatePDF.js', array( 'jquery' ),'',true );
wp_enqueue_script( 'jsPDF', 'https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.4.1/jspdf.debug.js', array( 'jquery' ),'',true );
wp_enqueue_script( 'jsPDF-autotable', 'https://cdnjs.cloudflare.com/ajax/libs/jspdf-autotable/2.3.4/jspdf.plugin.autotable.min.js', array( 'jquery' ),'',true );on 04.03.2021

myCustomTemplate.php

<div id='a4' style='height: 297mm; width: 210mm; border: 1px black solid; margin-left:20%;'>
    <p>Content of PDF</p>
</div>

生成PDF.js

函数导出PDF() { var doc = new jsPDF('p', 'mm', 'a4');

var specialElementHandlers = {
    '#getPDF': function(element, renderer){
    return true;
    },
    '.controls': function(element, renderer){
    return true;
    }
};

doc.fromHTML(jQuery('#a4').get(0), 15, 15, {
    'width': 170, 
    'elementHandlers': specialElementHandlers
});

doc.save('offerte.pdf');

}

【问题讨论】:

标签: wordpress jspdf


【解决方案1】:

我觉得你还需要在入队前加上这行代码:

wp_register_script( 'jsPDF', 'https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.3.1/jspdf.umd.min.js');

【讨论】:

    猜你喜欢
    • 2012-09-13
    • 2018-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-04
    • 2020-12-06
    • 1970-01-01
    相关资源
    最近更新 更多