Wojtek
Wojtek

Reputation: 51

pdf generation not working in framework Zend

I have a script that generates a PDF in Zend. I copied the script from converting image to pdf to another directory on the server. I now get the error:

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

Code of website, example link to image (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();
  }

Upvotes: 0

Views: 2299

Answers (2)

janenz00
janenz00

Reputation: 3310

Zend_Pdf_Image::imageWithPath expects a valid file and uses is_file function call to check the file existence.

First of all, use absolute path to the image, instead of using the relative one. You can specify the absolute path by referring to your APPLICATION_PATH. For example,

  APPLICATION_PATH . '/../public/data

If APPLICATION_PATH is not already defined in your code, paste this code in your public/index.php

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

Then, check if 'data/'.$GET['id'].'/crop'.$_GET['id'].'.jpg' exists. Also, check if the file has proper permissions to be accessed by PHP.

Note : Use the Zend request object instead of the $_GET.

Upvotes: 2

Tim Fountain
Tim Fountain

Reputation: 33148

Use an absolute path:

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

or:

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

you might also want to put some validation on $_GET['id'].

Upvotes: 0

Related Questions