【问题标题】:PDF document error using Zend_Pdf使用 Zend_Pdf 的 PDF 文档错误
【发布时间】:2012-08-26 14:44:55
【问题描述】:

在 magento1.7 中,我在自定义控制器中尝试了类似下面的操作。

public function getPDF()
{
$imagePath=C:\Users\.........;
$image = Zend_Pdf_Image::imageWithPath($imagePath);
$page->drawImage($image, 40,764,240, 820);
.
.
.
$pdf->pages[] = $page;
$pdf->save("mydoc.pdf");
}

里面没有错误。它生成带有图像的 PDF,但 PDF 文档保存在 magento 文件夹中,而不是保存在我的下载文件夹中。在做了一些研究之后,我发现了以下几行并将它们添加到 $pdf->pages[] = $page; 之后。

  $pdfString = $pdf->render();
  header("Content-Disposition: attachment; filename=myfile.pdf");
  header("Content-type: application/x-pdf");
  echo $pdfString;

现在它会在“我的下载”文件夹中生成 PDF。当我尝试打开它时。它抛出错误说:Adobe阅读器无法打开myfile.pdf,因为它不是受支持的文件类型或文件已损坏............当我们尝试打开时会发生这种情况在 localhost 上生成的 PDF 文档或有其他原因。请让我知道为什么会发生此错误,并为我提供解决方案。

【问题讨论】:

  • 您能否在文本编辑器中查看 PDF 文件的内容,并查看 pdf 文件中显示的 PHP 错误或警告之类的内容?它保存的文件有多大?
  • @drew010:我试图在文本编辑器中打开 PDF 文档,它包含当前 phtml 文件(即调用函数 getPDF() 的文件)和字母数字字符的混合 html。我认为,由于其中存在 html 内容而发生错误。文件大小超过 6500k。您能帮我解决这个问题吗...
  • @JamesG:不,我们是否使用不同的文件名保存文件并不重要。如果我用“myfile.pdf”名称保存文件,仍然会出现同样的错误。
  • @Nida 对于初学者,在输出 PDF 之前添加这些行,如果可行,我会将其添加为答案。 $this->_helper->viewRenderer->setNoRender(true); $this->_helper->layout()->disableLayout(); 这是标准的 ZF 代码,用于防止布局和操作视图被渲染,希望 Magento 中提供相同的帮助程序。
  • “我的文档”也不好。您需要 'my_documents' 给它一个公平的机会。

标签: php zend-framework magento magento-1.7 zend-pdf


【解决方案1】:

您的问题可能是由于同时调用了 save() 和 render()。

save() 实际上调用了 render(),问题可能是由于尝试渲染 PDF 两次。

这也是一种资源浪费,如果您需要保存文件,最好先保存文件,然后将该文件直接提供给用户。

您可以在普通的旧 PHP 中执行此操作(使用 passthru 或 readfile),尽管 Zendframework 中有一些方法可以更好地执行此操作:)

// .. create PDF here.. 
$pdf->save("mydoc.pdf");

$file = 'mydoc.pdf';

if (file_exists($file)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($file));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    readfile($file);
    exit;
}
?>

如果您的代码在 Magento 控制器中:

    $this->getResponse()
        ->setHttpResponseCode(200)
        ->setHeader('Pragma', 'public', true)
        ->setHeader('Cache-Control', 'must-revalidate, post-check=0, pre-check=0', true)
        ->setHeader('Content-type', $contentType, true)
        ->setHeader('Content-Length', filesize($file))
        ->setHeader('Content-Disposition', 'attachment; filename="'.$fileName.'"')
        ->setHeader('Last-Modified', date('r'));

    $this->getResponse()->clearBody();
    $this->getResponse()->sendHeaders();

    $ioAdapter = new Varien_Io_File();
    if (!$ioAdapter->fileExists($file)) {
        Mage::throwException(Mage::helper('core')->__('File not found'));
    }
    $ioAdapter->open(array('path' => $ioAdapter->dirname($file)));
    $ioAdapter->streamOpen($file, 'r');
    while ($buffer = $ioAdapter->streamRead()) {
        print $buffer;
    }
    $ioAdapter->streamClose();
    exit(0);

【讨论】:

  • 为什么使用 Varien_Io_File 而不是:if(file_exists($file)) { $this->getResponse()->setBody(file_get_contents($file));}
  • 如果文件很大,当您阅读完整内容并将其作为字符串存储在响应中时,您会遇到麻烦..
猜你喜欢
  • 1970-01-01
  • 2010-09-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多