Support the ongoing development of Laravel.io →
Mail Laravel

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

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

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

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

and EmailNotificationWithAttachment have handle

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

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.