首页 > 解决方案 > 使用 PHPMailer 和 html2pdf 通过电子邮件发送 PDF 数据

问题描述

我试图通过电子邮件发送 PDF 或 PNG,但似乎没有任何效果。以下是我的最后一次尝试,我已经阅读了关于 SO 的每一篇文章,但没有看到任何建议的工作,有人可以帮忙吗?我正在使用 PHPMailer、html2pdf 和 html2canvas,它们都会在点击时生成正确的文档,只是将它们发送到 php mailer 不起作用。我正在获取无法打开的文档...以下是我最后的尝试...使用 file_get_contents 方法我得到 0 大小的数据。

PDF尝试:

var element = document.getElementById('printableArea');
// var opt = {  
//      filename:     'nalog.pdf'
// };   
html2pdf().from(element).toPdf().output('datauristring').then(function (pdfAsString) {
    pdfcontent = pdfAsString.replace(/&/,"%26").replace(/=/,"%3D");
     var x = new XMLHttpRequest();
     var url = "xxx/mail.php";
     x.open("POST", url, true);
     x.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
     x.send("data="+pdfcontent);
console.log(pdfcontent);
        });

PHP:

$mail->AddStringAttachment($_POST['data'],"nalog.pdf"); 

PNG尝试:

html2canvas($('#printableArea')[0], {
  width: 1200
}).then(function(canvas) {
    var data = canvas.toDataURL("image/png");
    pdfcontent = data.replace(/&/,"%26").replace(/=/,"%3D");
    var x = new XMLHttpRequest();
    var url = "xxx/mail.php";
    x.open("POST", url, true);
    x.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    x.send("data="+pdfcontent);
    console.log(pdfcontent);
});

PHP:

$mail->AddStringAttachment($_POST['data'],"nalog.png"); 

编辑: 为了更新我的问题,我从 anwser 提出了建议并尝试对其进行调试,但没有任何帮助。我制作了 PHP file_put_contents() 并与控制台日志进行了比较,它是相同的数据。

还有其他建议吗?

标签: javascriptphpphpmailerhtml2canvashtml2pdf

解决方案


在几乎放弃之后,终于让它工作了。它结合了这里链接和建议的几件事。github html2pdf上的这篇文章也有所帮助。

我把它贴在这里,因为没有一个例子对我有用,我花了两天时间才找到对我和我的葬礼有用的东西。希望它可以帮助某人。

window.onload = function pdfDivload (){
let el = document.getElementById('printableArea');
let opt = {
    margin:       1,
    filename:     'myfile.pdf',
    image:        { type: 'jpeg', quality: 0.98 },
    html2canvas:  { scale: 2 },
    jsPDF:        { unit: 'in', format: 'A4', orientation: 'portrait' }
};

html2pdf().set( opt ).from( el ).toPdf().output('datauristring').then(function( pdfAsString ) {
    let data = {
        'fileDataURI': pdfAsString,
    };
    $.post( "../prog/mail.php", data);
    console.log( data );
} );
};

PHP:

  if (isset($_POST['fileDataURI'])) {

                $pdfdoc         = $_POST['fileDataURI'];

            $b64file        = trim( str_replace( 'data:application/pdf;base64,', '', $pdfdoc ) );
            $b64file        = str_replace( ' ', '+', $b64file );
            $decoded_pdf    = base64_decode( $b64file );
            //file_put_contents( $attachment, $decoded_pdf );

            $mail = new PHPMailer;
            $mail->setFrom( 'xxx@xxx.hr', 'website' );
            $mail->addAddress( 'xxx@gxxx.com', 'OdedTa' );
            $mail->Subject  = 'First PHPMailer Message';
            $mail->Body     = 'Hi! This is my first e-mail sent through PHPMailer.';
            $mail->addStringAttachment($decoded_pdf, "nalog.pdf");
            $mail->isHTML( true );
            $mail->send();

推荐阅读