-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Description
Hello,
I noticed a problem.
When changing the page size:
$customPaper = [0, 0, 800, 1100];
$domPdf->setPaper($customPaper);and then calling render() :
$domPdf->render();Dompdf recreates the canvas and replaces the existing one with the one containing embeddedFile.
The relevant code is in src/Dompdf.php at line 766. The if at line 765 is true because the width and height specified in $customPaper do not match the default dimensions (612 width and 792 height).
I worked around this issue by doing a pre-render to "lock" the canvas with the custom size, and only then adding the embedded file:
private function preRenderCanvasDomPdf(): Dompdf
{
$options = new Options([
'isRemoteEnabled' => true,
'isPdfAEnabled' => true,
'defaultFont' => 'DejaVu Sans',
]);
$domPdf = new Dompdf($options);
$customPaper = [0, 0, 800, 1100];
$domPdf->setPaper($customPaper);
$domPdf->loadHtml($yourTemplate);
$domPdf->render();
return $domPdf;
}
$dompdf = $this->preRenderCanvasDomPdf();
$cpdf = $dompdf->getCanvas()->get_cpdf();
$cpdf->addEmbeddedFile(/* ... */);This is the only reliable way I’ve found without touching the library itself.
If anyone’s motivated, a PR would be more than welcome
I’ve tried to make my explanation as clear as possible, but please feel free to give me feedback if anything isn’t clear