Rendering PDF document using `img` tag with Base64 content
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!
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.
Thanks for responding! Is this something that would be worth creating a PR for? If so, I can take a stab at implementing it
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 :)
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
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 😓