Thank for taking a while reading this post.
I am writing a PDF file on the fly with ZendPdf, but in the moment to add a picture to the PDF it is shown mirrored:
Here is the effect to better understand: http://snag.gy/ZYbWp.jpg
The original image is fine.
Could anybody tell me why is the image mirrored and how could I render it as the original one?
I paste here the whole action where it is generated the pdf.
public function createPdfAction()
{
$country = $this->params()->fromRoute('lang', null);
$pdf = new \ZendPdf\PdfDocument();
// Add new page generated by ZendPdf\Pdf object
// (page is attached to the specified the document)
$pdf->pages[] = ($page1 = $pdf->newPage('A4'));
$width = $page1->getWidth();
$height = $page1->getHeight();
$imageFile = dirname(__FILE__) . '/../../../../../html/img/site/logo_peug_scooter.jpg';
if ( !isset( $pdf->imageCache[$imageFile] ))
{
try {
// Create new image object
//$stampImage = ZendPdf\Image::imageWithPath($imageFile);
$pdf->imageCache[$imageFile] = ZendPdf\Image::imageWithPath($imageFile);
} catch (ZendPdf\Exception $e) {
// Example of operating with image loading exceptions.
if ($e->getMessage() != 'Image extension is not installed.' &&
$e->getMessage() != 'JPG support is not configured properly.') {
throw $e;
}
$pdf->imageCache[$imageFile] = null;
}
}
if (null != $pdf->imageCache[$imageFile]) {
$page1->drawImage( $pdf->imageCache[$imageFile], 50, $height, 50 + 220, $height - 70 );
}
// Create new font
$font = ZendPdf\Font::fontWithPath(dirname(__DIR__) . '/../../../../html/fonts/peugeot_style-webfont.ttf');
// Apply font and draw text
$page1->setFont($font, 16)
->setFillColor(ZendPdf\Color\Html::color('#0b2333'))
->drawText('DJANGO', 50, $height - 18 - 18 - 50);
if ('uk' == $country) {
$locale = 'en_GB'; //. strtoupper($country);
} else {
$locale = $country . '_' . strtoupper($country);
}
setlocale(LC_MONETARY, $locale);
$price = money_format('%i', 2660);
$font = ZendPdf\Font::fontWithPath(dirname(__DIR__) . '/../../../../html/fonts/peugeot_normal-webfont.ttf');
// Apply font and draw text
$page1->setFont($font, 16)
->setFillColor(ZendPdf\Color\Html::color('#0b2333'))
->drawText($price, 447, $height - 18 - 18 - 50);
$pdf->save('/tmp/pdfs/sample2.pdf');
$this->layout('layout/empty');
$viewModel = new ViewModel();
$viewModel->setTerminal(true);
return $viewModel;
}
I will appreciate any guide.
Thank you.
Note that the coordinate System has it's origin in the bottom left corner and also expects the 4 coordinate-parameters of drawImage to be provided in that order. From the bottom left corner of the image to the top right corner.
You were measuring from the bottom but handing the top left corner first:
$page1->drawImage( $pdf->imageCache[$imageFile], 50, $height, 50 + 220, $height - 70 );
Zend interprets the 2nd coordinate value as the bottom of the image. You set it to $height
(top edge of the page) and then the top of the image is at $height-70
measured from the bottom. (70px below the top edge of the page)
So the top of the image for Zend is below the bottom of the image. Hence the mirrored Image. (I know, lots of tops and bottoms.. in doubt: shuffle the 4 values until you're lucky.) It should instead be:
$page1->drawImage( $pdf->imageCache[$imageFile], 50, $height - 70, 50 + 220, $height);