Create/generate PDF file in Silverstripe via Dompdf

install dompdf first:

composer require dompdf/dompdf
in Silverstripe controller:
use Dompdf\Dompdf;

use SilverStripe\Control\Email\Email;
$pdfHtml = $memberapplication->renderWith('PDF/NewMemberPdf');

exit($pdfHtml); // uncomment for pdf debug

$dompdf = newDompdf();

$dompdf->loadHtml($pdfHtml);

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

$dompdf->render();

//$dompdf->stream();

$pdfData = $dompdf->output();

$pdfFilename = 'pdf/NewMemberPdf-'.$memberapplication->ID.'.pdf';
file_put_contents($pdfFilename,$pdfData); // will save to public/pdf folder
PDF/NewMemberPdf.ss in the SilverStripe templates folder using $memberapplication object’s variables
send an email with the pdf:
$email->addAttachmentFromData($pdfData, $pdfFilename);
doc: https://github.com/dompdf/dompdf

Leave a Reply

Your email address will not be published. Required fields are marked *