0

我正在使用CakePHP 3.4CakePdf使用domPdf插件生成 pdf 文件。

还使用QrCode插件随时随地生成二维码。

我想在我的 pdf 文件中使用生成的 QR 码而不将它们保存在磁盘上。

这就是我所做的

文件下载控制器.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 文件中并显示错误为 在此处输入图像描述

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

0 回答 0