0

Using dompdf Convert Custom Email Template To Pdf

But After Create PDF I Want to Save That PDF into pub/media/customFolder

Here My Code :-

Define in email_templates.xml :-

 <?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Email:etc/email_templates.xsd">
    <template id="custom_template_id" label="custom_template_id" file="pdftemplate.html" type="html" module="Vendor_Module" area="frontend"/>
</config>

Get Email Template As Html :-

$testTemplate = $this->templateFactory->get('custom_template_id') //template Identifier
                ->setVars($templateVars)
                ->setOptions($templateOptions);

        $html = $testTemplate->processTemplate();

Create PDF :-

$dompdf = new Dompdf();

$dompdf->loadHtml($html); //$html is html of template

$dompdf->setPaper('A4', 'portrait');

$dompdf->render();

file_put_contents('custom_pdf.pdf', $dompdf->output());
2

Please add with below code :

$path = '/var/www/html/magento2/pub/media/template.pdf';
file_put_contents($path, $dompdf->output()); 
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="template.pdf"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($path));
header('Accept-Ranges: bytes');
readfile($path);

make sure directory permission is 777

| improve this answer | |
  • Nice and easy answer. +1 for that – Muhammad Hasham Aug 30 '19 at 6:07
  • 1+ for answer let me check – Rk Rathod Aug 30 '19 at 6:18
  • in which folder save PDF ?? – Rk Rathod Aug 30 '19 at 6:35
  • i m try your code code execute successfully but file not found.. – Rk Rathod Aug 30 '19 at 6:36
  • possible to set path /var/www/html/magento2/pub/media/customfolder – Anas Mansuri Aug 30 '19 at 6:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.