接收“未找到文件”;Zend PDF


Receiving "file not found" Zend PDF

下面的代码是一个示例,它可以工作(从文件中加载pdf,绘制单个字符串,成功),但是如果我将invoice.pdf(来自教程的示例pdf)更改为我的pdf, buyersguide.pdf,我在firefox上收到一个错误,文件未找到。

    <?php
   header("Content-Type: application/x-pdf");
   //header("Content-Disposition: attachment; filename=invoice-". date("Y-m-d-H-i") . ".pdf");
   header("Cache-Control: no-cache, must-revalidate");
   require_once 'zendframework/library/Zend/Loader/Autoloader.php';
   //Zend_Loader::registerAutoload();   
   $loader = Zend_Loader_Autoloader::getInstance();
   // load the invoice
   $invoice = Zend_Pdf::load("invoice.pdf");
   $page = $invoice->pages[0];

   $font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_TIMES_BOLD);
   $page->setFont($font, 12);
   // invoice information*/
   $page->drawText('success ', 420,642);
   // output the PDF
   echo $invoice->render();
?>

文件名更改后的代码:

    <?php
   header("Content-Type: application/x-pdf");
   //header("Content-Disposition: attachment; filename=invoice-". date("Y-m-d-H-i") . ".pdf");
   header("Cache-Control: no-cache, must-revalidate");
   require_once 'zendframework/library/Zend/Loader/Autoloader.php';
   //Zend_Loader::registerAutoload();   
   $loader = Zend_Loader_Autoloader::getInstance();
   // load the invoice
   $invoice = Zend_Pdf::load("buyersguide.pdf");
   $page = $invoice->pages[0];

   $font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_TIMES_BOLD);
   $page->setFont($font, 12);
   // invoice information*/
   $page->drawText('success ', 420,642);
   // output the PDF
   echo $invoice->render();
?>

PDF格式的买家指南来自政府网站。PDF通常在adobereader中加载。买家指南PDF: http://www.consumer.ftc.gov/articles/pdf-0083-buyers-guide.pdf

下面的代码演示了ZF1的最新稳定版本(1.12.3)将成功地处理您遇到问题的文件:

<?php
set_include_path("PATH/TO/zf1/library" . PATH_SEPARATOR . get_include_path());
require_once 'Zend/Loader/Autoloader.php';
$loader = Zend_Loader_Autoloader::getInstance();
// Grab a copy of the file from the remote server
// This is just for demo purposes only
// Obviously you'd want to keep a copy of this file locally
$source = "http://www.consumer.ftc.gov/articles/pdf-0083-buyers-guide.pdf";
file_put_contents("template.pdf", file_get_contents($source));
// load the invoice
$invoice = Zend_Pdf::load("template.pdf");
$page = $invoice->pages[0];
$font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_TIMES_BOLD);
$page->setFont($font, 12);
// invoice information*/
$page->drawText("success ", 320, 700);
// Save the file to disk
$filename = "invoice-" . date("Y-m-d-H-i") . ".pdf";
file_put_contents($filename, $invoice->render());
// output the PDF
header("Content-Type: application/pdf");
header("Content-Disposition: attachment; filename='"$filename'"" );
header("Cache-Control: no-cache, must-revalidate");
header("Content-Length: " . filesize( $filename ));
readfile( $filename );

这段代码离生产就绪还很远,但它可以工作。如果它不适合你,那么你应该打印出每个函数调用的返回代码,直到你确定哪一行出错。