Wt: Wt::WPdfImage Class Reference

Wt 3.3.0

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes

- Class List
- Class Hierarchy
- Class Members

Public Member Functions | Protected Member Functions
Wt::WPdfImage Class Reference
Painting system

A paint device for rendering to a PDF. More...

`#include <Wt/WPdfImage>`

Inheritance diagram for Wt::WPdfImage:
[legend]

List of all members.

## Public Member Functions
| WPdfImage (const WLength &width, const WLength &height, WObject *parent=0)
| Create a PDF resource that represents a single-page PDF document.
| WPdfImage (HPDF_Doc pdf, HPDF_Page page, HPDF_REAL x, HPDF_REAL y, HPDF_REAL width, HPDF_REAL height, WObject *parent=0)
| Create a PDF paint device to paint inside an existing page.
| ~WPdfImage ()
| Destructor.
void | addFontCollection (const std::string &directory, bool recursive=true)
| Adds a font collection.
virtual WFlags < WPaintDevice::FeatureFlag > | features () const
| Returns device features.
virtual void | setChanged (WFlags< ChangeFlag > flags)
| Indicates changes in painter state.
virtual void | drawArc (const WRectF &rect, double startAngle, double spanAngle)
| Draws an arc.
virtual void | drawImage (const WRectF &rect, const std::string &imgUri, int imgWidth, int imgHeight, const WRectF &sourceRect)
| Draws an image.
virtual void | drawLine (double x1, double y1, double x2, double y2)
| Draws a line.
virtual void | drawPath (const WPainterPath &path)
| Draws a path.
virtual void | drawText (const WRectF &rect, WFlags< AlignmentFlag > alignmentFlags, TextFlag textFlag, const WString &text)
| Draws text.
virtual WTextItem | measureText (const WString &text, double maxWidth=-1, bool wordWrap=false)
| Measures rendered text size.
virtual WFontMetrics | fontMetrics ()
| Returns font metrics.
virtual void | init ()
| Initializes the device for painting.
virtual void | done ()
| Finishes painting on the device.
virtual bool | paintActive () const
| Returns whether painting is active.
virtual WLength | width () const
| Returns the device width.
virtual WLength | height () const
| Returns the device height.
virtual void | handleRequest (const Http::Request &request, Http::Response &response)
| Handles a request.
## Protected Member Functions
virtual WPainter * | painter () const
| Returns the painter that is currently painting on the device.
virtual void | setPainter (WPainter *painter)
| Sets the painter.

## Detailed Description

A paint device for rendering to a PDF.

A WPdfImage paint device should be used in conjunction with a WPainter, and can be used to make a PDF version of a WPaintedWidget's contents.

The PDF is generated using The Haru Free PDF Library, and this class is included in the library only if `libharu` was found during the build of the library.

You can use the image as a resource and specialize handleRequest() to paint the contents on the fly. Alternatively can also use write() to serialize to a PDF file (std::ostream). The latter usage is illustrated by the code below:

     Wt::Chart::WCartesianChart
     *chart = ...

     Wt::WPdfImage pdfImage("4cm", "3cm");
     {
       Wt::WPainter p(&pdfImage);
       chart->paint(p);
     }
     std::ofstream f("chart.pdf", std::ios::out | std::ios::binary);
     pdfImage.write(f);

A constructor is provided which allows the generated PDF image to be embedded directly into a page of a larger `libharu` document, and this approach is used for example by the WPdfRenderer to render XHTML to multi-page PDF files.

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 and matched to WFont descriptions.

This paint device has the following limitations:

- images (WPainter::drawImage()) can only be included from local files, and only JPG and PNG images are supported.
- drop shadows are not supported.

## Constructor & Destructor Documentation

Wt::WPdfImage::WPdfImage | ( | const WLength & | width,
|  | const WLength & | height,
|  | WObject * | parent = `0`
| ) |  |

Create a PDF resource that represents a single-page PDF document.

The single page will have a size `width` x `height`. The PDF will be using the same DPI (72dpi) as is conventionally used for the desktop.

The passed width and height (such as 4 cm by 3 cm) can be specified in physical units (e.g. 4cm x 3cm), but this will be converted to pixels using the default DPI used in CSS (96dpi) !

See also:
  write()

Wt::WPdfImage::WPdfImage | ( | HPDF_Doc | pdf,
|  | HPDF_Page | page,
|  | HPDF_REAL | x,
|  | HPDF_REAL | y,
|  | HPDF_REAL | width,
|  | HPDF_REAL | height,
|  | WObject * | parent = `0`
| ) |  |

Create a PDF paint device to paint inside an existing page.

The image will be drawn in the existing page, as an image with lower-left point (`x`, `y`) and size (`width` x `height`).

## Member Function Documentation

void Wt::WPdfImage::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, which is seeded with information on installed fonts by fontconfig. In that case, invocations for this method is ignored. Only TrueType fonts are supported, and thus you need to configure fontconfig (which is used by pango) to only return TrueType fonts. This can be done using a fonts.conf configuration file:

    <?xml version='1.0'?>
    <!DOCTYPE fontconfig SYSTEM 'fonts.dtd'>
    <fontconfig>
        <selectfont>
          <rejectfont>
    	 <glob>*.pfb</glob>
          </rejectfont>
        </selectfont>
    </fontconfig>

You may need to add more glob patterns to exclude other fonts than TrueType, and also to exclude TrueType fonts which do not work properly with libharu.

If Wt has not been configured to use `libpango`, then this method may be used to indicate the location of TrueType fonts. The main drawback compared to libpango is that font selection is not steered by the need for particular characters, i.e. font selection is independent from the text's need for specific characters. Most TrueType fonts provided only partial unicode support. The provided `directory` will be searched for fonts (currently only TrueType ".ttf" or ".ttc" fonts). TrueType fonts are preferable over Base-14 fonts (which are PDF's default fonts) since they provide partial (or complete) unicode support.

When using Base-14 fonts, WString::narrow() will be called on text which may result in loss of information.

void Wt::WPdfImage::done | ( |  | ) | `[virtual]`

Finishes painting on the device.

This method is called when a WPainter stopped painting.

See also:
  WPainter::end()

Implements Wt::WPaintDevice.

void Wt::WPdfImage::drawArc | ( | const WRectF & | rect,
|  | double | startAngle,
|  | double | spanAngle
| ) |  | `[virtual]`

Draws an arc.

The arc is defined as in WPainter::drawArc(const WRectF& rectangle, int startAngle, int spanAngle)

The arc must be stroked, filled, and transformed using the current painter settings.

Implements Wt::WPaintDevice.

void Wt::WPdfImage::drawImage | ( | const WRectF & | rect,
|  | const std::string & | imageUri,
|  | int | imgWidth,
|  | int | imgHeight,
|  | const WRectF & | sourceRect
| ) |  | `[virtual]`

Draws an image.

Draws sourceRect from the image with URL `imageUri` and original dimensions imgWidth and `imgHeight` to the location, into the rectangle defined by `rect`.

The image is transformed using the current painter settings.

Implements Wt::WPaintDevice.

void Wt::WPdfImage::drawLine | ( | double | x1,
|  | double | y1,
|  | double | x2,
|  | double | y2
| ) |  | `[virtual]`

Draws a line.

The line must be stroked and transformed using the current painter settings.

Implements Wt::WPaintDevice.

void Wt::WPdfImage::drawPath | ( | const WPainterPath & | path | ) | `[virtual]`

Draws a path.

The path must be stroked, filled, and transformed using the current painter settings.

Implements Wt::WPaintDevice.

void Wt::WPdfImage::drawText | ( | const WRectF & | rect,
|  | WFlags< AlignmentFlag > | alignmentFlags,
|  | TextFlag | textFlag,
|  | const WString & | text
| ) |  | `[virtual]`

Draws text.

The text must be rendered, stroked and transformed using the current painter settings.

Implements Wt::WPaintDevice.

WFontMetrics Wt::WPdfImage::fontMetrics | ( |  | ) | `[virtual]`

Returns font metrics.

This returns font metrics for the current font.

Throws a std::logic_error if the underlying device does not provide font metrics.

Implements Wt::WPaintDevice.

void Wt::WPdfImage::handleRequest | ( | const Http::Request & | request,
|  | Http::Response & | response
| ) |  | `[virtual]`

Handles a request.

Reimplement this method so that a proper response is generated for the given request. From the `request` object you can access request parameters and whether the request is a continuation request. In the `response` object, you should set the mime type and stream the output data.

A request may also concern a continuation, indicated in Http::Request::continuation(), in which case the next part for a previously created continuation should be served.

While handling a request, which may happen at any time together with event handling, the library makes sure that the resource is not being concurrently deleted, but multiple requests may happend simultaneously for a single resource.

Implements Wt::WResource.

virtual WLength Wt::WPdfImage::height | ( |  | ) | const`[virtual]`

Returns the device height.

The device height, in pixels, establishes the height of the device coordinate system.

Implements Wt::WPaintDevice.

void Wt::WPdfImage::init | ( |  | ) | `[virtual]`

Initializes the device for painting.

This method is called when a WPainter starts painting.

See also:
  WPainter::begin(WPaintDevice *), painter()

Implements Wt::WPaintDevice.

WTextItem Wt::WPdfImage::measureText | ( | const WString & | text,
|  | double | maxWidth = `-1`,
|  | bool | wordWrap = `false`
| ) |  | `[virtual]`

Measures rendered text size.

Returns the bounding rect of the given text when rendered using the current font.

If `maxWidth` != -1, then the text is truncated to fit in the width.

If `wordWrap` = `true` then text is truncated only at word boundaries. Note that in this case the whitespace at the truncated position is included in the text but not accounted for by the returned width (since usually you will not render the whitespace at the end of a line).

Throws a std::logic_error if the underlying device does not provide font metrics.

Implements Wt::WPaintDevice.

virtual bool Wt::WPdfImage::paintActive | ( |  | ) | const`[virtual]`

Returns whether painting is active.

See also:
  init(), painter()

Implements Wt::WPaintDevice.

virtual WPainter* Wt::WPdfImage::painter | ( |  | ) | const`[protected, virtual]`

Returns the painter that is currently painting on the device.

See also:
  init()

Implements Wt::WPaintDevice.

void Wt::WPdfImage::setChanged | ( | WFlags< ChangeFlag > | flags | ) | `[virtual]`

Indicates changes in painter state.

The `flags` argument is the logical OR of one or more change flags.

See also:
  ChangeFlag

Implements Wt::WPaintDevice.

virtual WLength Wt::WPdfImage::width | ( |  | ) | const`[virtual]`

Returns the device width.

The device width, in pixels, establishes the width of the device coordinate system.

Implements Wt::WPaintDevice.

All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator