HTML to PDF converter for Java and .NET

<< back

PDF page format and content scaling

There are 3 important PD4ML properties, impact resulting PDF page format and layout: pageSize, pageInsets (margins), htmlWidth (also known as userSpaceWidth in some contexts).

pageSize defines output paper file format: A4, ISOB4, LETTER, LEGAL etc. Corresponding constants are defined in PD4Constants class, but you may define any non-standard page dimension with, for instance, new Dimension(400,400) and pass it to pd4ml.setPageSize() method if the dimension are specified in typographical points, or to pd4ml.setPageSizeMM() if they are in millimeters.

Default page format is A4, orientation portrait.

PDF file format does not explicitly specifies on meta level if a particular page is landscape- or portrait-oriented. If a page has its width greater than height - it is landscape, otherwise it is portait. PD4ML implements "page rotation" by a simple swapping of page height and withs. There is an utility method for that:

Dimension landscapeA4 = pd4ml.changePageOrientation(PD4Constants.A4);

pageInsets define blank page area width around the page content (margins). The default value for it new Insets( 25, 50, 25, 25 ), which represents top, left, bottom, right margins correspondingly, given in typographical points.

Also the insets can be increased by HTML document margins (i.e. <body style="margin: 50">). The HTML document margins have some specifics, when converted to PDF: the top margin is applied on the first page only, the bottom margin on the last page only.

Document with no margins:

Note: PD4ML.addStyle() takes effect only in PD4ML Pro and derived products. For PD4ML Std the style must be defined in HTML document itself.

htmlWidth value defines "virtual web browser" frame width (in screen pixels), and by default is set to 640.

PD4ML renders source HTML page using htmlWidth parameter and maps the resulting layout to the efficient PDF page width (which is pageFormat.width - pageInsets.left - pageInsets.right).

That makes HTML-to-PDF scale factor computed like that:

scale = (pageFormat.width - pageInsets.left - pageInsets.right) / htmlWidth

From the above it is obvious, that an increasing htmlWidth makes the resulting document content appears smaller.


htmlWidth = 800


htmlWidth = 1200

See also: Output page format change on-a-fly

Copyright ©2004-17 zefer|org. All rights reserved. Bookmark and Share