0

我得到了这个例外,但没有其他有用的细节。我正在使用 mpdf6.1.0 和 PHPZip2.0.8 。我的代码如下所示。

$zip = new Zip();
$mpdf = null;
foreach ($htmlArr as $name=>$html) {
    if ($mpdf === null) {
        $mpdf = initializePdf();
    }
    $mpdf->WriteHTML($html);
    $zip->addFile($mpdf->Output('', 'S'), $name, microtime());
    $mpdf = null;
}
$zip->sendZip('test.zip', "application/zip", 'test.zip');

initializePdf 函数如下所示。

function initializePdf() {
    $mpdf = new mPDF('CJK', 'A4', '12px', '', 10, 10, 5, 5);
    $mpdf->useAdobeCJK = true;
    $mpdf->autoScriptToLang = true;
    $mpdf->autoLangToFont = true;
    $mpdf->SetDisplayMode('fullpage');
    $mpdf->autoMarginPadding = 1;
    $mpdf->cropMarkLength = 0;
    $mpdf->autoPageBreak = true;

    return $mpdf;
}

htmlArr 非常好。我已经检查了好几次了。我可以在 $zip->addFile 行输出 PDF 文件。但是当我更改为 zip 文件中的输出流时。它发生 BufferNotEmpty 异常。请帮我。我正在使用 CakePHP3.2.3 。当我将代码隔离到一个简单的 php 文件中时,它工作得非常好。但我不知道为什么我会得到例外。调试页面向我显示了这样的消息

Unable to send 'test.zip'. Output buffer contains the following text (typically warning or errors):
4

1 回答 1

0

I find out that there's a single space in the output buffer. That's why the error message looks so strang. PHPZip chose to stop because of it detected that the buffer is not empty. I put one line before sendZip to clean the output buffer like below.

$zip = new Zip();
$mpdf = null;
foreach ($htmlArr as $name=>$html) {
    if ($mpdf === null) {
        $mpdf = initializePdf();
    }
    $mpdf->WriteHTML($html);
    $zip->addFile($mpdf->Output('', 'S'), $name, microtime());
    $mpdf = null;
}
// clean buffer
ob_clean();
// send zip stream to browser
$zip->sendZip('test.zip', "application/zip", 'test.zip');
于 2016-07-06T05:57:47.393 回答