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->setPaper('A4', 'portrait');



$pdfData = $dompdf->output();

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

Leave a Reply

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