Winnovative Software Logo

 HTML to PDF Converter - Excel Library - ASP.NET Charts - RTF to PDF Converter
 PDF Merge and Split - PDF Security - PDF Viewers - PDF to Text - Images Extractor

 
Skip Navigation Links
 

Fine Control of HTML to PDF Conversion

The Winnovative HTML to PDF Converter and the Winnovative PDF Creator products allow a very fine control of the PDF rendering process. The default settings of the converter should be acceptable for majority of the situations but sometimes more control and customizations is necessary.

PDF Document Page Orientation

You can set the PDF document page orientation to Portrait or Landscape. By default the document page is A4 size Portrait orientation. To change the page orientation to Landscape you have to set the PdfConverter.PdfDocumentOptions.PdfPageOrientation property in HTML to PDF Converter or you can set the PDF page orientation when you create a PDF page with PDF Creator. All the pages auto generated  by the PDF Creator during conversion will have the same orientation with the page where the HtmlToPdfElement was placed.

With HTML to PDF Converter for .NET library you can set the landscape orientation with:

            PdfConverter.PdfDocumentOptions.PdfPageOrientation = PDFPageOrientation.Landscape

and with PDF Creator for .NET library you can create a new landscape page with :

            Document.Pages.AddNewPage(PageSize.A4, new Margins(10,10,0, 0), PageOrientation.landscape)

PDF Document Page Size

You can set the PDF page size to a standard size like A4, A3, etc. or you can set the PDF page size to a custom value. The custom page size width and height are specified in points (1 point is 1/72 inches).

With the HTML to PDF Converter for .NET library you can set the PDF page size to a standard value with:

             PdfConverter.PdfDocumentOptions.PdfPageSize = PdfPageSize.A4

or a custom value with:

             PdfConverter.PdfDocumentOptions.PdfPageSize = PdfPageSize.Custom
             PdfConverter.PdfDocumentOptions.CustomPdfPageSize = new SizeF(widthInPoints, heightInPoints)

With the PDF Creator for .NET library you can set the PDF page size to a standard value with:

            Document.Pages.AddNewPage(PageSize.A4, new Margins(10,10,0, 0), PageOrientation.landscape)

or a custom value with:

            Document.Pages.AddNewPage(new PageSize(widthInPoints, widthInPoints), new Margins(10,10,0, 0), PageOrientation.landscape)

HTML to PDF Converter Virtual Display

The converter internally uses a virtual display where to render the HTML page very similar to what the web browser does on the screen. The virtual display resolution is given by the Windows DPI currently set in the display properties in Windows Control Panel. The default Windows DPI is 96 dpi (dots per inch). The web page elements dimensions are usually measured in pixels and this is the reason why the virtual display of the converter is also specified in pixels. These are the only dimensions used by the converter which are expressed in pixels. All the other dimensions are specified in points (1 point is 1/72 inches). The UnitsConverter class can be used to convert dimensions from pixels to points and from points to pixels.

You can specify the virtual display width and height in pixels using the PdfConverter.PageWidth and PdfConverter.PageHeight properties or you can specify the same values as parameters when you construct the PdfConverter object.

For the PDF Creator you can specify the virtual display width and height by setting the the HtmlViewerWidth and HtmlViewerHeight properties of the HtmlToPdfElement.

By default the PageWidth is set to 1024 pixels which should be sufficient to display the majority of the web pages. If the web page you are converting cannot be completely displayed in this width then you can increase this value or you can set the PageWidth to 0 to allow the converter to automatically determine your web page width from the HTML elements width. The PageHeight property is 0 by default which means the virtual display height will be automatically set to display the whole HTML page height. In general the PageHeight property should not be set in your code. However, there are situations when the converter cannot automatically determine the web page height for example when the web page contains a frame set because in this case the web page can be displayed in any height. In such a situation you can manually set the PageHeight to the desired value in pixels.

HTML Content Resizing in PDF Page

After the HTML content is displayed in the virtual display the virtual display content will be transferred into PDF as you would take a picture of the virtual display and put that picture into a PDF document. The PDF documents pages have a fixed size in points. For example, the A4 portrait page is 595 points in width and 842 points in height. If the virtual display width is more than 595 points then the rendered HTML content would be shrunk to fit the PDF page width and display the whole HTML content in the PDF document. If the virtual display width is less than 595 points then the rendered HTML content will not be resized and will be rendered in the top left corner of the PDF page at real size.

The dimension of the A4 portrait page in virtual device pixels is 793x1122 pixels. This means that at a default virtual display width of 1024 pixels the HTML content will be shrunk to fit the PDF page. This is the reason why you see smaller fonts and images in PDF.
The calculation above does not take into account the possible margins set for the PDF page and it is done at the default resoltion of 96 DPI. If you have set the PDF page margins or if you are using a custom Windows DPI then the exact calculation of the available width in PDF page can be made using UnitsConverter API.

The FitWidth property was added to the PdfConverter.PdfDocumentOptions to control if the rendered HTML content can be scaled to fit the PDF page width. The default value is true which makes the HTML content to be resized if necessary to fit the PDF page width. When false, the HTML content will not be resized and it will be rendered at the real size in PDF.

When the FitWidth property is false the HTML content could be wider than the PDF page width and the therefore the HTML content will be cut off to the right in PDF. In this case, in order to get the whole content in PDF you have to set a wider page for the PDF document. You can first try to set landscape orientation for the PDF page by setting PdfConverter.PdfDocumentOptions.PdfPageOrientation = PDFPageOrientation.Landscape. If this not enough you can choose a wider standard page like A3 or A2. It is also possible to set a custom size for the PDF page when the PdfConvert.PdfDocumentOptions.PdfPageSize=PdfPageSize.Custom In this case the custom size of the PDF page will be taken from PdfConverter.PdfDocumentOptions.CustomPdfPageSize property. The PdfConverter.PdfDocumentOptions.AutoSizePdfPage was added to automatically determine a custom PDF page width when the FitWidth property is false. The AutoSizePdfPage can create PDF documents with non-standard PDF page size.

If you don't want to resize the PDF page but you want to keep it A4 portrait for example, then you have to decrease the virtual display width. If your page can be correctly and entirely displayed in 793 pixels (which is the width of the A4 portrait page in pixels without margins at default 96 dpi resolution) you can set this value for PdfConverter.PageWidth property and you should get the whole HTML rendered at real size in PDF.

The HTML content centering in PDF problem can be observed when the HTML content size is fixed and it is less than 1024 pixels in width. In this case there will normally be an empty space in the right side of the web page displayed at 1024 pixels. When the virtual display content is transferred to PDF the content will appear as not centered in PDF. You can also solve this if you set the PdfConverter.PageWidth to a value of 793 pixels or less.

Single PDF Page Conversion

The HTML to PDF Converter offers the possibility to produce the whole HTML content into a single PDF page automatically resized to display the whole content. To get this behavior you have to set the PdfConverter.PdfDocumentOptions.SinglePage property on true. When the FitWidth is true the PDF page width will be preserved, otherwise the PDF page width will be automatically resized to display the whole HTML content.