0

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

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

4

1 回答 1

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 );

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

于 2013-04-22T05:25:20.883 回答