如何使用CakePDF和Dompdf将外部图像嵌入到PDF文件中?

时间:2017-05-08 17:47:25

标签: pdf cakephp dompdf cakephp-3.4

我使用CakePHP 3.4CakePdf使用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>

但这并未将QR码添加到生成的pdf文件中,并显示错误 enter image description here

在直接视图中使用相同的代码(没有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,
    ],
]);

0 个答案:

没有答案