0% found this document useful (0 votes)
257 views

Image Resolver

The document discusses using Flying Saucer to render an XHTML document to a PDF through a servlet. The XHTML contains an image requested from another servlet that checks who is logged in before returning the appropriate image. However, the PDF renderer's HTTP request for the image does not contain login information, so the wrong image is returned. The solution is to use a ReplacedElementFactory to provide the image directly to the renderer for the specific XHTML element, avoiding the separate image servlet request. An example ReplacedElementFactory implementation is provided that reads the desired image and returns it to the renderer.

Uploaded by

muthuselvas
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
257 views

Image Resolver

The document discusses using Flying Saucer to render an XHTML document to a PDF through a servlet. The XHTML contains an image requested from another servlet that checks who is logged in before returning the appropriate image. However, the PDF renderer's HTTP request for the image does not contain login information, so the wrong image is returned. The solution is to use a ReplacedElementFactory to provide the image directly to the renderer for the specific XHTML element, avoiding the separate image servlet request. An example ReplacedElementFactory implementation is provided that reads the desired image and returns it to the renderer.

Uploaded by

muthuselvas
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

java - Render image from servlet in flyingsaucer generated pdf - Stack Overflow

Render image from servlet in flyingsaucer generated pdf

I'm using flyingsaucer to render an xhtml document to pdf through a servlet which returns the generated pdf document. The xhtml document features an image which is requested from another servlet. The image servlet checks who is logged in before returning the appropriate image. The code below shows how the image is requested: <img height="140" width="140" src="http://localhost:8080/myapp/servlet/DisplayPic" /> My problem is that the http request for the image is from the pdf renderer and not the logged in user so the image servlet doesn't know who's logged in and therefore the desired image is not returned. I'm currently using the code below to render the xhtml document: ITextRenderer renderer = new ITextRenderer(); renderer.setDocumentFromString(xhtmlDocumentAsString); renderer.layout(); os = response.getOutputStream(); renderer.createPDF(os); I need to either maintain the user's session when the image servlet is requested or provide the renderer with the image to use for that specific xhtml element. I think the latter can be done using a ReplacedElementFactory but I haven't been able to dig out any example code that can help me.
java pdf-generation itext flying-saucer xhtmlrenderer

asked Apr 25 at 13:22 Edd 590 2 19 89% accept rate feedback

1 Answer
I've got this working very nicely now. Here's the code. In my xhtml document i have: <div class="profile_picture" style="display:block;width:140px;height:140px;" /> (I'm using a div element instead of img as the factory is only used for block level elements) I render my document using: ITextRenderer renderer = new ITextRenderer(); renderer.getSharedContext().setReplacedElementFactory(new ProfileImageReplacedElementFactory renderer.setDocumentFromString(xhtmlDocumentAsString); renderer.layout(); os = response.getOutputStream(); renderer.createPDF(os); And i have my own ReplacedElementFactory as below: public class ProfileImageReplacedElementFactory implements ReplacedElementFactory { private final ReplacedElementFactory superFactory;

1 of 2

java - Render image from servlet in flyingsaucer generated pdf - Stack Overflow

public ProfileImageReplacedElementFactory(ReplacedElementFactory superFactory) { this.superFactory = superFactory; } @Override public ReplacedElement createReplacedElement(LayoutContext layoutContext, BlockBox blockBox UserAgentCallback userAgentCallback, int cssWidth, int cssHeight) { Element element = blockBox.getElement(); if (element == null) { return null; } String nodeName = element.getNodeName(); String className = element.getAttribute("class"); if ("div".equals(nodeName) && className.contains("profile_picture")) { InputStream input = null; try { input = ...; byte[] bytes = IOUtils.toByteArray(input); Image image = Image.getInstance(bytes); FSImage fsImage = new ITextFSImage(image); if (fsImage != null) { if ((cssWidth != -1) || (cssHeight != -1)) { fsImage.scale(cssWidth, cssHeight); } return new ITextImageElement(fsImage); } } catch (IOException e) { getLogger().error(ExceptionUtils.getStackTrace(e)); } catch (BadElementException e) {
edited Jun 4 at 12:02 answered Apr 26 at 15:04 Edd 590 2 19

Will this work for an embedded image? one that looks like <img src="data:image/png; base64,iVBORw0KGgoAAAANSUhEU... ? It Grunt May 31 at 18:39 It needs to be a block level element so the simple answer is no. You can do something like <div><img/></div> and do the replacement on the div element instead of the img element Edd Jun 1 at 9:51 Sorry... I just realised that I completely misunderstood your question. I don't see any reason why that wouldn't work, you just need to read the embedded image in your ReplacedElementFactory and return it Edd Jun 1 at 9:56

feedback

Not the answer you're looking for? Browse other questions tagged java pdf-generation
itext flying-saucer xhtmlrenderer or ask your own question.

question feed

2 of 2

You might also like