html2pdf.js 生成的 pdf 附加与 laravel 邮件

问题描述

我正在尝试将我的 html dom 转换为 pdf 并将该 pdf 作为附件与电子邮件一起发送。

我在前端使用 Vue2 和 html2pdf.js。我正在使用它以 pdf 格式下载 dom 并将 pdf 作为电子邮件附件发送。直接下载的 pdf 可以正确打开,但我在电子邮件中收到的 pdf 无法打开,在 chrome 和 Failed to load pdf document显示错误 unable to open document,File type plain text document (text/plain) is not supported

这是我在前端的 html2pdf 代码

let opt = {
   margin:       1,filename:     'summary.pdf',image:        { type: 'jpeg',quality: 0.98 },html2canvas:  { scale: 2 },jsPDF:        { unit: 'in',format: 'A4',orientation: 'portrait' }
};

let canvas = document.querySelector("#summary-block");

html2pdf().from(canvas).toPdf()
.output('datauristring')
.then(function (pdfAsstring) {
    const data = {
        pdf: pdfAsstring,emails: this.emails
    }
    console.log("promise-2-1");
    console.log(this.selectedListing);
    HTTP.post(`api/listings/${this.selectedListing.id}/share`,data)
    .then((response) => {
        this.btnLoading = false;
        this.tagError = false;
        this.emails = [];
    })

}.bind(this));

这是我在 Laravel 控制器中的 PHP 代码

$request = $this->request;
$b64file = trim( str_replace( 'data:application/pdf;base64,','',$request->pdf ) );
$b64file = str_replace( ' ','+',$b64file );
$decoded_pdf = base64_decode( $b64file );

if(!empty($request->emails))
{
    foreach ($request->emails as $recipient)
    {
        $user = User::find($recipient["key"]);
        $__name__ = 'User';
        $__MAIL_FROM_NAME__ = "Repylot";
        $__ADMIN_EMAIL__ = "support@repylot.com";
        $search = ['__name__','__MAIL_FROM_NAME__','__ADMIN_EMAIL__'];
        $replace = [$__name__,$__MAIL_FROM_NAME__,$__ADMIN_EMAIL__];
        Mail::to($user)->send(new EmailNotificationWithAttachment($request->pdf,$search,$replace,"offer_summary"));
    }
}

和 EmailNotificationWithAttachment 有句柄

return $this->subject($subject)->html($template)->attachData(
        $attachment,'summary.pdf',[
        'mime' => 'application/pdf',]);

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)