0

Converting html page to pdf. When i convert the page to pdf, html page content is not fit in A4 page. I want whole html page to be converted as pdf without any content missing.

I am already having code that converts html to pdf

            String tempOutFileNameXHTML = "/Users/Common/index" + i + DOT + "xhtml";
            String tempOutFileNamePDF = "/Users/Common/index" + i + DOT + PDF;
            File targetFile = targetFiles.get(i); // gets all the specified html files
            CleanerProperties props = new CleanerProperties();
            props.setTranslateSpecialEntities(true);
            props.setTransResCharsToNCR(true);
            props.setOmitComments(true);

            //checking of starting and ending tags are in proper
            HtmlCleaner htmlCleaner = new HtmlCleaner(props);
            TagNode tagNode = htmlCleaner.clean(targetFile);
            PrettyXmlSerializer prettyXmlSerializer = new PrettyXmlSerializer(props);

            prettyXmlSerializer.writeToFile(tagNode, tempOutFileNameXHTML, "utf-8");

            File xhtmlpath = new File(tempOutFileNameXHTML);
            File pdfPath = new File(tempOutFileNamePDF);
            com.itextpdf.text.Document pdfDocument = null;
            PdfWriter pdfWriter = null;
            pdfDocument = new com.itextpdf.text.Document(com.itextpdf.text.PageSize.A4); // TODO handle this

            pdfWriter = PdfWriter.getInstance(pdfDocument, new FileOutputStream(pdfPath));
            pdfDocument.open();
            Font catFont = new Font(Font.FontFamily.TIMES_ROMAN, 25, Font.BOLD);
            pdfDocument.add(new Paragraph("Target : " + targetFile.getParentFile().getName(), catFont));

            XMLWorkerHelper.getInstance().parseXHtml(pdfWriter, (com.itextpdf.text.Document) pdfDocument,
                    new FileInputStream(xhtmlpath), null);

            pdfDocument.close();

The issue here is when the html page width is too long, it is not fit in A4 size pdf. Is there anyway to shrink or to make it as pdf without any content lose. ? How can i do that ? Html page can be shrinked and converted to pdf ? Any idea on how to do this ?

Muthu
  • 1,550
  • 10
  • 36
  • 62
  • have a look at this post. Might help http://stackoverflow.com/questions/14089883/how-to-create-several-pages-with-dompdf/14089936#14089936 – Michel Feldheim Jan 11 '13 at 12:50

2 Answers2

0

"The Flying Saucer XHTML renderer project has support for outputting XHTML to PDF. Have a look at an example here."

> SOURCE <

Community
  • 1
  • 1
Georgian
  • 8,795
  • 8
  • 46
  • 87
0

iText probably supports CSS styling. You could use stylesheets to format your HTML page so that it would fit one page. Some information about style sheets and iText: http://itextpdf.com/examples/iia.php?id=56

As mentioned, you could also try Flying Saucer which is another HTML to PDF converter. At least with Flying Saucer you can provide real style sheets and the CSS support is pretty good.

Juha Palomäki
  • 26,385
  • 2
  • 38
  • 43