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 width is only an indication of the minimum page width recommended for conversion. If the content does not fit this width, the converter will automatically resize the internal browser to fit the whole html content. To avoid this, the WebPageFixedSize property needs to be set to true. When WebPageFixedSize is true, the web page will be rendered with the specified WebPageWidth and WebPageHeight even though the content might be truncated.
If WebPageWidth is set to 0, the converter will automatically determine the page width, finding the width that will fit the html content.
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. This means that the converter will automatically calculate the page height. Generally this property does not need to be changed, but there are situations when the converter cannot calculate correctly the web page height (for example for web pages with frames) and in that case, WebPageHeight needs to be set, otherwise no content might appear in the generated pdf.
Note: If the WebPageHeight is set, the content that exceeds this page height is truncated and will not appear in the generated pdf document. Only using the default 0 value will allow the whole page content to be rendered all the time in the generated pdf document.
This property can also be set directly in the constructor of HtmlToPdf class.
WebPageFixedSize - Controls whether the web page is rendered with a fixed size internal browser or the size automatically extends to make the whole content visible.
The default value of this property is false.
If the web page does not fit the dimensions specified by the WebPageWidth and WebPageHeight properties and WebPageFixedSize is set to false, the converter will try to automatically calculate a larger width and height to be able to display the whole html content.
If WebPageFixedSize is set to true, this will indicate the converter not to calculate another width and height, but use width and height specified by WebPageWidth and WebPageHeight. These dimensions with be used even though the content will not fit the available space and it will be truncated.
Note: If WebPageFixedSize is set to true, a page height needs to be set using WebPageHeight, because the default value (0) will make the converter fail (cannot render a web page with no height).
Content Resizing
Because a web page has generally a different width compared with a standard pdf page, the content will not fit perfectly when the web page content is rendered into pdf. 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, when the content is rendered into pdf, it will either get trucated or it needs to be resized (shrinked) to fit the pdf page width.
Select.Pdf Html to Pdf Converter has a few properties that control how the content from the web page is resized during the rendering into the pdf document:
AutoFitWidth - Specifies the html content horizontal auto fit mode.
The converter considers both AutoFitWidth and AutoFitHeight when the html content is rendered in the pdf page or specified rectangle.
If this property is set to NoAdjustment, the html content is not resized horizontally in any way to fit the available space. If the content is larger, it will be cut and not all of it will be displayed in the generated pdf file.
If this property is set to ShrinkOnly, the html content is resized only if the content width is larger than the destination space (pdf page or rectangle) width. In this case, the content is shrunk to fit the destination space width and the elements that it contains (texts, images) will appear smaller in the generated pdf document than in the original web page. If the original content width is smaller than the destination width, no adjustments will be done and the content will be rendered exactly as it is, even though some additional white space might appear to its right.
If this property is set to AutoFit, the html content is resized to fit the available width of the destination space. If the original content width is smaller than the destination width, the elements rendered (texts, images) will appear larger in the generated pdf document. If the original content width is larger than the destination width, the elements rendered (texts, images) will appear smaller in the generated pdf document.
The default value of this property is ShrinkOnly.
AutoFitHeight - Specifies the html content vertical auto fit mode.
The converter considers both AutoFitWidth and AutoFitHeight when the html content is rendered in the pdf page or specified rectangle.
If this property is set to NoAdjustment, the html content is not resized vertically in any way to fit the available space. If the content is larger, it will be cut and not all of it will be displayed in the generated pdf file.
If this property is set to ShrinkOnly, the html content is resized only if the content height is larger than the destination space (pdf page or rectangle) height. In this case, the content is shrunk to fit the destination space height and the elements that it contains (texts, images) will appear smaller in the generated pdf document than in the original web page. If the original content height is smaller than the destination height, no adjustments will be done and the content will be rendered exactly as it is, even though some additional white space might appear at the bottom.
If this property is set to AutoFit, the converter will treat it like ShrinkOnly.
The default value of this property is NoAdjustment and for rendering, the converter will only take AutoFitWidth into consideration.
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; converter.Options.AutoFitWidth = HtmlToPdfPageFitMode.ShrinkOnly; converter.Options.AutoFitHeight = HtmlToPdfPageFitMode.NoAdjustment; // create a new pdf document converting an url PdfDocument doc = converter.ConvertUrl(url);