aspose file tools*
The moose likes Other Open Source Projects and the fly likes Rendering image in xml with Flying saucer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Products » Other Open Source Projects
Bookmark "Rendering image in xml with Flying saucer" Watch "Rendering image in xml with Flying saucer" New topic
Author

Rendering image in xml with Flying saucer

Suzi Cooper
Ranch Hand

Joined: Jul 23, 2005
Posts: 45
Hi Folks,

I am trying to convert a html doc to pdf using the flying saucer project. I am converting the htm to an xml/css. My question is how to i specify images(paths) in my xml so they are rendered out correctly in my pdf?

Any suggestions/ideas?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42280
    
  64
I have a couple demos (from the article "Generating PDFs for Fun and Profit with Flying Saucer and iText") that use regular <img> tags, with the image being in the same directory as the XHTML file. Is your setup different, or doesn't it work that way?


Ping & DNS - my free Android networking tools app
Suzi Cooper
Ranch Hand

Joined: Jul 23, 2005
Posts: 45
I have written a demo xml for testing purposes. Here is the code snippet:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="weather.css" type="text/css" media="all" ?>
<regions>
<editableRegion>
<title>Title</title>
<description>Your TRS</description>
</editableRegion>
<editableRegion>
<title>Info text</title>
<description>balh blah blah</description>
</editableRegion>
<editableRegion>
<title>Signature</title>
<description>ABCD</description>
</editableRegion>
<img>\rss.jpg</img>
</regions>

The image(rss.jpg) is located in local drive C: (same as my css). The css is rendered fine but i get FielNotFoundException for the image:

java.io.FileNotFoundException: C:\ (The system cannot find the path specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:106)
at org.jboss.net.protocol.file.FileURLConnection.getInputStream(FileURLConnection.java:105)
at java.net.URL.openStream(URL.java:1007)
at org.xhtmlrenderer.swing.NaiveUserAgent.getInputStream(NaiveUserAgent.java:74)
at org.xhtmlrenderer.pdf.ITextUserAgent.getImageResource(ITextUserAgent.java:62)
at org.xhtmlrenderer.pdf.ITextReplacedElementFactory.createReplacedElement(ITextReplacedElementFactory.java:40)
at org.xhtmlrenderer.render.BlockBox.calcDimensions(BlockBox.java:696)
at org.xhtmlrenderer.render.BlockBox.calcDimensions(BlockBox.java:650)
at org.xhtmlrenderer.render.BlockBox.layout(BlockBox.java:771)
at org.xhtmlrenderer.render.BlockBox.layout(BlockBox.java:741)
at org.xhtmlrenderer.layout.BlockBoxing.layoutBlockChild(BlockBoxing.java:259)
at org.xhtmlrenderer.layout.BlockBoxing.layoutContent(BlockBoxing.java:80)
at org.xhtmlrenderer.render.BlockBox.layoutChildren(BlockBox.java:930)
at org.xhtmlrenderer.render.BlockBox.layout(BlockBox.java:810)
at org.xhtmlrenderer.render.BlockBox.layout(BlockBox.java:741)
at org.xhtmlrenderer.pdf.ITextRenderer.layout(ITextRenderer.java:139)
at com.bdr.emp.ERP.portals.actions.PDFTestAction.execute(PDFTestAction.java:148)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:421)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:226)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1164)
at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:397)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at com.bdr.emp.web.servlet.ActionURLFilter.doFilter(ActionURLFilter.java:68)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at com.bdr.emp.web.servlet.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:130)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:179)
at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:84)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:157)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:262)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:446)
at java.lang.Thread.run(Thread.java:595)
[ August 08, 2008: Message edited by: Suzi Cooper ]
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42280
    
  64
Hm, there's no server involved in this, so the notion of absolute paths doesn't make much sense. Try it without the backslash in the image path.
Suzi Cooper
Ranch Hand

Joined: Jul 23, 2005
Posts: 45
I have tried that as well but i still get the same error.
Any other suggestions?

Could you post a code sample/snippet ?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42280
    
  64
D'oh! The error message says "C:\" because there's no src attribute for the image. Try something like


The article I was referring to is at http://today.java.net/lpt/a/399
Suzi Cooper
Ranch Hand

Joined: Jul 23, 2005
Posts: 45
Thanks a lot! It worked.
Suzi Cooper
Ranch Hand

Joined: Jul 23, 2005
Posts: 45
How can you set the dpi for images.By default it uses,96 dpi.Is there any way of changing it?
Suzi Cooper
Ranch Hand

Joined: Jul 23, 2005
Posts: 45
Anyone any pointers/suggestions?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42280
    
  64
Search for "96 dpi" in https://xhtmlrenderer.dev.java.net/r7/users-guide-r7.html; that might help.
[ August 08, 2008: Message edited by: Ulf Dittmer ]
Suzi Cooper
Ranch Hand

Joined: Jul 23, 2005
Posts: 45
I looked up the source code and it is the iTextRenderer class that sets the following two constants DEFAULT_DOTS_PER_POINT, DEFAULT_DOTS_PER_PIXEL as constants. These two defaults combine to produce an effective resolution of 96 px to the inch.

What i can't figure out is how to override it ?
Should it be specified in the xml/ css?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42280
    
  64
What are you trying to achieve by setting the DPI? Setting width and height lets you scale the image. As the page I linked to says, the library doesn't change any settings.
Suzi Cooper
Ranch Hand

Joined: Jul 23, 2005
Posts: 45
It was a requirement for high- resolution images to be generated in the pdf. I worked around by (at least for the time being till i figure outa better solution) calling a different constructor for iTextRenderer class.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Rendering image in xml with Flying saucer