Skip to content

Conversation

@davidsev
Copy link
Contributor

Other image formats already work with data uri's, but jpeg's don't as cpdf->addJpegFromFile uses file_exists.
This change adds support for data uri's to addJpegFromFile.

Other image formats already work with data uri's, but jpeg's don't as
cpdf->addJpegFromFile uses file_exists.
This change adds support for data uri's to addJpegFromFile.
@bsweeney
Copy link
Member

Data URIs are handled by the Dompdf image caching logic, not the canvas library.

The following seems to work fine:

<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
</head>
<body>
  <img src="data:img/jpeg;base64,/9j/4AAQSkZJRgABAQEAYABgAAD//gA7Q1JFQVRPUjogZ2QtanBlZyB2MS4wICh1c2luZyBJSkcgSlBFRyB2ODApLCBxdWFsaXR5ID0gNjUK/9sAQwALCAgKCAcLCgkKDQwLDREcEhEPDxEiGRoUHCkkKyooJCcnLTJANy0wPTAnJzhMOT1DRUhJSCs2T1VORlRAR0hF/9sAQwEMDQ0RDxEhEhIhRS4nLkVFRUVFRUVFRUVFRUVFRUVFRUVFRUVFRUVFRUVFRUVFRUVFRUVFRUVFRUVFRUVFRUVF/8AAEQgACgAKAwEiAAIRAQMRAf/EAB8AAAEFAQEBAQEBAAAAAAAAAAABAgMEBQYHCAkKC//EALUQAAIBAwMCBAMFBQQEAAABfQECAwAEEQUSITFBBhNRYQcicRQygZGhCCNCscEVUtHwJDNicoIJChYXGBkaJSYnKCkqNDU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6g4SFhoeIiYqSk5SVlpeYmZqio6Slpqeoqaqys7S1tre4ubrCw8TFxsfIycrS09TV1tfY2drh4uPk5ebn6Onq8fLz9PX29/j5+v/EAB8BAAMBAQEBAQEBAQEAAAAAAAABAgMEBQYHCAkKC//EALURAAIBAgQEAwQHBQQEAAECdwABAgMRBAUhMQYSQVEHYXETIjKBCBRCkaGxwQkjM1LwFWJy0QoWJDThJfEXGBkaJicoKSo1Njc4OTpDREVGR0hJSlNUVVZXWFlaY2RlZmdoaWpzdHV2d3h5eoKDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uLj5OXm5+jp6vLz9PX29/j5+v/aAAwDAQACEQMRAD8Ak0ifUoJY4b24DSK/+rVyNox7cfhXUi6hAALjP1rh42b+2Sdxz57Dr9a0JCfMfnuaSd9ypKx//9k=" />
</body>
</html>

@davidsev
Copy link
Contributor Author

That works for images in HTML, but it doesn't work for embedded php or callbacks.

For example, the following code doesn't work, but works fine with a png or bmp:

$dompdf->setCallbacks([[
    'event' => 'end_page_render',
	'f' => function ($info) {
		$canvas = $info['canvas'];
		$canvas->image('data:image/jpeg;base64,'.base64_encode(file_get_contents('tests/_files/jamaica.jpg')),20,80,100,100);
		$canvas->image('tests/_files/jamaica.jpg',20,200,100,100);
    }
]]);

The difference in behaviour between image formats suggests that this isn't intentional behaviour, and the wiki also implies that this should work.

Maybe a better solution would be to update the CPDF adapter to use the same image caching as the HTML uses?

@bsweeney bsweeney added this to the 1.2.1 milestone Feb 17, 2022
@bsweeney
Copy link
Member

Thanks for the additional info. I'll take another look at the change.

@bsweeney bsweeney modified the milestones: 1.2.1, 2.0.1 Mar 16, 2022
@bsweeney bsweeney modified the milestones: 2.0.1, 2.0.2 Aug 25, 2022
@bsweeney bsweeney modified the milestones: 2.0.2, 2.0.3 Jan 29, 2023
@bsweeney bsweeney modified the milestones: 2.0.4, 2.0.5 Jun 2, 2023
@bsweeney bsweeney modified the milestones: 3.0.1, 3.1.0 Jun 23, 2024
@bsweeney
Copy link
Member

bsweeney commented Jan 6, 2025

the following code doesn't work, but works fine with a png or bmp.

You mean using a data-URI for a PNG or BMP adds the image to the PDF? That is entirely unintentional. I, honestly, had no idea that file_get_contents could read data-URIs! Which is completely silly since it is listed as a supported wrapper 😞.

Maybe a better solution would be to update the CPDF adapter to use the same image caching as the HTML uses?

Not sure I would go quite that far. The Cpdf backend will be extracted from Dompdf into a separate project at some point.

Comment on lines +6233 to +6235
if(substr($img, 0, 5) == 'data:') {
$filename = 'data-'.hash('md4',$img);
}
Copy link
Member

@bsweeney bsweeney Jan 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now wondering if we should apply this same logic elsewhere where the filename is used as a caching key since data-URIs can be quite large.

@bsweeney bsweeney merged commit dded277 into dompdf:master Jan 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants