0

I'm using CakePHP 3.4 and CakePdf to generate pdf files using domPdf plugin.

also using QrCode plugin to generate QR Code on the go.

I want to use generated QR Code in my pdf file without saving them on disk.

This is what I have done

FileDownloadController.php

public function view($username = null)
{
    $this->loadModel('Users');

    $user = $this->Users->find()
        ->where(['Users.username' => $username])
        ->contain([])
        ->first();

    $this->viewBuilder()->options([
        'pdfConfig' => [
            'filename' => 'profplus_file_'.$user->id.'.pdf',
        ]
    ]);

    $this->set('user', $user);
}

public function generateQR($user_id)
{
    $this->autoRender = false;

    $this->loadModel('Users');

    $user = $this->Users->get($user_id);

    $string = Router::url('/', true) . 'q' . DS . $user->id;;
    $qrCode = new QrCode($string);
    $qrCode
    ->setSize(100);

    // Create a response object
    $response = $this->response;
    $response = $response->withType($qrCode->getContentType(PngWriter::class))
    ->withStringBody($qrCode->writeString(PngWriter::class));

    return $response;
}

and in the view of pdf pdf/view.ctp

<table>
    <tr>
        <td class="left-section">
            <table>
                <tr>
                    <td class="top-name">
                        <?= $user->first_name .' '. $user->last_name ?>
                    </td>
                </tr>
                <tr>
                    <td class="top-address">
                        <i class="fa3 fa-phone"></i> <?= $user->mobile ?> | <i class="fa fa-envelope"></i>: <?= $user->email ?>
                    </td>
                </tr>
            </table>
        </td>
        <td class="right-section">
            <img src="<?= $this->Url->build(['prefix' => false, 'controller' => 'ResumeDownload', 'action' => 'generateQR', $user->id], true) ?>" />
        </td>
    </tr>
</table>

But this is not adding the QR Code to the generated pdf file and shows error as enter image description here

using same code in direct view (without pdf generation), QR code is rendered on screen.

How can I use the rendered response directly in a pdf file?

Edit 2

CakePdf plugin configuration

Configure::write('CakePdf', [
    'engine' => 'CakePdf.DomPdf',
    'margin' => [
        'bottom' => 15,
        'left' => 50,
        'right' => 30,
        'top' => 45
    ],
    'orientation' => 'potrait',
    'download' => false,
    'options' => [
        'isRemoteEnabled' => true,
    ],
]);
Anuj TBE
  • 9,198
  • 27
  • 136
  • 285
  • Have you [**enabled `allow_url_fopen`**](https://github.com/dompdf/dompdf/wiki/Requirements#optional-configuration) and checked whether you may need to [**configure the HTTP context**](https://github.com/dompdf/dompdf/wiki/Usage#sethttpcontext)? – ndm May 08 '17 at 18:26
  • `allow_url_fopen` is set to `on` http://profplus.in/info.php – Anuj TBE May 09 '17 at 02:01
  • I'm adding image generated from the same server. Will it be called to be external image, as you have edited title of the question? – Anuj TBE May 09 '17 at 04:17
  • Yes it will, from the point of view of Dompdf, all non-local filesystem paths are external/remote content. Have you checked the second link regarding context? Also have you enabled the `enable_remote` or `is_remote_enabled` option? **https://github.com/dompdf/dompdf/issues/1118** – ndm May 09 '17 at 13:39
  • Yes, since I'm using `CakePdf` plugin which is further using `DomPdf` engine. I have included `isRemoteEnabled` set to `true` in configuration. Please see `Edit 2` – Anuj TBE May 09 '17 at 13:45
  • Check for possible warnings after rendering the PDF: `global $_dompdf_warnings; debug($_dompdf_warnings);`. Also what version of Dompdf are you using? – ndm May 09 '17 at 14:04
  • `CakePdf` version `3.2.3` and `DomPdf` version `0.8.0` – Anuj TBE May 09 '17 at 14:12
  • Thanks ndm, I somewhat solved it. I think the issue was there with configuration. adding `options` inside `engine` made it work – Anuj TBE May 09 '17 at 14:25
  • Ah, I overlooked that... – ndm May 09 '17 at 14:26

0 Answers0