0

I'm using iText to convert a XHTML to PDF. I created the XHTML using a XLSX to HTML converter and cleened out. Here you can see the HTML page. The point is that i'm not able to convert it in a equivalent PDF on A4 pages. I tryed recent and older iText libraries, used ITextRenderer, XMLWorkerHelper and HTMLWorker but no one created the PDF correctly. Follow my tries.

Example with external CSS (HTML and CSS are paths):

com.itextpdf.text.Document document = new com.itextpdf.text.Document(PageSize.A4);
          PdfWriter pdfWriter = PdfWriter.getInstance
               (document, new FileOutputStream(PDF));
          document.open();
          document.addAuthor("Real Gagnon");
          document.addCreator("Real's HowTo");
          document.addSubject("Thanks for your support");
          document.addCreationDate();
          document.addTitle("Please read this");

          XMLWorkerHelper worker = XMLWorkerHelper.getInstance();

          String str = readHtml();
          worker.parseXHtml(pdfWriter, document,  new FileInputStream(HTML), new FileInputStream(CSS));
          document.close();

Example with ITextRenderer and internal CSS but since it doesn't considered the font tags i added the font programmatically:

DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        Document doc = builder.parse(new ByteArrayInputStream(readHtml().getBytes("UTF-8")));

        ITextRenderer renderer = new ITextRenderer();
        File tmpFontFile = new File(
                "C:\\Android\\workspace\\GestioneCommesse\\WebContent\\resources\\font\\arial_narrow.ttf");
        renderer.getFontResolver().addFont(tmpFontFile.getAbsolutePath(), BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
        renderer.setDocument(doc, null);

        FileOutputStream os = new FileOutputStream(PDF);
        Transformer tf = TransformerFactory.newInstance().newTransformer();
        tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        tf.setOutputProperty(OutputKeys.INDENT, "yes");
        tf.transform(new DOMSource(doc), new StreamResult(os));
        os.close();

        File file = new File("c:\\temp.pdf");
        file.createNewFile();
        OutputStream os2 = new FileOutputStream(file);
        renderer.layout();
        renderer.createPDF(os2);
        os.close();

Here you can see the two results i get. In one pdf the entire css is missing and by the other i'm not able to change widths and font style:

Example1 has been created using XmlWorkerHelper and Example2 using iTextRenderer

With THIS example i get result 2

Emaborsa
  • 2,360
  • 4
  • 28
  • 50
  • Can you show an example of the "imperfect" PDF? HTML and PDF are two completely different languages that really bear no relationship to each other and exist for very different purposes, so there's no 100% translation between the two. – Chris Haas May 23 '14 at 21:29
  • Added the result pdfs... – Emaborsa May 23 '14 at 21:53
  • Example 2 looks pretty good to me. I'd remove or reduce the margins on your document to get more space. You might also have to use a wider paper. As for breaks, iTextSharp has no idea where you'd want to break things, you'll need to give it some help there. If you can break your single giant table into multiple subtables that will help. Really, it is a total visual thing and there's no way to automate that part. – Chris Haas May 24 '14 at 13:57

2 Answers2

1

I was working on a project and I had Font problems as well. I had styled a div with brush script font on an html page and when I created a PDF the font did not show on the PDF. I noticed that by changing the quotes from single to double it worked for me.

Here is my before:

< div style="font-family : 'Brush Script MT';" >< /div >

(notice the placement of single and double quotes, this did not work) then I tried

< div style='font-family : "Brush Script MT";'>

(at first it did not work by itself)

but then I Registered the font using the FontFactory and gave it an alias with the same name.

FontFactory.RegisterDirectories();
var fontPath = Environment.GetEnvironmentVariable("SystemRoot") + "\\fonts\\BRUSHSCI.TTF";
FontFactory.Register(fontPath, "Brush Script MT");

Hope this helps

Jose Gomez
  • 11
  • 2
0

I'm working with iText in a similar project, and so far I just surrended trying to fully understand how it works :P

But in this case all you need is adding page margins in your print css. Something like:

@page {  
margin-left: 10px;  
margin-right: 10px;  
margin-top: 10px;  
margin-bottom: 10px;  
} 

I tried converting your html source with my own WebApp and it works just fine. It should work for you, too (using example #2).

A little tip: I never declare widths in pixels because that's what happens (content going out of the page because of iText's conversion). If you remove all your width="" tags, you shoud get the same result even without page margins.

Ciao!

bs_
  • 11
  • 3