Wt: Wt::Render::WPdfRenderer Class Reference

Wt 4.10.4

Loading...
Searching...
No Matches
Public Member Functions | List of all members
Wt::Render::WPdfRenderer Class Reference
XHTML Rendering (Wt::Render)

An XHTML to PDF renderer. More...

`#include <Wt/Render/WPdfRenderer.h>`

Inheritance diagram for Wt::Render::WPdfRenderer:
[legend]

## Public Member Functions
| WPdfRenderer (HPDF_Doc pdf, HPDF_Page page=nullptr)
| Creates a new PDF renderer.
void | setMargin (double cm, WFlags< Side > sides=AllSides)
| Sets the page margins.
void | setDpi (int dpi)
| Sets the resolution.
void | addFontCollection (const std::string &directory, bool recursive=true)
| Adds a font collection.
void | setCurrentPage (HPDF_Page page)
| Sets the current page.
HPDF_Page | currentPage () const
| Returns the current page.
virtual double | pageWidth (int page) const override
| Returns the page width.
virtual double | pageHeight (int page) const override
| Returns the page height.
virtual double | margin (Side side) const override
| Returns the margin.
virtual WPaintDevice * | startPage (int page) override
| Returns a paint device to render a given page.
virtual void | endPage (WPaintDevice *device) override
| Stops painting on the given page.
virtual WPainter * | getPainter (WPaintDevice *device) override
| Returns a painter for the current page.
virtual HPDF_Page | createPage (int page)
| Creates a new page.
Public Member Functions inherited from Wt::Render::WTextRenderer
virtual | ~WTextRenderer ()
| Destructor.
double | render (const WString &text, double y=0)
| Renders an XHTML fragment.
bool | setStyleSheetText (const WString &contents)
| Sets the contents of a cascading style sheet (CSS).
bool | useStyleSheet (const WString &filename)
| Appends an external cascading style sheet (CSS).
void | clearStyleSheet ()
| Clears the used stylesheet.
WString | styleSheetText () const
| Returns the CSS in use.
std::string | getStyleSheetParseErrors () const
| Returns all parse error information of the last call to setStyleSheetText.
double | textWidth (int page) const
| Returns the page text width.
double | textHeight (int page) const
| Returns the page text height.
void | setFontScale (double scale)
| Sets the scaling factor used for font rendering.
double | fontScale () const
| Returns the font scaling factor.
virtual void | paintNode (WPainter &painter, const Node &node)
| Paints an XHTML node.

## Additional Inherited Members
Protected Member Functions inherited from Wt::Render::WTextRenderer
| WTextRenderer ()
| Constructor.

## Detailed Description

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 libharu 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 setFontScale() to scale the font size differently than other content.

Usage example:

extern "C" {
HPDF_STATUS HPDF_UseUTFEncodings(HPDF_Doc pdf);
}
HPDF_Doc pdf = HPDF_New(error_handler, 0);
HPDF_UseUTFEncodings(pdf); // enables UTF-8 encoding with true type fonts
HPDF_Page page = HPDF_AddPage(pdf);
HPDF_Page_SetSize(page, HPDF_PAGE_SIZE_A4, HPDF_PAGE_PORTRAIT);
Render::WPdfRenderer renderer(pdf, page);
renderer.setMargin(2.54);
renderer.setDpi(96);
renderer.render("<p style=\"background-color: #c11\">Hello, world !</p>");
HPDF_SaveToFile(pdf, "hello.pdf");
HPDF_Free(pdf);
Wt::Render::WPdfRenderer
An XHTML to PDF renderer.
Definition WPdfRenderer.h:70

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 & Destructor Documentation

## ◆ WPdfRenderer()

Wt::Render::WPdfRenderer::WPdfRenderer | ( | HPDF_Doc | pdf,
|  | HPDF_Page | page = nullptr )

Creates a new PDF renderer.

The PDF renderer will render on the given `pdf` (starting). If the `page` is not `0`, 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.

## Member Function Documentation

## ◆ addFontCollection()

void Wt::Render::WPdfRenderer::addFontCollection | ( | const std::string & | directory,
|  | bool | recursive = true )

Adds a font collection.

If Wt 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 Wt was not configured to use `libpango`, you will have to add the directories where Wt should look for fonts. You will also have to specify the required font in the HTML source, e.g.:

Render::WPdfRenderer renderer(pdf, page);
// ...
renderer.render(Wt::utf8(u8"<p style=\"font-family: 'DejaVuSans', Arial\">\u00e9l\u00e8ve, fen\u00eatre, \u00e2me</p>"));
Wt::WString::utf8
WT_API WString utf8(const char *value)
Short hand for WString(const char * value, CharEncoding::UTF8)

See also
  WPdfImage::addFontCollection()

## ◆ createPage()

virtual HPDF_Page Wt::Render::WPdfRenderer::createPage | ( | int | page | ) |
| virtual

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.

## ◆ currentPage()

HPDF_Page Wt::Render::WPdfRenderer::currentPage | ( |  | ) | const

Returns the current page.

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

## ◆ margin()

virtual double Wt::Render::WPdfRenderer::margin | ( | Side | side | ) | const
| overridevirtual

Returns the margin.

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

Implements Wt::Render::WTextRenderer.

## ◆ pageHeight()

virtual double Wt::Render::WPdfRenderer::pageHeight | ( | int | page | ) | const
| overridevirtual

Returns the page height.

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

Implements Wt::Render::WTextRenderer.

## ◆ pageWidth()

virtual double Wt::Render::WPdfRenderer::pageWidth | ( | int | page | ) | const
| overridevirtual

Returns the page width.

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

Implements Wt::Render::WTextRenderer.

## ◆ setDpi()

void Wt::Render::WPdfRenderer::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 margin() pixel calculations.

The default resolution is 72 DPI.

## ◆ setMargin()

void Wt::Render::WPdfRenderer::setMargin | ( | double | cm,
|  | WFlags< Side > | sides = AllSides )

Sets the page margins.

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

## ◆ startPage()

virtual WPaintDevice * Wt::Render::WPdfRenderer::startPage | ( | int | page | ) |
| overridevirtual

Returns a paint device to render a given page.

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

Implements Wt::Render::WTextRenderer.