Discussions

Web tier: servlets, JSP, Web frameworks: Converting an HTML Page to PDF using iText

  1. Converting an HTML Page to PDF using iText (10 messages)

    Hi,
    I am new to using iText. We are developing a Web application using Struts. We have a requirement of converting a complete HTML page to PDF. The way we want to do it is provide a Button called "Generate PDF" and when the user clicks on this button, the whole page should be converted to a PDF. This HTML Page has images and form fields like Text boxes, radio buttons, etc. Is there a way I can read this whole page as it is and convert to PDF using iText? Please let me know how I can achieve this. Thanks and regards, Shweta
  2. Try JodConverter JodConverter supports conversion from one format to another. It is opensource. It uses OpenOffice API and OpenOffice instance (service). Openoffice service need to run separately on windows or unix (including linux) environments. Visit http://artofsolving.com/opensource/jodconverter/guide/supportedformats
  3. i have gone through the link given by you. But it does not support HTML to PDF converter. Moreover, i was unable to find any example(source code) for the same. Can you provide me with a simple example, so that i can test the conversion? Thanks in advance. Shweta
  4. did u converted the HTML page to PDF ? if "yes" how?
  5. i have converted HTML to PDF now using the following code: <%@ page contentType="text/html;charset=windows-1252"%> <%@ page import= "com.lowagie.text.*"%> <%@ page import= "java.io.FileOutputStream"%> <%@ page import= "com.lowagie.text.pdf.PdfWriter"%> <%@ page import= "java.net.URL"%> <%@ page import= "java.net.URLConnection"%> <%@ page import= "java.io.OutputStream"%> <%@ page import= "java.io.OutputStreamWriter"%> <%@ page import= "java.io.File"%> <%@ page import= "java.io.OutputStream"%> <%@ page import= "com.lowagie.text.html.HtmlParser"%> untitled <%try{String paramStr = "func=getReport&print=true&reportID=1&selectedIDs=315,158"; // POST requests are required to have Content-Length String lengthString = String.valueOf(paramStr.length()); /* * Establish a connection to the application. */ URL url = new URL("http://10.230.213.163:8988/crm_interaction-crmint-context-root/common/CreatedPdf.jsp"); URLConnection con = url.openConnection(); /* * configure the connection to allow for the operations necessary. * 1. Turn off all caching, so that each new request/response is * made from a fresh connection with the servlet. 2. Indicate that * this client will attempt to SEND and READ data to/from the * servlet. */ con.setUseCaches(false); con.setDefaultUseCaches(false); con.setDoInput(true); con.setDoOutput(true); // Set the content type we are POSTing and length of the data. con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); con.setRequestProperty("Content-Length", lengthString); OutputStream os = con.getOutputStream(); OutputStreamWriter osw = new OutputStreamWriter(os); // send data to servlet osw.write(paramStr); osw.flush(); osw.close(); Document document = new Document(); document.setPageSize(PageSize.A4.rotate()); OutputStream out1 = response.getOutputStream(); response.addHeader("Content-Type", "application/pdf"); response.addHeader("Content-Disposition", "attachment; filename=\"ss.pdf\""); //response.addHeader("Content-Length", "" + cachedURL.length()); try { PdfWriter.getInstance(document, out1); HtmlParser.parse(document, con.getInputStream()); } catch (Exception e) { System.out.println("error="+e); e.printStackTrace(); //log.error(e); } } catch (Throwable t) { System.out.println("error="+t); t.printStackTrace(); // log.error(t); } %> Here, CreatedPdf is the jsp file which i want to convert into PDF.. It works fine if i add simple text. But if i add table tags to it it gives followinf error: 08/07/17 15:39:28 java.lang.ClassCastException: com.lowagie.text.Table 08/07/17 15:39:28 at com.lowagie.text.xml.SAXiTextHandler.handleStartingTags(Unknown Source) 08/07/17 15:39:28 at com.lowagie.text.html.SAXmyHtmlHandler.startElement(Unknown Source) 08/07/17 15:39:28 at oracle.xml.parser.v2.NonValidatingParser.parseElement(NonValidatingParser.java:1181) 08/07/17 15:39:28 at oracle.xml.parser.v2.NonValidatingParser.parseRootElement(NonValidatingParser.java:300) 08/07/17 15:39:28 at oracle.xml.parser.v2.NonValidatingParser.parseDocument(NonValidatingParser.java:267) 08/07/17 15:39:28 at oracle.xml.parser.v2.XMLParser.parse(XMLParser.java:149) 08/07/17 15:39:28 at oracle.xml.jaxp.JXSAXParser.parse(JXSAXParser.java:286) 08/07/17 15:39:28 at com.lowagie.text.html.HtmlParser.go(Unknown Source) 08/07/17 15:39:28 at com.lowagie.text.html.HtmlParser.parse(Unknown Source) 08/07/17 15:39:28 at _common._HtmltoPdf._jspService(HtmltoPdf.jsp:64) 08/07/17 15:39:28 at oracle.jsp.runtime.HttpJsp.service(HttpJsp.java:139) 08/07/17 15:39:28 at oracle.jsp.runtimev2.JspPageTable.service(JspPageTable.java:349) 08/07/17 15:39:28 at oracle.jsp.runtimev2.JspServlet.internalService(JspServlet.java:509) 08/07/17 15:39:28 at oracle.jsp.runtimev2.JspServlet.service(JspServlet.java:413) 08/07/17 15:39:28 at javax.servlet.http.HttpServlet.service(HttpServlet.java:853) 08/07/17 15:39:28 at com.evermind.server.http.ResourceFilterChain.doFilter(ResourceFilterChain.java:65) 08/07/17 15:39:28 at oracle.security.jazn.oc4j.JAZNFilter.doFilter(Unknown Source) 08/07/17 15:39:28 at com.evermind.server.http.ServletRequestDispatcher.invoke(ServletRequestDispatcher.java:604) 08/07/17 15:39:28 at com.evermind.server.http.ServletRequestDispatcher.forwardInternal(ServletRequestDispatcher.java:317) 08/07/17 15:39:28 at com.evermind.server.http.ServletRequestDispatcher.forward(ServletRequestDispatcher.java:220) 08/07/17 15:39:28 at org.apache.struts.action.RequestProcessor.doForward(RequestProcessor.java:1069) 08/07/17 15:39:28 at org.apache.struts.tiles.TilesRequestProcessor.doForward(TilesRequestProcessor.java:274) 08/07/17 15:39:28 at org.apache.struts.action.RequestProcessor.processForwardConfig(RequestProcessor.java:455) 08/07/17 15:39:28 at org.apache.struts.tiles.TilesRequestProcessor.processForwardConfig(TilesRequestProcessor.java:320) 08/07/17 15:39:28 at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:279) 08/07/17 15:39:28 at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482) 08/07/17 15:39:28 at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:507) 08/07/17 15:39:28 at javax.servlet.http.HttpServlet.service(HttpServlet.java:740) 08/07/17 15:39:28 at javax.servlet.http.HttpServlet.service(HttpServlet.java:853) 08/07/17 15:39:28 at com.evermind.server.http.ResourceFilterChain.doFilter(ResourceFilterChain.java:65) 08/07/17 15:39:28 at oracle.security.jazn.oc4j.JAZNFilter.doFilter(Unknown Source) 08/07/17 15:39:28 at com.evermind.server.http.ServletRequestDispatcher.invoke(ServletRequestDispatcher.java:604) 08/07/17 15:39:28 at com.evermind.server.http.ServletRequestDispatcher.forwardInternal(ServletRequestDispatcher.java:317) 08/07/17 15:39:28 at com.evermind.server.http.HttpRequestHandler.processRequest(HttpRequestHandler.java:790) 08/07/17 15:39:28 at com.evermind.server.http.HttpRequestHandler.run(HttpRequestHandler.java:270) 08/07/17 15:39:28 at com.evermind.server.http.HttpRequestHandler.run(HttpRequestHandler.java:112) 08/07/17 15:39:28 at com.evermind.util.ReleasableResourcePooledExecutor$MyWorker.run(ReleasableResourcePooledExecutor.java:192) 08/07/17 15:39:28 at java.lang.Thread.run(Thread.java:534)
  6. Hi , can u plz suggest me the best technology which helps me to work even in USA? rite now m working on a tool (OpenClovis IDE) which is related to java/j2ee technologies.... Everybody(Companies) are asking me in interviews about Spring , Hibernate...JBoss...along with java/j2ee & Somebody(Friends) suggested to go/learn for WebSphere MQ..some are Dataware Housing....some are Oracle Apps....Some are .Net..... probably my trip to USA on H1B would be in Feb/March 2009. Pls Suggest me....
  7. Were you able to get this working with Table tags? What do we have to do?
  8. HTMLWorker[ Go to top ]

    I could not tell if you have found a response to this based on this thread. I have used iText extensively and it supports printing HTML to PDF. The thing you want to use is called HTMLWorker. It is not an in-depth HTML parser, but it works for basic HTML. With iText, this is currently your best method; it is not heavily supported. Check out this link, I have posted some code that I used to do this: http://www.nabble.com/HTMLWorker-tables-and-fonts-td22232868.html#a22232868
  9. Converting HTML pages to PDF[ Go to top ]

      "... I have used iText extensively and it supports printing HTML to PDF..."

    But the support for HTML seems to be only partial.  I need to convert arbitary web pages into PDF documents. Is there a way to improve the quality of conversion, or an alternative library? Please don't suggest ABCpdf - it really needs to be converted natively in Java. Any recommendations would be greatly appreciated.

  10. Hope this helps http://javaxtendsolutions.wordpress.com/2011/01/03/itext-1-5-html-to-pdf/

  11. Try[ Go to top ]

    If you just need a button than I recommend you to use service like http://www.web2pdfconvert.com/pdf-button#cid7 This service also has offer for developers.