【问题标题】:pdf generation not working in framework Zendpdf生成在Zend框架中不起作用
【发布时间】:2012-10-11 23:34:50
【问题描述】:

我有一个在 Zend 中生成 PDF 的脚本。我将脚本从将图像转换为 pdf 复制到服务器上的另一个目录。我现在得到错误:

Fatal error: Uncaught exception 'Zend_Pdf_Exception' with message 'Cannot create image resource. 
File not found.' in /kalendarz/Zend/Pdf/Resource/ImageFactory.php:38 
 Stack trace: 
 #0 /kalendarz/Zend/Pdf/Image.php(124): Zend_Pdf_Resource_ImageFactory::factory('data/0116b4/cro...') 
 #1 /kalendarz/cms.php(56): Zend_Pdf_Image::imageWithPath('data/0116b4/cro...') 
 #2 {main} thrown in /kalendarz/Zend/Pdf/Resource/ImageFactory.php on line 38

网站代码,图片链接示例(http://tinyurl.com/8srbfza):

else if($_GET['action']=='generate') {
    //1 punkt typograficzny postscriptowy (cyfrowy) = 1/72 cala = 0,3528 mm
    function mm_to_pt($size_mm) {
      return $size_mm/0.3528;
    }

    require_once("Zend/Pdf.php");

    $pdf = new Zend_Pdf(); 

    $page_w = mm_to_pt(100);
    $page_h = mm_to_pt(90);

    $page = $pdf->newPage($page_w.':'.$page_h.':'); 
    $pdf->pages[] = $page; 

    $imagePath= 'data/'.$_GET['id'].'/crop_'.$_GET['id'].'.jpg'; //to nie jest miniaturka
    $image = Zend_Pdf_Image::imageWithPath($imagePath);


    $left = mm_to_pt(0);
    $right = mm_to_pt(100);
    $top = mm_to_pt(90);
    $bottom = mm_to_pt(0);

    $page->drawImage($image, $left, $bottom, $right, $top);     

    $pdfData = $pdf->render(); 

    header("Content-Disposition: inline; filename=".$_GET['id'].".pdf"); 
    header("Content-type: application/x-pdf"); 
    echo $pdfData; 
    die();
  }

【问题讨论】:

  • 只需添加产生错误的代码。

标签: php zend-framework pdf-generation


【解决方案1】:

Zend_Pdf_Image::imageWithPath 需要一个有效文件并使用 is_file 函数调用来检查文件是否存在。

首先,使用图像的绝对路径,而不是使用相对路径。您可以通过引用您的 APPLICATION_PATH 来指定绝对路径。例如,

  APPLICATION_PATH . '/../public/data

如果您的代码中尚未定义 APPLICATION_PATH,请将此代码粘贴到您的 public/index.php 中

 defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));

然后,检查 'data/'.$GET['id'].'/crop'.$_GET['id'].'.jpg' 是否存在。另外,检查文件是否有适当的权限供 PHP 访问。

注意:使用Zend request object 而不是 $_GET。

【讨论】:

  • 你的数据文件夹的绝对路径是什么?
  • 尝试将 /kalendarz/data/0116b4/crop_0116b4.jpg 作为 imageWithPath 的参数
  • 另外,检查文件是否有 apache 用户的读取权限
  • Fatal error: Uncaught exception 'Zend_Pdf_Exception' with message 'Cannot create image resource. File not found.' in /kalendarz/Zend/Pdf/Resource/ImageFactory.php:38 Stack trace: #0 /kalendarz/Zend/Pdf/Image.php(124): Zend_Pdf_Resource_ImageFactory::factory('/kalendarz/data...') #1 /kalendarz/cms.php(42): Zend_Pdf_Image::imageWithPath('/kalendarz/data...') #2 {main} thrown in /kalendarz/Zend/Pdf/Resource/ImageFactory.php on line 38
  • /kalendarz/ 不是根文件夹吗?
【解决方案2】:

使用绝对路径:

$imagePath = '/kalendarz/data/'.$_GET['id'].'/crop_'.$_GET['id'].'.jpg';

或:

$imagePath = APPLICATION_PATH.'/../data/'.$_GET['id'].'/crop_'.$_GET['id'].'.jpg';

您可能还想对$_GET['id'] 进行一些验证。

【讨论】:

  • $imagePath = '/kalendarz/'.$_GET['id'].'/crop_'.$_GET['id'].'.jpg';同样的错误Notice: Use of undefined constant APPLICATION_PATH - assumed 'APPLICATION_PATH' in /kalendarz/cms.php on line 42 APPLICATION_PATH/data/0116b4/crop_0116b4.jpg Fatal error: Uncaught exception 'Zend_Pdf_Exception' with message 'Cannot create image resource. File not found.' in /kalendarz/Zend/Pdf/Resource/ImageFactory.php:38 Stack trace: #0 /kalendarz/Zend/Pdf/Image.php(124): Zend_Pdf_Resource_ImageFactory::factory('APPLICATION_PAT...') #1 /kalendarz/cms.php(44): .....第二种方式
  • 您确定路径正确吗?你说你已经移动了服务器,所以新服务器可能会有所不同。
  • 错误仍然是“找不到文件”吗?如果是这样,您是否 100% 确定您使用的路径是正确的?
  • 是的,仍然出现错误“找不到文件”。路径来自 phpinfo()。我不知道出了什么问题。
  • 这是不正确的到图像文件的路径,您的phpinfo屏幕截图没有帮助,因为它没有显示这是什么。找出图像文件在文件系统上的位置,并将其与您提供给imageWithPath()的路径进行比较
猜你喜欢
  • 2013-11-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-29
  • 1970-01-01
  • 1970-01-01
  • 2010-10-22
相关资源
最近更新 更多