Search code examples
pdf-generationdpiflying-saucer

Flying Saucer: set custom DPI for output PDF


I'm using Flying Saucer for HTML to PDF conversion. I need to produce an output PDF with 600dpi in Letter size. How can I achieve this?


Solution

  • You can set the letter size with the CSS page size property in your HTML document:

       @page {
          size: letter;
        }
    

    You can change the dpi of the document using the following ITextRenderer constructor :

    public ITextRenderer(float dotsPerPoint, int dotsPerPixel)
    

    I don't understand what those values really represent, but default values are dotsPerPoint = 20f * 4f / 3f and dotsPerPixel = 20, and it will output a 96dpi document.

    To get 600dpi, you can use dotsPerPoint = 500f / 3f and dotsPerPixel = 20.

    Looking at the code of ITextRenderer, the final dpi is given by the formula: dpi = dotsPerPoint * 72 / dotsPerPixel.