收到“未找到文件”Zend PDF

时间:2013-04-10 02:34:58

标签: php zend-framework pdf zend-pdf

下面的代码是一个示例,它可以工作(从文件加载pdf,绘制单个字符串,成功),但是如果我将invoice.pdf(示例pdf从教程中)更改为我的pdf,buyerguide.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通常在adobe reader中加载。买家指南PDF:http://www.consumer.ftc.gov/articles/pdf-0083-buyers-guide.pdf

1 个答案:

答案 0 :(得分:0)

以下代码演示了最新的稳定版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 );

这段代码远非生产就绪,但可行。如果它对您不起作用,那么您应该打印出每个函数调用的返回代码,直到确定哪一行失败为止。