I'm using dompdf to generate a PDF file. In the file I'm including an image that is generated on the fly by PHP by using;
<img src="image.php" />
When I view the page I can see an error saying "Image not found" which links to the supposedly not found image which displays fine when you click on it.
I've tried adding a .png extension to the page generating the image but that doesn't make any difference. I've also saved the generated image to the server and linked to that instead and it works fine.
The only method I can think of to solve this is to save the image on the server and then link to that. Ideally, I don't want to do this so I was wondering if anybody had come across this before or if anyone can think of a fix for it.
The version of dompdf I'm using is 0.6.0 beta 3 and I'm using it with CodeIgniter.
How are you loading your HTML document into dompdf? $dompdf->load_html()
? $dompdf->load_html_file()
? To access a PHP-generated image you should be using the latter. Additionally, unless you are loading the document through a web server dompdf will grab the file itself, not the rendered file. Since image.php is not a valid image type dompdf will fail to load it.
There are two solutions:
- Load your document through your web server, e.g.
$dompdf->load_html_file('http://example.com/mydoc.html');
. When you do this the image reference will be interpreted as http://example.com/image.php
.
- Reference your image using a full URL (including domain) so that your server processes it before sending it back to dompdf, e.g.
<img src="http://example.com/image.php" />
.
First add this tag img src="image.php"
, you need to specify the complete root where is located your image, so it most end in the format required like JPG or PNG, etc., and in the configuration file (dompdf_config.inc.php
) you need to change this line DOMPDF_ENABLE_REMOTE
, which his default value is false change it to true and that is all :)
You can use as an alternative supported the Base64 image data
$html = '<div>
<p>IMAGE</p>
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMgAAABMCAIAAACnG28HAAAABmJLR0QA/wD/AP%2BgvaeTAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH1AgeBS42B7vBqgAAEhBJREFUeNrtnd%2BPHEcVhfcfBoknxAOIh0g88EsIIqKI1UprObYVe23FwQmskO0EYSskkCgkEo5MCMgoGBABhBDkIXzV3%2Byh6Onu6ZnZ2Zm1u1Qa9fb0VFfdOvfcW7du9%2B59NpWpbKDsTSKYygSsqUzAmsoErKlMZQLWVDZeHj3%2B67vvf/DSjZcODy/%2B9N5bH374O85MwJrKWpACT/vPfe%2B5Z5/97re/8cxXv/L1rz3Dwc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k9/9vUjpHna9ovJatxCnXRlfn6WCvT3y06KS2MMTJ8WrnADJmRqt0WgWp/eajj5WmIuYnSyklN6qPlbhC8e7MEI1zd9oXAV7g9NfVn1jpCT9Jn%2Bkk1ciccSwfIm2FsmLic/DJ3/9JzQUcO941WYFf1c32LTWSXG%2B4QR/rwQcfRfKP//TJoz885jjIZuCZMpCEKLQwHMi%2BfVUuoBZgpRO0wgTQHE0MTyRTwj1UC7tIz2yEA8/U8lrTqNMglQkGHAqCT44Ljf/yHaXDBc5WLeu%2B%2B7bmtXVmeLZkLJPJ9F12/JWFyZrMI0wLF%2Ba13BR%2BJhdBCUFORlelTzVWftlbKHd/Xxjr3n0xvlD0T3zx8cPkbox/GHpry9iT51zM7dloEFHkTeGG1UNZeQ7Ct07sbG/zTpe8nOgMgroTsFYpeSWOby7Z8ZCvGbnZK5yAtdPAmqV9jk5N22LxscRkZpfHIauHhH0v5gSsXSlmUJ2L7Skjunn/Vt447MtRzKKegDWVVTzCkpJ1eDGvfjRb32f4NmTKJ2A9FcWnhvKWnrwrS4RtYh99AtZTUcqLSU8eAa/fpy17TYw1ldWtoW5W3ouc9yX3vZRlAtZUFhcfi8q/PMlrz/2/hxOwprKWNfTB2sTfh9%2BBPQFrKmOtoU/x5%2BVQG32qYgLW00Va/vcb/z3nRu81AevpKr5tYPi9EhOwprK7ZQLWVCZgTeX8lP8CuDenorrKqfIAAAAASUVORK5CYII%3D">
</div>':
require_once('dompdf/dompdf_config.inc.php');
$dompdf = new DOMPDF ();
$dompdf->load_html ($html);
$dompdf->render ();
$dompdf->stream ("Test.pdf", array ("Attachment" => false));
Get the image from php in base64:
//getIMG.php?data=1&id=123
if ($data == 1) {
echo "data:image/".$adj->type.";base64,"
.str_replace ("\n", "", base64_encode ($adj->imageBinary));
}