PdfConverter Class |
PdfConverter defines a set of methods to render a PDF document from a URL or from a HTML string. The conversion result can be a Stream of bytes, a byte[] object, a file on the disk or a Document object. The generated PDF document can be further customized, saved in a file on disk or can be sent as HTTP response to the client browser.
The method below converts the URL specified as parameter to a PDF document returned as bytes array. The bytes array can be sent to a web browser from an ASP.NET application, saved in a file on disk or into a database or sent by email as an attachment.
To convert a HTML string to PDF you can use one of the following methods below. The first method simply renders the HTML string as a PDF document. The second one accepts an additional parameter urlBase which is the full URL of the page from where you have retrieved the HTML string. The urlBase parameter is a hint for the converter which is used to determine the full URL of the images and other external files like CSS and JavaScript referenced in the HTML string by a relative URL. If you don't set this parameter the images referenced by relative URLS won't appear in the document and the styles from external CSS files won't be applied to the rendered document.
public byte[] GetPdfBytesFromHtmlString(string htmlString); public byte[] GetPdfBytesFromHtmlString(string htmlString, string urlBase);
The correspondent methods you can use to render the PDF document in a file on the disk are listed below. These methods internally use the methods above to get the bytes array and then they simply save the bytes in the specified file on disk.
public void SavePdfFromUrlToFile(string url, string outFile); public void SavePdfFromHtmlStringToFile(string htmlString, string outFile); public void SavePdfFromHtmlStringToFile(string htmlString, string outFile, string urlBase);
The methods above first obtain the PDF document as a byte array and then simply return that array of bytes or save it in a file. There is a set of methods which can save the PDF document directly into a stream without storing it first in the memory. The name of these methods have the ToStream suffix instead of the ToFile suffix from the set of Save methods prototyped above:
public void SavePdfFromUrlToStream(string url, Stream outPdfStream); public void SavePdfFromUrlToStream(string url, string internalLinksDocUrl, Stream outPdfStream); public void SavePdfFromHtmlFileToStream(string htmlFilePath, Stream outPdfStream); public void SavePdfFromHtmlFileToStream(string htmlFilePath, string internalLinksDocUrl, Stream outPdfStream); public void SavePdfFromHtmlStringToStream(string htmlString, Stream outPdfStream); public void SavePdfFromHtmlStringToStream(string htmlString, string urlBase, Stream outPdfStream); public void SavePdfFromHtmlStringToStream(string htmlString, string urlBase, string internalLinksDocUrl, Stream outPdfStream); public void SavePdfFromHtmlStreamToStream(System.IO.Stream htmlStream, Encoding streamEncoding, Stream outPdfStream); public void SavePdfFromHtmlStreamToStream(System.IO.Stream htmlStream, Encoding streamEncoding, string urlBase, Stream outPdfStream); public void SavePdfFromHtmlStreamToStream(System.IO.Stream htmlStream, Encoding streamEncoding, string urlBase, string internalLinksDocUrl, Stream outPdfStream);
There is also a set of methods returning a Document object that can be used to further modify the generated PDF document as described in the Post Convert Customization Overview section by adding new PDF pages and new PDF elements to the document. The Document class and the elements that can be added to a Document object are defined in the ExpertPdf.HtmlToPdf.PdfDocument namespace.
public Document GetPdfDocumentObjectFromUrl(string url); public Document GetPdfDocumentObjectFromUrl(string url, string internalLinksDocUrl); public Document GetPdfDocumentObjectFromHtmlFile(string htmlFilePath); public Document GetPdfDocumentObjectFromHtmlFile(string htmlFilePath, string internalLinksDocUrl); public Document GetPdfDocumentObjectFromHtmlString(string htmlString); public Document GetPdfDocumentObjectFromHtmlString(string htmlString, string urlBase); public Document GetPdfDocumentObjectFromHtmlString(string htmlString, string urlBase, string internalLinksDocUrl); public Document GetPdfDocumentObjectFromHtmlStream(System.IO.Stream htmlStream, Encoding streamEncoding); public Document GetPdfDocumentObjectFromHtmlStream(System.IO.Stream htmlStream, Encoding streamEncoding, string urlBase); public Document GetPdfDocumentObjectFromHtmlStream(System.IO.Stream htmlStream, Encoding streamEncoding, string urlBase, string internalLinksDocUrl);
The conversion process and the aspect of the generated PDF document can be configured in many ways. You can set the PDF document page size (A4, A3, etc), orientation (Portrait or Landscape), compression level, encryption and passwords, document info (author, title, subject, etc), add headers and footers with page numbering, etc. The main properties of the converter are listed below.
To set the license key you received after purchase and unlock the product you can use the LicenseKey property. If this property is not set with any value the converter will enter in demo mode.
The PageWidth and PageHeight properties allows you to set the width and height of the virtual browser windows. The web page content is rendered based on the virtual browser width specified as a integer value in pixels. Setting these properties has the same effect as the effect produced when resizing a web page in a browser window to the specified dimensions.
The default value of the PageWidth property is 1024 pixels. The default value of the PageHeight property is 0 pixels which means the height will be automatically determined. These values are producing good results in most of the cases but there are also some situations when you'll have to change these properties. You can also choose to let the converter autodetermine both the width and height of the virtual browser by setting both PageWidth and Page Height properties to 0.
The PdfDocumentOptions property allows you to change the aspect and properties of the rendered PDF document like setting the margins, add header and footer, embed true type fonts, generate a document with selectable texts and images or a document with an embedded image, enable or disable live links, pdf page size and page orientation, compression level, show or hide the headers and footers.
This property exposes an object of type PdfDocumentOptions which is automatically created in the PdfConverter constructor. Therefore you don't have to set this property directly with a value from your code but you'll have to set the properties of the exposed PdfDocumentOptions object.
The main properties of the PdfDocumentOptions class are described later in another section.
The PdfSecurityOptions property allows you to change the permissions of the rendered PDF document like allow or disallow printing, editing, etc and also to set user and owner passwords.
This property exposes an object of type PdfSecurityOptions which is automatically created in the PdfConverter constructor. Therefore you don't have to set this property directly with a value from your code but you'll have to set the properties of the exposed PdfSecurityOptions object.
The main properties of the PdfSecurityOptions class are described later in another section.
The PdfDocumentInfo property allows you to set the rendered PDF description like title, author, subject, keywords, etc.
This property exposes an object of type PdfDocumentInfo which is automatically created in the PdfConverter constructor. Therefore you don't have to set this property directly with a value from your code but you'll have to set the properties of the exposed PdfDocumentInfo object.
The main properties of the PdfDocumentInfo class are described later in another section.
The PdfHeaderOptions and PdfFooterOptions properties allows you to customize the aspect of the headers and footers added to the rendered PDF document. Note that the header and footer are visible in the resulted PDF document only if the corresponding ShowHeader and ShowFooter properties from the PdfDocumentOptions property are true.
These properties expose objects of type PdfHeaderOptions and PdfFooterOptions which are automatically created in the PdfConverter constructor. Therefore you don't have to set this property directly with a value from your code but you'll have to set the properties of the exposed PdfHeaderOptions and PdfFooterOptions object.
The main properties of the PdfHeaderOptions and PdfFooterOptions classes are described later in another section.