Skip to content

Embedded File removed #3658

@gunter423

Description

@gunter423

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions