3

How to set PDF page size A4 when we use ITextRenderer to generate PDF from thymeleaf HTML template ?

I have generated PDF but page size is not proper, how to set page size A4 ITextRenderer library in JAVA

    ClassLoaderTemplateResolver templateResolver = new 
    ClassLoaderTemplateResolver();
    templateResolver.setSuffix(".html");
    templateResolver.setTemplateMode("HTML5");

    TemplateEngine templateEngine = new TemplateEngine();
    templateEngine.setTemplateResolver(templateResolver);

    Context context = new Context();
    context.setVariable("name", "Thomas");

    String html = templateEngine.process("templates/Quote", context);

    OutputStream outputStream = new FileOutputStream("message.pdf");
    ITextRenderer renderer = new ITextRenderer();
    renderer.setDocumentFromString(html);
    renderer.layout();
    renderer.createPDF(outputStream,true);
    outputStream.close();
Rishikesh Dhokare
  • 3,559
  • 23
  • 34
Ketan Navadiya
  • 279
  • 3
  • 6
  • 18
  • 1
    You mention `ITextRenderer`. This means that you are using Flying Saucer, a 3rd party product that uses an obsolete version of iText [that should no longer be used in production](https://developers.itextpdf.com/question/versions-older-than-5) and that most certainly doesn't use iText 7 (if it does, it's doing something illegal). If you want to use iText 7, read [Converting HTML to PDF](https://stackoverflow.com/questions/47895935/converting-html-to-pdf-using-itext); also read [this answer](https://stackoverflow.com/questions/48018716/itextsharp-pdf-resize-and-data-alignment/48021729#48021729). – Bruno Lowagie Jan 24 '18 at 12:29

2 Answers2

6

Please be aware that you are using FlyingSaucer, not iText. FlyingSaucer is a product that internally uses (a very old version of) iText.

You are immediately cutting yourself off from 10+ years of bugfixes and developments.

If you are comfortable going for just iText, the best way of solving this issue is with pdfHTML.

It's an add-on we wrote to the iText7 core library that is specifically designed to convert HTML into PDF.

Simple example:

    File src = new File("source_html_file.html");
    File dest = new File("target_pdf_file.pdf");

    PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
    Document doc = new Document(pdf, PageSize.A4);
    InputStream stream = new FileInputStream(src);

    ConverterProperties converterProperties = new ConverterProperties();
    FontProvider dfp = new DefaultFontProvider(true, true, true);
    converterProperties.setFontProvider(dfp);

    HtmlConverter.convertToPdf(stream, pdf, converterProperties);

Check out the tutorials online for more information https://developers.itextpdf.com/content/itext-7-examples/itext-7-converting-html-pdf

Joris Schellekens
  • 8,483
  • 2
  • 23
  • 54
  • Note that defining `PageSize.A4` the way you do in your code can be ignored if there's an `@page` definition of the page size on the CSS level. – Bruno Lowagie Jan 24 '18 at 12:58
  • Very true. In order to be completely sure that any incoming HTML and CSS is ok, you can parse it with JSoup however, and remove those conflicting properties. – Joris Schellekens Jan 24 '18 at 12:59
1

To fix this issue using jsoup, xhtmlrenderer from flying-saucer-pdf-openpdf just set this in your html-document:

@page {
    size: A4;
}
javabeangrinder
  • 6,939
  • 6
  • 35
  • 38