Render embedded image in PDF using Flying-Saucer from html

14,599

Solution 1

Yes, you can use the approach given here: Render image from servlet in flyingsaucer generated pdf

Where Edd has:

        InputStream input = null;
        try {
            input = ...;
            byte[] bytes = IOUtils.toByteArray(input);
            Image image = Image.getInstance(bytes);

In Edd's case the image is coming from a remote source (he skips over that bit with input = ...;). In your case you just want to read it from your Base64 encoded data (the text after the base64,. First use a Base64 decoder to get the binary data, into a byte[] or Stream, you can then use Java ImageIO to create the image from your bytes and follow Edd's approach to get the image into the PDF. Kudos to Edd here (upvote for sure!).

Solution 2

Flying-Saucer supports the data: protocol natively. All you have to do is register a protocol handler:

-Djava.protocol.handler.pkgs=org.xhtmlrenderer.protocols

No need for servlets whatsoverver.

Share:
14,599
It Grunt
Author by

It Grunt

Medium-size Human Male, 43 years old Level 3 Software Engineer (Smart Hero Class) Alignment: Lawful Good Hit Points: 25 Initiative: +4 Abilities: Str 10, Dex 15, Con 10, Int 17, Wis 13, Cha 12 Feats: Athletic, Creative, Educated, Exploit Weakness, Bar Sports Proficiency Skills: Computer Use +12, Drive+7, Navigate+5, Search+8, Profession (White Collar+5), Peform (Guitar, Piano) +5, Read/Write/Speak Language (Korean)+5, Knowledge (General) +10, Coding (C#, Java, ExtJS) +8 Equipment: Basic clothes, backpack, keyring, laptop, Android device, Character Background: Jon is a full-time wage slave who spends his spare time thinking about world peace and helping others make it a better place. He attended The Ohio State University, got laid off and finished his education at American Intercontinental University in Atlanta, Ga. He enjoys cooking, meeting new people and helping others..

Updated on July 18, 2022

Comments

  • It Grunt
    It Grunt almost 2 years

    I have an xhtml document that I'm turning into a PDF using flyingsaucer. The xhtml has several tags that have a base64 encoded images inline. The source of the xhtml is dynamic so the structure of where the image tags are can vary. This is a sample of what the tag looks like:

    <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAagAAAEuCAYAAADbW4YFAAAgAElEQVR4Aex9CYBdRZ ...
    

    When I look at the html in a browser, the image appears correctly, however, the img element doesn't get rendered in the final PDF. Here is how I'm rendering it out to create the PDF.

    ITextRenderer renderer = new ITextRenderer();
    renderer.setDocument(builder.parse(source), "");
    renderer.layout();
    renderer.createPDF(response.getOutputStream(),true);
    

    Can anyone let me know what approach I should take to accomplish this? I saw this posting, however, I'm using inline images so I can't see how I can accomplish this using Edd's solution.

    Thanks in advance

  • It Grunt
    It Grunt almost 12 years
    Do you have a link for how protocol handlers are registered?