html2pdf.js generated pdf attach with laravel Mail

  html2pdf, javascript, laravel, php, vue.js

I am trying to convert my html dom into pdf and send that pdf as an attachment with email.

I am using Vue2 in frontend along with html2pdf.js. I am using this to download the dom as pdf and sending the pdf as email attachment. The pdf which is directly downloading is opening correctly but the pdf I am receiving on email is unable to open showing error Failed to load pdf document on chrome and unable to open document , File type plain text document (text/plain) is not supported.

This is my html2pdf code on frontend:

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

.then(function (pdfAsString) {
    const data = {
        pdf: pdfAsString,
        emails: this.emails
    console.log(this.selectedListing);`api/listings/${}/share`, data)
    .then((response) => {
        this.btnLoading = false;
        this.tagError = false;
        this.emails = [];


This is my php code in my Laravel controller.

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

    foreach ($request->emails as $recipient)
        $user = User::find($recipient["key"]);
        $__NAME__ = 'User';
        $__MAIL_FROM_NAME__ = "Repylot";
        $__ADMIN_EMAIL__ = "[email protected]";
        $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"));

and EmailNotificationWithAttachment have handle

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

Source: Laravel

Leave a Reply