In my laravel project, I want to add a image watermark for the pdf page generated using DOMPDF. One approach I can go with is adding background image through html, but is there any direct way provided by DOMPDF to do so?
Dompdf version I am using is 0.7.0
PHP version is 7.0.18
Dompdf is optimized for HTML rendering, but if for some reason that's not feasible you can also add an image to the PDF directly using the canvas object.
With the releases available at this time (latest release being 0.8.0) you should be able to do this using something like the following code snippet:
$dompdf = new Dompdf();
$dompdf->loadHtml($html);
$dompdf->render();
$canvas = $dompdf->getCanvas();
$canvas->page_script('
$pdf->set_opacity(.5);
$pdf->image("/path/to/image.png", {x}, {y}, {w}, {h});
');
Where:
{x}
= horizontal (X) position from top left{y}
= vertical (Y) position from top left{w}
= image width{h}
= image height