Search code examples
phphtmlpdfphpmailerdompdf

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


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

Solution

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