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