使用PHPMailer和html2pdf通过电子邮件发送PDF数据 [英] Sending PDF data via email using PHPMailer and html2pdf

查看:114
本文介绍了使用PHPMailer和html2pdf通过电子邮件发送PDF数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过电子邮件发送PDF或PNG,但似乎没有任何效果.以下是我的LAST尝试,我已经阅读了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()并使用控制台日志对其进行了编辑,它是相同的数据.

还有其他建议吗?

解决方案

几乎放弃后,终于可以使用它了.它是这里链接和建议的几件事的组合. 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();

Im trying to send PDF or PNG over email and nothing seems to work. Below is my LAST attemnt, I have read every single article here on SO and nothing sees to work what is suggested, can someone please help? I am using PHPMailer, html2pdf and html2canvas, both of those produce proper documents on click, just sending them to php mailer dos not work. I am getting documents that can not be opened... Below are my last attempts... With file_get_contents method i get 0 sized data.

PDF attempt:

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 attempt:

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"); 

EDIT: To update my question, I have made suggested things from anwser and tryed to debug it and nothing helped. I made PHP file_put_contents() and compere it with console log and it is the same data.

Any other suggestions?

解决方案

After almost giving up, finally got it to work. Its a combination of few things linked here and suggested. This post on github html2pdf helped a bit also.

I'm posting it here as none of the examples worked for me, took me two days to find what works for me and my interment. Hope it helps someone.

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();

这篇关于使用PHPMailer和html2pdf通过电子邮件发送PDF数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆