Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using dynamic php generated images with MPDF

Tags:

php

yii

mpdf

I am using php and mpdf to generate a particular pdf. I have some pie diagrams in the pdf. For that I am using pChart. I am generating pie diagrams dynamically for rendering in pdf.

In my pdf all the static images are rendered properly, but no dynamically generated pie diagrams.

This one(static) works,

<div class="chart">
<?php echo CHtml::image(Yii::app()->baseUrl.'/images/color-block2.png', 'Logo screenshot', array('class' => 'logo_image')); ?>
</div>

But not this(dynamic)

<div class="chart">
<?php echo CHtml::image(Yii::app()->baseUrl.'/images/pie.png', 'Logo screenshot', array('class' => 'logo_image')); ?>
</div>

Ps: I am using Yii url rules for a clean url for pie diagram.

When i rendered the pdf_template as html, both images are rendered properly.

How can i implement dynamic images using mpdf without the $mpdf->Image() method.

I suspect i cannot use the $mpdf->Image() while generating a pdf from html.

Update:

After

$mPDF->showImageErrors = true;

I'm getting

mPDF error: IMAGE Error (http://csrt.dev/images/pie.png): Could not find image file
like image 221
Quicksilver Avatar asked Nov 22 '25 11:11

Quicksilver


1 Answers

Base64 encoded the image and embedded it in a <img> tag.

<div class="chart">
<img src="data:image/png;base64,xx5t.." />
</div>

I had the same problem using Yii, and found this to be the solution, however for other Yii/PHP users out there, you have to use base64_encode(your file contents) to get the part after base64, in the tag. for example: base64_encode(file_get_contents("../images/folder16.gif"));

like image 154
Quicksilver Avatar answered Nov 24 '25 01:11

Quicksilver