I’ve created an action button in admin product section. And when it’s clicked I want to generate or download a pdf file. I don’t know how to use the shopware 6 built-in pdf renderer. Can anyone help me? Or is there a documentation for this?
hi,
please a small piece of code below. Of cause you can also use twig to generate the content
use Dompdf\Dompdf;
use Dompdf\Options;
public function generatePdf($htmlContent, $mode = 'return', $filename = 'output.pdf') {
$options = new Options();
$options->set('defaultFont', 'Helvetica');
$options->set('isRemoteEnabled', true);
$dompdf = new Dompdf($options);
$a4 = [0, 0, 595.28, 841.89];
// $a4 = 'a4';
$dompdf->setPaper($a4, 'portrait');
$dompdf->loadHtml($htmlContent);
$dompdf->render();
if ($mode == 'return') {
return $dompdf->output();
} elseif (($mode == 'echo') or ($mode == 'stream')) {
$dompdf->stream($filename, ['Attachment' => true, 'compress' => true]);
} elseif ($mode == 'save') {
file_put_contents($filename, $dompdf->output());
}
return true;
}