0

Possible Duplicate:
Converting HTML files to PDF

I have HTML in java string and i want to render it as a PDF. Is there any API or reference Available for free?

Community
  • 1
  • 1
TaherT
  • 1,285
  • 2
  • 22
  • 41
  • This post in SO will be your answer [iText html to pdf ] [1]:http://stackoverflow.com/questions/235851/using-itext-to-convert-html-to-pdf – Tito May 02 '12 at 13:29

2 Answers2

3

Take a look at Flying Saucer. It is CSS 2.1 compliant, it even covers some CSS3 features, and it uses iText under the hood. I've found it quite useful to convert XHTML to PDF.

Xavi López
  • 27,550
  • 11
  • 97
  • 161
  • I am getting the following error : org.apache.jasper.JasperException: org.xhtmlrenderer.util.XRRuntimeException: Can't load the XML resource (using TRaX transformer). org.xml.sax.SAXParseException: The declaration for the entity "HTML.Version" must end with '>'. – TaherT May 03 '12 at 05:22
  • Take into account that Flying Saucer is an [XHTML](http://en.wikipedia.org/wiki/XHTML) renderer. Make sure your input complies the [XHTML specs](http://www.w3.org/TR/xhtml1/). Summarising, make it look like an XML (close every tag, like
    and such).
    – Xavi López May 03 '12 at 06:48
  • That I have taken care but now I am getting NullpointerException in INFO: Class : org.xhtmlrenderer.pdf.ITextRenderer INFO: Method : createPDF INFO: LineNumber : 288 – TaherT May 03 '12 at 07:17
  • Can you tell us what's on line 288 of `org.xhtmlrenderer.pdf.ITextRenderer` ? Or which Flying Saucer version are you using? And maybe how are you calling the library ? (maybe it's time to start a new question) – Xavi López May 03 '12 at 07:47
  • If you're using R8, there's probably some problem with the document you're trying to convert. Maybe `_root` is null [here](http://grepcode.com/file/repo1.maven.org/maven2/org.xhtmlrenderer/core-renderer/R8/org/xhtmlrenderer/pdf/ITextRenderer.java/#288) ? – Xavi López May 03 '12 at 08:02
  • yes I am using R8. I don't know what is going null... The raw class is here http://grepcode.com/file_/repo1.maven.org/maven2/org.xhtmlrenderer/core-renderer/R8pre2/org/xhtmlrenderer/pdf/ITextRenderer.java/?v=source – TaherT May 03 '12 at 08:23
  • yes _root was going null one method need to be called renderer.layout(); which instantiates _root :) – TaherT May 04 '12 at 08:15
0

Depending on the complexity of your HTML code, you could give a try to iText's XMLWorker.

The project :

http://sourceforge.net/projects/xmlworker/

The demo (paste your HTML there to check what the generated PDF will look like) :

http://demo.itextsupport.com/xmlworker/

The documentation :

http://demo.itextsupport.com/xmlworker/itextdoc/index.html

Alexis Pigeon
  • 7,423
  • 11
  • 39
  • 44