Using Zend_Pdf and Zend_Pdf_Image with remote URL’s (files)

Zend_Pdf is a handy component of Zend Framework, but expects you to use local files. However if you need to open files that are located on a different server (such as an Amazon S3 bucket) you will need a way of getting them into Zend_Pdf first.

$pdf = Zend_Pdf::load($pdfUrl);

One solution is to stream them in via a string…

$pdf = Zend_Pdf::parse(file_get_contents($pdfUrl));

Similarly, if you are using Zend_Pdf_Image, which doesn’t have a parse method, you will need to save the remote stream locally first, then open the file locally…

$imagePath = sys_get_temp_dir() . '/' . basename($imageUrl);
file_put_contents($imagePath, file_get_contents($imageUrl));
$image = Zend_Pdf_Image::imageWithPath($imagePath);

1 comment