Wt  3.3.8
Public Member Functions | List of all members
Wt::Render::WPdfRenderer Class Reference

An XHTML to PDF renderer. More...

#include <Wt/Render/WPdfRenderer>

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

Public Member Functions

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

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);

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

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

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

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("<p style=\"font-family: 'DejaVuSans', Arial\">élève, fenêtre, âme</p>");
See also
WPdfImage::addFontCollection()
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.

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.

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

Returns the margin.

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

Implements Wt::Render::WTextRenderer.

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

Returns the page height.

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

Implements Wt::Render::WTextRenderer.

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

Returns the page width.

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

Implements Wt::Render::WTextRenderer.

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.

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

Sets the page margins.

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

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

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.


Generated on Mon Sep 4 2017 for the C++ Web Toolkit (Wt) by doxygen 1.8.11