Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rendering PDF document using img tag with Base64 content #829 #79

Open
sonnb opened this issue Dec 3, 2024 · 4 comments
Open

Rendering PDF document using img tag with Base64 content #829 #79

sonnb opened this issue Dec 3, 2024 · 4 comments

Comments

@sonnb
Copy link

sonnb commented Dec 3, 2024

The original issue from the original repository so someone can pickup and implement later: danfickle#829

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!

@siegelzc
Copy link
Member

siegelzc commented Dec 4, 2024

I don't know how Daniel got the image working given a PDF file. For the data url, openhtmltopdf uses the javax.imageio package to decode your image data. We only support data types which are supported by default by this package. A data type is only supported if it returns true from javax.imageio.spi.ImageReaderSpi # canDecodeInput.

Here's a glimpse at what is supported: (inheritors of javax.imageio.spi.ImageReaderSpi)
Screenshot 2024-12-03 at 11 02 04 PM

If you really need this functionality, ImageIO has a plugin system designed to add codec capabilities for other data formats, and it looks like they can be injected into its internal registry via a scan of the classpath. See javax.imageio.spi.IIORegistry # registerApplicationClasspathSpis.

@syjer
Copy link

syjer commented Dec 12, 2024

hi @sonnb , currently you can only render pdf file as external resource of an img element (and the file name must end with ".pdf"), see

for the special case handling of pdf as img.

In theory it should be quite easy to add base64 support for pdf too (tbh, this section of the code should be cleaned up and generalized).

@syjer
Copy link

syjer commented Dec 14, 2024

@sonnb , I've opened a PR (#81) as the changes were not intrusive.

@sonnb
Copy link
Author

sonnb commented Dec 18, 2024

Thanks @siegelzc and @syjer for the explanation and implementation.

siegelzc pushed a commit that referenced this issue Jan 18, 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

No branches or pull requests

3 participants