Resizing Content During Conversion with Select.Pdf Html to Pdf Converter | |
Html pages that are converted to pdf and pdf pages in the generated document usually do not have the same size. Because of that, Select.Pdf Library needs to perform some operations on the content rendered from the web page to be able to display it in the pdf page.
Pdf Page Size
The size of the pdf pages can be specified using PdfPageSize property of the HtmlToPdfOptions object. More details about this in the following section: Setting Pdf Page Properties.
Web Page Size
Select.Pdf Html to Pdf Converter has an internal browser that renders the web page just like a regular browser. There are some properties that control how the web page will look in the internal browser:
WebPageWidth - Gets or sets the width of the converted web page as it would appear in the internal browser used to render the html.
The web page width is specified in pixels and the default value is 1024px. The page is laid out the way a browser window this wide lays it out. How that width maps onto the pdf page is described in Content Resizing below.
This property can also be set directly in the constructor of HtmlToPdf class.
WebPageHeight - Gets or sets the height of the converted web page as it would appear in the internal browser used to render the html.
The web page height is specified in pixels and the default value is 0px. The height does not limit what is converted: the whole web page is converted and flows onto as many pdf pages as it needs. To leave out the content below this height, set WebPageFixedSize to true as well.
It is the height of the browser window the page is loaded in: the height scripts on the page see (window.innerHeight) and what decides which parts of the page count as visible while it loads. With the default value the window is as tall as the area of a pdf page the content is printed in, so scripts see the page they are printed on. With ManagedContentTrimming set to false the window has almost no height while the page loads, and content that a script sizes from the window height, or loads or reveals only once it is scrolled into view, can be missing from the generated pdf; for such pages set a height, for example 768.
The printed layout does not depend on it: CSS viewport units such as 100vh are resolved against the pdf page.
This property can also be set directly in the constructor of HtmlToPdf class.
WebPageFixedSize - Controls whether the content below WebPageHeight is left out of the generated pdf document.
The default value of this property is false. If it is set to true, the web page is converted only down to WebPageHeight pixels, and the content below that height is not included. The width is not affected: content wider than WebPageWidth is scaled to fit the pdf page either way.
A fixed size taller than the space available on the pdf page is spread over as many pages as it needs - it is not squeezed onto a single page. Every page but the last is a full page, and the last one is cut at the height asked for: the content below it is removed from the document, not hidden. A line of text is never cut: when one crosses that height the last page ends above it, so it can be a line shorter than the height asked for. Bookmarks, mapped web elements, links and internal destinations follow the same height.
Note: WebPageFixedSize needs a page height set with WebPageHeight - there is no size to fix the browser at otherwise. With the default value (0) this property is ignored. A PageRanges set alongside it is honoured within the fixed size.
Content Resizing
Because a web page generally has a different width compared with a standard pdf page, the content is scaled to fit the pdf page. As an example, most web sites are optimized for browsers with page widths of at least 1024px or 1280px. A standard A4 page has 595 x 842 points. 1 point is 1/72 inch. 1 pixel is 1/96 inch. This means that an A4 page width is 793px. Because the web page width (1024px or more) is larger than this pdf page width, the rendered content is scaled down to fit the pdf page width.
The converter renders the web page in the internal browser at WebPageWidth pixels wide, then scales the result so that width maps to the pdf page content width (the page width minus the left and right margins). A smaller WebPageWidth therefore produces larger content in the pdf, and a larger value produces smaller content. Content wider than WebPageWidth is not cut off: it is scaled down further, so that the widest content fits the pdf page. Content that is taller than one pdf page flows onto additional pages automatically.
The following sample code shows the simplest code that can be used to convert an url to pdf:
// instantiate a html to pdf converter object HtmlToPdf converter = new HtmlToPdf(); // create a new pdf document converting an url PdfDocument doc = converter.ConvertUrl(url);
The above code is equivalent with the following sample code where the default values are explicitly specified:
// instantiate a html to pdf converter object HtmlToPdf converter = new HtmlToPdf(); // set converter options converter.Options.PdfPageSize = PdfPageSize.A4; converter.Options.PdfPageOrientation = PdfPageOrientation.Portrait; converter.Options.WebPageWidth = 1024; converter.Options.WebPageHeight = 0; converter.Options.WebPageFixedSize = false; // create a new pdf document converting an url PdfDocument doc = converter.ConvertUrl(url);