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
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,
],
]);