Class WPdfRenderer


public class WPdfRenderer extends WTextRenderer
An XHTML to PDF renderer.

This class implements an XHTML to PDF renderer. The rendering engine supports only a subset of XHTML. See the documentation of WTextRenderer for more information.

The renderer renders to a PDFJet PDF document (using WPdfImage).

By default it uses a pixel resolution of 72 DPI, which is the default for a WPdfImage, but differs from the default used by most browsers (which is 96 DPI and has nothing to do with the actual screen resolution). The pixel resolution can be configured using setDpi(). Increasing the resolution has the effect of scaling down the rendering. This can be used in conjunction with WTextRenderer#setFontScale() to scale the font size differently than other content.

Font information is embedded in the PDF. Fonts supported are native PostScript fonts (Base-14) (only ASCII-7), or true type fonts (Unicode). See addFontCollection() for more information on how fonts are located.

  • Constructor Details

    • WPdfRenderer

      public WPdfRenderer(com.pdfjet.PDF pdf, com.pdfjet.Page page)
      Creates a new PDF renderer.

      The PDF renderer will render on the given pdf (starting). If the page is not null, then rendering will happen on this first page (and its page sizes will be taken into account).

      Default margins are 0, and the default DPI is 72.

    • WPdfRenderer

      public WPdfRenderer(com.pdfjet.PDF pdf)
      Creates a new PDF renderer.

      Calls this(pdf, (com.pdfjet.Page)null)

  • Method Details

    • setMargin

      public void setMargin(double margin, EnumSet<Side> sides)
      Sets the page margins.

      This sets page margins, in cm, for one or more sides.

    • setMargin

      public final void setMargin(double margin, Side side, Side... sides)
      Sets the page margins.

      Calls setMargin(margin, EnumSet.of(side, sides))

    • setMargin

      public final void setMargin(double margin)
      Sets the page margins.

      Calls setMargin(margin, Side.AllSides)

    • setDpi

      public void setDpi(int dpi)
      Sets the resolution.

      The resolution used between CSS pixels and actual page dimensions. Note that his does not have an effect on the de facto standard CSS resolution of 96 DPI that is used to convert between physical WLength units (like cm, inch and point) and pixels. Instead it has the effect of scaling down or up the rendered XHTML on the page.

      The dpi setting also affects the pageWidth(), pageHeight(), and getMargin() pixel calculations.

      The default resolution is 72 DPI.

    • addFontCollection

      public void addFontCollection(String directory, boolean recursive)
      Adds a font collection.

      If JWt has been configured to use libpango, then font matching and character selection is done by libpango, and calls to this method are ignored. See WPdfImage#addFontCollection() for more details.

      If JWt was not configured to use libpango, you will have to add the directories where JWt should look for fonts. You will also have to specify the required font in the HTML source, e.g.:

       WPdfRenderer renderer = new WPdfRenderer(pdf, page);
       // ...
       renderer.render("<p style=\"font-family: 'DejaVuSans', Arial\">élève, fenêtre, âme</p>");

      See Also:
    • addFontCollection

      public final void addFontCollection(String directory)
      Adds a font collection.

      Calls addFontCollection(directory, true)

    • setCurrentPage

      public void setCurrentPage(com.pdfjet.Page page)
      Sets the current page.
    • getCurrentPage

      public com.pdfjet.Page getCurrentPage()
      Returns the current page.

      This returns the page last created using createPage(), or the page set with setCurrentPage() page.

    • pageWidth

      public double pageWidth(int page)
      Description copied from class: WTextRenderer
      Returns the page width.

      Returns the total page width (in pixel units), including horizontal margins.

      Specified by:
      pageWidth in class WTextRenderer
    • pageHeight

      public double pageHeight(int page)
      Description copied from class: WTextRenderer
      Returns the page height.

      Returns the total page height (in pixel units), including vertical margins.

      Specified by:
      pageHeight in class WTextRenderer
    • getMargin

      public double getMargin(Side side)
      Description copied from class: WTextRenderer
      Returns the margin.

      Returns the margin at given side (in pixel units).

      Specified by:
      getMargin in class WTextRenderer
    • startPage

      public WPaintDevice startPage(int page)
      Description copied from class: WTextRenderer
      Returns a paint device to render a given page.

      The render() method calls this function once for each page it wants to render.

      Specified by:
      startPage in class WTextRenderer
    • endPage

      public void endPage(WPaintDevice device)
      Description copied from class: WTextRenderer
      Stops painting on the given page.
      Specified by:
      endPage in class WTextRenderer
    • getPainter

      public WPainter getPainter(WPaintDevice device)
      Description copied from class: WTextRenderer
      Returns a painter for the current page.
      Specified by:
      getPainter in class WTextRenderer
    • createPage

      public com.pdfjet.Page createPage(int page)
      Creates a new page.

      The default implementation creates a new page with the same dimensions as the previous page.

      You may want to specialize this method to add e.g.~headers and footers.