无法在浏览器中获得Zend pdf节目

时间:2010-07-19 19:10:17

标签: php zend-framework pdf zend-pdf

我对zend pdf有一个奇怪的问题,我只是无法得到pdf来显示 在我的行动中我得到了这段代码:

$this->_helper->layout->disableLayout();
$this->_helper->viewRenderer->setNoRender();

$pdf = new Zend_Pdf('path/to/file.pdf');

$this->getResponse()->setHeader('Content-type', 'application/x-pdf', true);
$this->getResponse()->setHeader('Content-disposition', 'inline; filename=filetrace.pdf', true);
$this->getResponse()->setBody($pdf->render());

Zend创建了这个堆栈跟踪:

#0 E:\SRVApps\Zend\ZendServer\share\ZendFramework\library\Zend\Pdf.php(303): Zend_Pdf_Parser->__construct('http://example...', Object(Zend_Pdf_ElementFactory_Proxy), false)
#1 E:\wwwroot\test\htdocs\application\modules\dashboard\controllers\FileController.php(171): Zend_Pdf->__construct('http://example...')
#2 E:\SRVApps\Zend\ZendServer\share\ZendFramework\library\Zend\Controller\Action.php(513): Dashboard_FileController->showAction()
#3 E:\SRVApps\Zend\ZendServer\share\ZendFramework\library\Zend\Controller\Dispatcher\Standard.php(289): Zend_Controller_Action->dispatch('showAction')
#4 E:\SRVApps\Zend\ZendServer\share\ZendFramework\library\Zend\Controller\Front.php(954): Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http), Object(Zend_Controller_Response_Http))
#5 E:\SRVApps\Zend\ZendServer\share\ZendFramework\library\Zend\Application\Bootstrap\Bootstrap.php(97): Zend_Controller_Front->dispatch()
#6 E:\SRVApps\Zend\ZendServer\share\ZendFramework\library\Zend\Application.php(366): Zend_Application_Bootstrap_Bootstrap->run()
#7 E:\wwwroot\test\htdocs\public\index.php(26): Zend_Application->run()
#8 {main}

创建新的pdf时没有问题

有人有想法吗?

由于

3 个答案:

答案 0 :(得分:2)

我意识到这是一个老问题,但我通过反复试验找到了答案。这就是我的行为看起来只是在浏览器中显示的pdf。这适用于Chrome。我没有在任何其他浏览器中测试它。

    $this->_helper->viewRenderer->setNoRender();
    $this->_helper->layout()->disableLayout();
    $this->getResponse()
        ->setHeader('Content-Disposition', 'inline; filename=foo.pdf')
        ->setHeader('Content-type', 'application/pdf');
    $pdf = Zend_Pdf::load('/path/to/my.pdf');
    echo $pdf->render();

答案 1 :(得分:1)

方法1:获取Zend_Pdf

之外的二进制字符串
$file = file_get_contents('path/to/file.pdf')
$pdf = new Zend_Pdf($file);

方法2:设置$load参数true

$pdf = new Zend_Pdf('path/to/file.pdf', null, true);

方法3:使用静态load - 方法

$pdf = Zend_Pdf::load('path/to/file.pdf');

答案 2 :(得分:0)

在使用Zend_Pdf在浏览器中呈现PDF的Magento 1.9中,以下对我有用:

// In controller action
$filename = 'foo.pdf';
$pdf = Zend_Pdf::load('path/to/foo.pdf');
$content = $pdf->render();
$contentLength = strlen($content);

$this->getResponse()
    ->setHttpResponseCode(200)
    ->setHeader('Pragma', 'public', true)
    ->setHeader('Cache-Control', 'must-revalidate, post-check=0, pre-check=0', true)
    ->setHeader('Content-type', 'application/pdf', true)
    ->setHeader('Content-Length', $contentLength, true)
    ->setHeader('Content-Disposition', 'inline; filename="'.$filename.'"', true)
    ->setHeader('Content-Transfer-Encoding', 'binary', true)
    ->setHeader('Accept-Ranges', 'bytes', true)
    ->setBody($content);
// Exit controller action