无法在浏览器中显示 Zend pdf

发布于 2024-09-10 05:11:54 字数 1750 浏览 5 评论 0原文

我在 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 时没有问题

有人有想法吗?

谢谢

I've got a strange problem with zend pdf, I just can't get the pdf to show
in my action i've got this code:

$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 creates this stacktrace:

#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}

When creating an new pdf there is no problem

Does anybody have an idea?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

长发绾君心 2024-09-17 05:11:54

我意识到这是一个老问题,但我通过反复试验找到了答案。这就是我的操作,让 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();

I realize this is an old question but I found the answer to this through trial and error. This is what my action looks like to get just the pdf to show up in the browser. This works in Chrome. I haven't tested it in any other browser.

    $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();
且行且努力 2024-09-17 05:11:54

方法 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');

Method 1: Get the binary string outside of Zend_Pdf

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

Method 2: Set the $load parameter true

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

Method 3: Use the static load-method

$pdf = Zend_Pdf::load('path/to/file.pdf');
挥剑断情 2024-09-17 05:11:54

在 Magento 1.9 中,它使用 Zend_Pdf 在浏览器中渲染 PDF,以下内容对我有用:

// 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

In Magento 1.9, which uses Zend_Pdf, to render PDF in browser, the following works for me:

// 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文