openhtmltopdf icon indicating copy to clipboard operation
openhtmltopdf copied to clipboard

Rendering PDF document using `img` tag with Base64 content

Open jfreedman-eagent opened this issue 3 years ago • 5 comments

I want to display PDFs and images inline with the PDF I'm generating, but I only have the raw data for each file (and their associated MIME type). For images, I can do this with:

<img 
   src="data:image/png;base64,{base64 encoded data here}"
   width="500" 
   height="500"
/>

However, when I do the same thing with a PDF (following the guidance in this issue), the PDF can't be resolved.

This is the HTML that I used:

<img 
   src="data:@file/pdf;base64,{base64 encoded data here}"
   page="1"
   width="500" 
   height="500"
/>

And the error I get is:

com.openhtmltopdf.load WARNING:: Unrecognized image format for: data:@file/pdf;base64,{base64 encoded data here} => java.io.IOException:: Unrecognized Image format

Is this expected behavior, or is this a bug with how PDFs are handled via the img tag?

Thanks!

jfreedman-eagent avatar Apr 20 '22 20:04 jfreedman-eagent

hi @jfreedman-eagent as far as I remember and looking at the source of the error ( https://github.com/danfickle/openhtmltopdf/blob/open-dev-v1/openhtmltopdf-pdfbox/src/main/java/com/openhtmltopdf/pdfboxout/PdfBoxImage.java#L33 ) , the base64 images are handled through ImageIO.

So we may need to add a special case for data:file/pdf;base64 encoded data.

syjer avatar Apr 27 '22 21:04 syjer

Thanks for responding! Is this something that would be worth creating a PR for? If so, I can take a stab at implementing it

jfreedman-eagent avatar Apr 28 '22 01:04 jfreedman-eagent

Is this something that would be worth creating a PR for

Yes, as it's quite a coherent request, if linking a img via the file protocol work, it should also for data: base64 :)

syjer avatar Apr 28 '22 06:04 syjer

hi, @jfreedman-eagent Do you still intend to make a PR? We need this function too, I am thinking about implementing it myself, but I would hate to duplicate efforts

Dunemaster avatar Jun 06 '22 13:06 Dunemaster

Gah, sorry @Dunemaster. I haven't started working on this, so feel free to go for it.

I meant to pick it up, but other stuff came up since then 😓

jfreedman-eagent avatar Jun 06 '22 13:06 jfreedman-eagent