Wt
4.11.1
|
A paint device for rendering to a PDF. More...
#include <Wt/WPdfImage.h>
Public Member Functions | |
WPdfImage (const WLength &width, const WLength &height) | |
Create a PDF resource that represents a single-page PDF document. More... | |
WPdfImage (HPDF_Doc pdf, HPDF_Page page, HPDF_REAL x, HPDF_REAL y, HPDF_REAL width, HPDF_REAL height) | |
Create a PDF paint device to paint inside an existing page. More... | |
~WPdfImage () | |
Destructor. | |
void | addFontCollection (const std::string &directory, bool recursive=true) |
Adds a font collection. More... | |
virtual WFlags< PaintDeviceFeatureFlag > | features () const override |
Returns device features. | |
virtual void | setChanged (WFlags< PainterChangeFlag > flags) override |
Indicates changes in painter state. More... | |
virtual void | drawArc (const WRectF &rect, double startAngle, double spanAngle) override |
Draws an arc. More... | |
virtual void | drawImage (const WRectF &rect, const std::string &imgUri, int imgWidth, int imgHeight, const WRectF &sourceRect) override |
Draws an image. More... | |
virtual void | drawLine (double x1, double y1, double x2, double y2) override |
Draws a line. More... | |
virtual void | drawRect (const WRectF &rect) override |
Draws a rectangle. More... | |
virtual void | drawPath (const WPainterPath &path) override |
Draws a path. More... | |
virtual void | drawText (const WRectF &rect, WFlags< AlignmentFlag > alignmentFlags, TextFlag textFlag, const WString &text, const WPointF *clipPoint) override |
Draws text. More... | |
virtual WTextItem | measureText (const WString &text, double maxWidth=-1, bool wordWrap=false) override |
Measures rendered text size. More... | |
virtual WFontMetrics | fontMetrics () override |
Returns font metrics. More... | |
virtual void | init () override |
Initializes the device for painting. More... | |
virtual void | done () override |
Finishes painting on the device. More... | |
virtual bool | paintActive () const override |
Returns whether painting is active. More... | |
virtual WLength | width () const override |
Returns the device width. More... | |
virtual WLength | height () const override |
Returns the device height. More... | |
virtual void | handleRequest (const Http::Request &request, Http::Response &response) override |
Handles a request. More... | |
Public Member Functions inherited from Wt::WResource | |
WResource () | |
Creates a new resource. | |
~WResource () | |
Destroys the resource. More... | |
void | suggestFileName (const Wt::WString &name, ContentDisposition disposition=ContentDisposition::Attachment) |
Suggests a filename to the user for the data streamed by this resource. More... | |
const Wt::WString & | suggestedFileName () const |
Returns the suggested file name. More... | |
void | setDispositionType (ContentDisposition cd) |
Configures the Content-Disposition header. More... | |
ContentDisposition | dispositionType () const |
Returns the currently configured content disposition. More... | |
void | setChanged () |
Generates a new URL for this resource and emits the changed signal. More... | |
void | setInvalidAfterChanged (bool enabled) |
Return "page not found" for prior resource URLs after change. More... | |
bool | invalidAfterChanged () const |
Should "page not found" be returned for outdated resource URLs. More... | |
void | setInternalPath (const std::string &path) |
Sets an internal path for this resource. More... | |
std::string | internalPath () const |
Returns the internal path. More... | |
const std::string & | generateUrl () |
Generates an URL for this resource. More... | |
const std::string & | url () const |
Returns the current URL for this resource. More... | |
Signal & | dataChanged () |
Signal emitted when the data presented in this resource has changed. More... | |
void | setUploadProgress (bool enabled) |
Indicate interest in upload progress. More... | |
Signal< ::uint64_t, ::uint64_t > & | dataReceived () |
Signal emitted when data has been received for this resource. More... | |
void | write (std::ostream &out, const Http::ParameterMap ¶meters=Http::ParameterMap(), const Http::UploadedFileMap &files=Http::UploadedFileMap()) |
Stream the resource to a stream. More... | |
virtual void | handleAbort (const Http::Request &request) |
Handles a continued request being aborted. More... | |
void | haveMoreData () |
Indicate that more data is available. More... | |
void | setTakesUpdateLock (bool enabled) |
Set whether this resource takes the WApplication's update lock. More... | |
bool | takesUpdateLock () const |
Returns whether this resources takes the WApplication's update lock. More... | |
Public Member Functions inherited from Wt::WObject | |
void | addChild (std::unique_ptr< WObject > child) |
Add a child WObject whose lifetime is determined by this WObject. | |
template<typename Child > | |
Child * | addChild (std::unique_ptr< Child > child) |
Add a child WObject, returning a raw pointer. More... | |
std::unique_ptr< WObject > | removeChild (WObject *child) |
Remove a child WObject, so its lifetime is no longer determined by this WObject. | |
template<typename Child > | |
std::unique_ptr< Child > | removeChild (Child *child) |
Remove a child WObject, so its lifetime is no longer determined by this WObject. More... | |
virtual const std::string | id () const |
Returns the (unique) identifier for this object. More... | |
virtual void | setObjectName (const std::string &name) |
Sets an object name. More... | |
virtual std::string | objectName () const |
Returns the object name. More... | |
void | resetLearnedSlots () |
Resets learned stateless slot implementations. More... | |
template<class T > | |
void | resetLearnedSlot (void(T::*method)()) |
Resets a learned stateless slot implementation. More... | |
template<class T > | |
WStatelessSlot * | implementStateless (void(T::*method)()) |
Declares a slot to be stateless and learn client-side behaviour on first invocation. More... | |
template<class T > | |
WStatelessSlot * | implementStateless (void(T::*method)(), void(T::*undoMethod)()) |
Declares a slot to be stateless and learn client-side behaviour in advance. More... | |
void | isNotStateless () |
Marks the current function as not stateless. More... | |
template<class T > | |
WStatelessSlot * | implementJavaScript (void(T::*method)(), const std::string &jsCode) |
Provides a JavaScript implementation for a method. More... | |
Public Member Functions inherited from Wt::Core::observable | |
observable () noexcept | |
Default constructor. | |
virtual | ~observable () |
Destructor. More... | |
template<typename... Args, typename C > | |
auto | bindSafe (void(C::*method)(Args...)) noexcept |
Protects a method call against object destruction. More... | |
template<typename... Args, typename C > | |
auto | bindSafe (void(C::*method)(Args...) const) const noexcept |
Protects a const method call against object destruction. More... | |
template<typename Function > | |
auto | bindSafe (const Function &function) noexcept |
Protects a function against object destruction. More... | |
Public Member Functions inherited from Wt::WPaintDevice | |
virtual | ~WPaintDevice () |
Destructor. More... | |
Protected Member Functions | |
virtual WPainter * | painter () const override |
Returns the painter that is currently painting on the device. More... | |
virtual void | setPainter (WPainter *painter) override |
Sets the painter. | |
Protected Member Functions inherited from Wt::WResource | |
void | beingDeleted () |
Prepares the resource for deletion. More... | |
Protected Member Functions inherited from Wt::WObject | |
virtual WStatelessSlot * | getStateless (Method method) |
On-demand stateless slot implementation. More... | |
Additional Inherited Members | |
Public Types inherited from Wt::WObject | |
typedef void(WObject::* | Method) () |
Typedef for a WObject method without arguments. | |
Public Types inherited from Wt::WPaintDevice | |
typedef PaintDeviceFeatureFlag | FeatureFlag |
Typedef for enum Wt::PaintDeviceFeatureFlag. | |
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:
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:
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) !
Wt::WPdfImage::WPdfImage | ( | HPDF_Doc | pdf, |
HPDF_Page | page, | ||
HPDF_REAL | x, | ||
HPDF_REAL | y, | ||
HPDF_REAL | width, | ||
HPDF_REAL | height | ||
) |
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
).
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.
As of Wt 3.3.7, only TrueType fonts will be selected by default. Prior to Wt 3.3.7, you needed to configure fontconfig (which is used by pango) to only return TrueType fonts. This can be done using a fonts.conf configuration file:
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 provide 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.
|
overridevirtual |
Finishes painting on the device.
This method is called when a WPainter stopped painting.
Implements Wt::WPaintDevice.
|
overridevirtual |
Draws an arc.
The arc describes the segment of an ellipse enclosed by the rect. The segment starts at startAngle
, and spans an angle given by spanAngle
. These angles have as unit degree, and are measured counter-clockwise starting from the 3 o'clock position.
The arc must be stroked, filled, and transformed using the current painter settings.
Implements Wt::WPaintDevice.
|
overridevirtual |
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.
|
overridevirtual |
Draws a line.
The line must be stroked and transformed using the current painter settings.
Implements Wt::WPaintDevice.
|
overridevirtual |
Draws a path.
The path must be stroked, filled, and transformed using the current painter settings.
Implements Wt::WPaintDevice.
|
overridevirtual |
Draws a rectangle.
The rect must be stroked, filled, and transformed using the current painter settings.
Implements Wt::WPaintDevice.
|
overridevirtual |
Draws text.
The text must be rendered, stroked and transformed using the current painter settings.
If clipPoint is not null, a check is performed whether the point is inside of the current clip area. If not, the text is not drawn.
Implements Wt::WPaintDevice.
|
overridevirtual |
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.
|
overridevirtual |
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.
|
overridevirtual |
Returns the device height.
The device height, in pixels, establishes the height of the device coordinate system.
Implements Wt::WPaintDevice.
|
overridevirtual |
Initializes the device for painting.
This method is called when a WPainter starts painting.
Implements Wt::WPaintDevice.
|
overridevirtual |
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.
|
overridevirtual |
|
overrideprotectedvirtual |
Returns the painter that is currently painting on the device.
Implements Wt::WPaintDevice.
|
overridevirtual |
Indicates changes in painter state.
The flags
argument is the logical OR of one or more change flags.
Implements Wt::WPaintDevice.
|
overridevirtual |
Returns the device width.
The device width, in pixels, establishes the width of the device coordinate system.
Implements Wt::WPaintDevice.