Friday, February 4, 2022

[FIXED] Cakepdf not showing image for DomPdf

Issue

I am using cakepdf plugin to generate pdf in cakephp application. I am using below configuration for dompdf

Configure::write('CakePdf', [
            // 'engine' => 'CakePdf.WkHtmlToPdf',
            'engine' => 'CakePdf.DomPdf',
            'margin' => [
                'bottom' => 15,
                'left' => 50,
                'right' => 30,
                'top' => 45
            ],
            'orientation' => 'landscape',
            'download' => false,
            'isRemoteEnabled'=> true
            // 'enable-local-file-access' => true
]);

I have added 'isRemoteEnabled'=> true

For display image I have used

<img src="<?= WWW_ROOT ?>img/4dx/01/00.png" />

I have also tried by

<?php echo $this->Html->image('4dx/01/00.png', ['fullBase' => true]); ?>

Same result no any change found. $this->Html->image this way also not working for WkHtmlToPdf. I am using PHP version 7.4.x

This image perfectly showing if I use WkHtmlToPdf engine, but for dompdf I am not getting image, it's showing not found. Like below image

enter image description here


Solution

Engine options must be passed via the engine option, eg:

'engine' => [
    'className' => 'CakePdf.DomPdf',
    'options' => [
        'isRemoteEnabled' => true,
    ]
],

Also note that the WWW_ROOT constant holds is a filesystem path! In order to be able to access the filesystem you must configure the chroot option in recent versions of Dompdf!

'engine' => [
    'className' => 'CakePdf.DomPdf',
    'options' => [
        // allow local file access to webroot and Dompdf files
        'chroot' => [
            WWW_ROOT,
            ROOT . DS . 'vendor' . DS . 'dompdf' . DS . 'dompdf' . DS,
        ],
    ]
],

See also



Answered By - ndm

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.