Dompdf - Attach Created PDF to Email

DOMPDF - attach created PDF to email

PHP's mail function has no "standard" file attachment method. It's an extremely barebones interface to the SMTP system that forces you to do ALL the work of attaching a file yourself.

I strongly suggest using PHPMailer or Swiftmailer to do the email for you - it reduces the heavy grunt work of generating your own MIME email and inserting the attachment (many many lines of code) does to maybe 5 lines total.

Note that neither of them will handle a streamed PDF from DOMPDF. You'll have to save the PDF to a temporary file and attach that,

Attach PDF file to email with DOMPDF in Laravel

The PDF also requires a view, like so:

// creating the pfd:
$pdf = PDF::loadView('pdf.multiple_pages', [
'pages' => $this->pages,
'title' => $this->options->get('documentName'),
]);

Where the view looks like this:

<?php

$imgPath = public_path('store/assets/images/new_logo.png');
$img = base64_encode(file_get_contents($imgPath));
?>
<html lang="{{ app()->getLocale() }}">
<head>
<title>{{ $title ?? 'document' }}</title>

</head>
<body>
<style>
@php
include(public_path('/css/pdf/multiple_pages.css'))
@endphp

div.page {
background-image: url("data:image/png;base64, {{ $img }}");
}
</style>
@foreach($pages as $page)
<div class="page">
{!! html_entity_decode($page) !!}
</div>
@endforeach
</body>
</html>

So i recommend first creating the PDF, and only then attaching it to the email

Keep in mind this is very specific to our own use case and you will need to modify this.

How can I Send HTML outputs to an email as a PDF using DOMPDF

Maybe this way will work for you:

<?php

$dompdf = new Dompdf();
$dompdf->loadHtml(html_entity_decode($output));
$dompdf->setPaper('A4');
$dompdf->render(); // Render the HTML as PDF

// https://github.com/dompdf/dompdf/wiki/Usage#output
$output = $dompdf->output();

// Save PDF Document
file_put_contents('Document.pdf', $output);

// Use PHPMailer
// $mail->isSMTP();
// $mail->Host = 'smtp.example.org';
// $mail->SMTPAuth = true;
// $mail->Username = 'alice';
// $mail->Password = '123456'
// $mail->SMTPSecure = 'tls';
// $mail->Port = 25;
$mail->addAttachment('Document.pdf');
$mail->send();
?>

Generate pdf with dompdf and send it as an attachment in email in cakephp

You can find how to save a file here: how to save DOMPDF generated content to file?

$html =
'<html><body>'.
'<p>Put your html here, or generate it with your favourite '.
'templating system.</p>'.
'</body></html>';

$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$output = $dompdf->output();
file_put_contents('Brochure.pdf', $output);

Sending email is very straight forward with CakeEmailL. Take a look here:
http://book.cakephp.org/2.0/en/core-utility-libraries/email.html#sending-attachments

$Email = new CakeEmail();
$Email->from(array('me@example.com' => 'My Site'));
$Email->to('you@example.com');
$Email->subject('About');
$Email->attachments('/full/file/path/Brochure.pdf');
$Email->send('My message');

How do I attach a generated pdf by dompdf into mail using sendgrid mail api?

Save your PDF file in the disk:

$output = $dompdf->output();
file_put_contents('output.pdf', $output);
$fileName = 'output.pdf'; // Pass this variable to sendEMailwithAttachment function

Then pass the file path to the mail sender. After sending remove your pdf file from server.

Source: how to save DOMPDF generated content to file?



Related Topics



Leave a reply



Submit