Home > front end >  How can I Send HTML outputs to an email as a PDF using DOMPDF
How can I Send HTML outputs to an email as a PDF using DOMPDF

Time:04-04

I'm using this library from Dompdf, after showing the file as a PDF I want to send it to my email, I have tried several methods but I have not found a solution so far.

Thanks in advance

$dompdf = new Dompdf();
$dompdf->loadHtml(html_entity_decode($output));
$dompdf->setPaper('A4');
$dompdf->render(); // Render the HTML as PDF
$dompdf->stream($invoiceFileName, array("Attachment" => false));

CodePudding user response:

I gather from the DOMPDF docs that you can get a PDF binary string without saving it to a file by doing this:

$pdfData = $dompdf->output();

and then you can pass that directly to PHPMailer as a string attachment so your PDF data never has to be written to a temporary file:

$mail-addStringAttachment($pdfData, 'file.pdf');

PHPMailer will take care of the encoding and MIME type for you.

CodePudding user response:

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();
?>
  • Related