0

I am trying to convert html file into pdf using iText lib(4.2.0). But the problem is it's not printing all the html content to pdf, its only partially printing some data. Here is the code to convert html to pdf.

InputStream il = new FileInputStream("/tmp/test.html");

      // step 1
      Document document = new Document();

      // step 2
      PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("/tmp/pdf.pdf"));

      writer.setInitialLeading(12.5f);

      // step 3
      document.open();

      HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);

      htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());

      // CSS
      CSSResolver cssResolver = new StyleAttrCSSResolver();
      InputStream csspathtest =new FileInputStream("/tmp/test.css");
      CssFile cssfiletest = XMLWorkerHelper.getCSS(csspathtest);
      cssResolver.addCss(cssfiletest);

      Pipeline<?> pipeline = new CssResolverPipeline(cssResolver,
                                                     new HtmlPipeline(htmlContext,
                                                                      new PdfWriterPipeline(
                                                                          document, writer)));

      XMLWorker worker = new XMLWorker(pipeline, true);
      XMLParser p = new XMLParser(worker);
      p.parse(il);

      // step
      document.close();

Here is the sample html file http://codepaste.net/65kmhp

pradex
  • 328
  • 4
  • 18
  • Are you sure there has been a `XMLWorker` for iText 4.2.0? – mkl Feb 24 '16 at 21:21
  • iText 4.2.0 was never released by iText Software and will never be endorsed or supported. The last official release with weak copyleft was 2.1.7. XMLWorker didn't exist until 5.x.x. To be able to use XMLWorker, your version of iText and XMLWorker need to be identical. – Amedee Van Gasse Feb 25 '16 at 07:59
  • Oh then i guess i should look for alternatives as i want to do it using open source apis only. I think i would try PdfBox but please do help me with any other library that i can use. :) Thanks – pradex Feb 25 '16 at 11:58
  • 1
    iText 5.x is still [open source](http://itextpdf.com/agpl). I'm pretty sure you mean that you don't like the license change from MPL/LPGL to AGPL. You should probably [read this still](http://developers.itextpdf.com/question/can-itext-217-or-earlier-be-used-commercially). – Chris Haas Feb 25 '16 at 19:25
  • So that means i can use it in my project internally with giving due credit? I am not going to sell my tool or outsource its pdf creation services, i am only going to use it as sub-process to reach to the final result i need. Also i do not plan to make my project opensource. – pradex Feb 26 '16 at 12:06
  • You only want to use Open Source, but you do not want to make your own project Open Source. Is that what you are saying? To answer your non-technical question: contact iText sales (sales@itextpdf.com) and describe your situation in detail. They will tell you if you need to purchase a license that releases you from the Open Source restrictions. – Amedee Van Gasse May 25 '16 at 12:35

0 Answers0