0

我可以使用smtpJS在我的网站上发送邮件。但是现在我想附加一个用jsPDF创建的新生成的 PDF ,我尝试了不同的方法,但它对我不起作用。有人认出我的问题吗?

我已在 functions.php 文件中将此链接添加到 WordPress。

wp_enqueue_script( 'pdfJS-js', get_stylesheet_directory_uri() . 'https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.2/jspdf.min.js', array( 'jquery' ),'',true );

但是在我的浏览器控制台中总是出现以下错误:

ReferenceError: 找不到变量:jsPDF
(var doc = new jsPDF('p', 'mm', "A4"); )

我的代码:

function sendMail() {

/* SmtpJS.com - v3.0.0 */
var Email = { send: function (a) { return new Promise(function (n, e) { a.nocache = Math.floor(1e6 * Math.random() + 1), a.Action = "Send"; var t = JSON.stringify(a); Email.ajaxPost("https://smtpjs.com/v3/smtpjs.aspx?", t, function (e) { n(e) }) }) }, ajaxPost: function (e, n, t) { var a = Email.createCORSRequest("POST", e); a.setRequestHeader("Content-type", "application/x-www-form-urlencoded"), a.onload = function () { var e = a.responseText; null != t && t(e) }, a.send(n) }, ajax: function (e, n) { var t = Email.createCORSRequest("GET", e); t.onload = function () { var e = t.responseText; null != n && n(e) }, t.send() }, createCORSRequest: function (e, n) { var t = new XMLHttpRequest; return "withCredentials" in t ? t.open(e, n, !0) : "undefined" != typeof XDomainRequest ? (t = new XDomainRequest).open(e, n) : t = null, t } };

var receiverMail = document.getElementById('email').value;

var doc = new jsPDF('p', 'mm', "A4");          
var elementHandler = {
    '#ignorePDF': function (element, renderer) {
        return true;
    }
};
var source = window.document.getElementById("A4")[0];
doc.fromHTML(
    source,
    297,
    210,
{
  'width': 210,'elementHandlers': elementHandler
});


var base64 = doc.output("datauristring");


Email.send({
    SecureToken : "C973D7AD-F097-4B95-91F4-40ABC5567812",
    To : receiverMail,
    From : "name@example.com",
    Subject : "Offerte von Name",
    Body : "tst",
    Attachments : [
        {
         name : "offerte.pdf",
         data : base64
   
        }]
}).then(
  message => alert(message)
);

}

4

1 回答 1

0

首先,如果脚本在页面上,我验证它jsPDF作为全局变量加载。这是。所以你的问题是脚本没有加载到页面上。这是一个 PHP 问题,而不是 JS 问题。

问题是您的wp_enqueue_script. 您正在加载远程站点的 URL,因此它已经是一个完整的 URL。您不需要添加get_stylesheet_directory_uri()它。您在此处的代码将在 URL 上查找脚本,https://mywordpresssite.com/wp-content/themes/my-theme/https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.2/jspdf.min.js这显然不是您想要的路径。

wp_enqueue_script( 
   'pdfJS-js',
    get_stylesheet_directory_uri() . 'https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.2/jspdf.min.js', 
    array( 'jquery' ),
    '',
    true
);

你可以删除get_stylesheet_directory_uri() . ,你应该没问题。我还添加了一个版本号作为最佳实践。

wp_enqueue_script( 
   'pdfJS-js', // handle
   'https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.2/jspdf.min.js', // src
    array( 'jquery' ), // dependecies
    '1.3.2', // version
    true // in footer
);
于 2021-04-21T23:26:23.227 回答