For some reason, DomPDF won\'t render an image included in the html that is being parsed:
None of the solutions here worked for me. Instead I just base64 encoded the image and then it worked. You can use this tool.
I solve this problem by using external CSS's full path. This one worked on my linux ubuntu server :
<link href="{{ public_path('css/style.css') }}" />
<img src="{{ public_path('images/image.jpg') }}" />
and work on image.
In path :
vendor/dino/dompdf-module/config/module.config.php
change settings
enable_remote' => false,
то true.
Ok I had the same problem with image using :
<img id="logo" src="/images/flags/fr.png" width="50" alt="Logo">
But if I add a . before /images, without changing anything in dompdf_config.custom.inc, it works
<img id="logo" src="./images/flags/fr.png" width="50" alt="Logo">
Hope it helps
For our use case we had to convert all the images on the page to base64 since the pdf should be usable offline. Our code lives inside a controller class but you can modify that to your needs.
Here's the code:
/**
* Convert images to Base64 so it's included in the PDF.
*
* @param $html Full html render of the page.
*/
public function convertReportImagesToURI($html): string
{
$doc = new DOMDocument();
libxml_use_internal_errors(true);
$doc->loadHTML($html);
$tags = $doc->getElementsByTagName('img');
$imgArr = array();
// Iterate over all image tags.
foreach ($tags as $tag) {
// Get the src attribute.
$imgSrc = $tag->getAttribute('src');
// Convert to base64.
$base64src = self::getImageDataURI($imgSrc);
$tag->setAttribute('src', $base64src);
}
return $doc->saveHTML();
}
/**
* This does the actual encoding.
*/
public static function getImageDataURI($image, $mime = ''): string
{
// Director::absoluteURL('/') gets the base url of the site.
// We had to remove the leading slash of the image hence the use of substr.
// If your images have absolute urls you will need to modify this.
$imageLocation = Director::absoluteURL('/') . substr($image, 1);
// Get the image location. remove leading slash on image url.
return 'data:' . self::get_image_mime_type($imageLocation) . ';base64,' . base64_encode(file_get_contents($imageLocation));
}
/**
* https://stackoverflow.com/a/45054843
* @param $image_path
* @return string
*/
public static function get_image_mime_type($image_path): string
{
$mimes = array(
IMAGETYPE_GIF => "image/gif",
IMAGETYPE_JPEG => "image/jpg",
IMAGETYPE_PNG => "image/png",
IMAGETYPE_SWF => "image/swf",
IMAGETYPE_PSD => "image/psd",
IMAGETYPE_BMP => "image/bmp",
IMAGETYPE_TIFF_II => "image/tiff",
IMAGETYPE_TIFF_MM => "image/tiff",
IMAGETYPE_JPC => "image/jpc",
IMAGETYPE_JP2 => "image/jp2",
IMAGETYPE_JPX => "image/jpx",
IMAGETYPE_JB2 => "image/jb2",
IMAGETYPE_SWC => "image/swc",
IMAGETYPE_IFF => "image/iff",
IMAGETYPE_WBMP => "image/wbmp",
IMAGETYPE_XBM => "image/xbm",
IMAGETYPE_ICO => "image/ico");
if (($image_type = exif_imagetype($image_path))
&& (array_key_exists($image_type, $mimes))) {
return $mimes[$image_type];
} else {
return '';
}
}
You can use base64 encoded image
<img src="{{'data:image/png;base64,' . base64_encode(file_get_contents(@$image))}}" alt="image" >