我使用CakePHP 3.4
和CakePdf
使用domPdf
插件生成pdf文件。
还可以使用QrCode
插件随时生成QR码。
我想在我的pdf文件中使用生成的QR码而不将它们保存在磁盘上。
这就是我所做的
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;
}
并在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>
在直接视图中使用相同的代码(没有pdf生成),QR代码在屏幕上呈现。
如何直接在pdf文件中使用呈现的响应?
编辑2
CakePdf插件配置
Configure::write('CakePdf', [
'engine' => 'CakePdf.DomPdf',
'margin' => [
'bottom' => 15,
'left' => 50,
'right' => 30,
'top' => 45
],
'orientation' => 'potrait',
'download' => false,
'options' => [
'isRemoteEnabled' => true,
],
]);