I have a custom Magento 2 Invoice PDF template. I manage to get all of the order/invoice info including the product image url but as soon as this is included in the img tag it is not displayed.

Retrieve the product thumb url:

$_objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$imageHelper  = $_objectManager->get('\Magento\Catalog\Helper\Image');
$image_url = $imageHelper->init($product, 'product_thumbnail_image')->setImageFile($product->getFile())->resize(40, 40)->getUrl();

Output image:

<img src="<?php echo $image_url; ?>" width="40" height="40" />

Doesn't display:
enter image description here

However, if I simply echo the thubmnail url without the img tag, the url is correct:

<?php echo $image_url; ?>

Shows correct URLS:
enter image description here

Why does this url not display when it is placed within an img tag?

Thank you.

2 Answers 2


check these things,

  • Check the image dimensions: Ensure that the image file specified by the $image_url has dimensions that are suitable for the tag's width and height attributes. If the image is too large or too small, it may not be visible within the specified dimensions.
  • Verify the image format: Confirm that the image file format matches what the tag expects. Typically, the supported image formats are JPEG, PNG, and GIF. If the image is in an unsupported format, it may not be displayed


$imageHelper = \Magento\Framework\App\ObjectManager::getInstance()->get('\Magento\Catalog\Helper\Image');
$image = $imageHelper->init($product, 'product_thumbnail_image')->setImageFile($product->getFile())->resize(40, 40);

$mediaDirectory = $imageHelper->getMediaDirectory();
$imagePath = $image->getPath();

$imageData = base64_encode(file_get_contents($mediaDirectory->getAbsolutePath($imagePath)));

echo '<img src="data:' . $image->getMimeType() . ';base64,' . $imageData . '" width="40" height="40" />';

you can try is to include the base64-encoded image data directly within the tag.

  • I have tried removing the resize from the image retrieval and the height and width tags from the img. This makes the image retrieved larger but makes no difference to it being displayed. The files are jpgs. May 31 at 12:15
  • Check the updated answer i hope it works. May 31 at 12:38
  • Error: Call to undefined method Magento\Catalog\Helper\Image::getMediaDirectory() May 31 at 12:43
  • try the updated one May 31 at 13:10
  • Unfortunately it's the same: Error: Call to undefined method Magento\Catalog\Helper\Image::getMediaDirectory() May 31 at 13:13

I figured out what was going wrong here. I was testing this on a staging server without strict https.

On my live server the image url retrieved was being retrieved via http:// rather than secure https://

Getting the product thumbnail:

// Get Product by ID
$productId = $item->getProductId();
$product = $objectManager->get('Magento\Catalog\Model\Product')->load($productId);
// Get Thumbnail Code
$imageHelper  = $objectManager->get('\Magento\Catalog\Helper\Image');
$imageurl = $imageHelper->init($product, 'product_thumbnail_image')->setImageFile($product->getFile())->resize(40, 40)->getUrl();

This outputs the thumbnail with the http://, for example: http://example.com/media/catalog/product/cache/thumbnail.jpg

The PDF does not generate an image correctly without a secure url (https://).

I wasn't sure the best way to go about this but used str_replace to convert the url from http:// to https://

$imageurl = str_replace('http://', 'https://', $imageurl);

Then I call the variable in an img tag as required:

<img src="<?php echo $imageurl; ?>" width="40" height="40" />

If anyone has a better way of achieving this by getting the url with https rather than having to use str_replace then feel free to add.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.