Starting with version 1.6, AspPDF is capable of converting HTML documents to PDF
via PdfDocument's ImportFromUrl method.
This method opens an HTML document from a given URL, splits it into pages and renders it
onto an empty or existing PDF document. The document can then be further edited, if necessary,
and saved to disk, memory or an HTTP stream as usual.
ImportFromUrl's support for various HTML tags and constructs is not quite as extensive
as that of major browsers, but still considerably stronger than the limited
HTML functionality of Canvas.DrawText
available in older version of AspPDF. ImportFromUrl recognizes tables, images, lists, cascading style sheets, etc.
Release note: Initially, HTML to PDF functionality was implemented
via PdfManager's OpenUrl method but starting with Service Release 1.6.0.5, OpenUrl is
replaced by the more versatile ImportFromUrl. The latter supports writing HTML onto existing documents
and is capable of returning debug information. OpenUrl is deprecated and will not be supported
in future releases. As of now, ImportFromUrl is still a work in progress. Use it at your own risk.
ImportFromUrl accepts four parameters, all but the first one optional: the input URL, a parameter
list, and a username/password pair.
The URL parameter can be an HTTP or HTTPS address, such as
http://www.server.com/path/file.html, or a local physical path such as c:\path\file.html.
Note that if you want to open a dynamically generated document such as an .asp or aspx file,
you need to invoke it via HTTP even if this file is local to your own script.
The following simple code snippet creates a PDF document out of the Persits Software site persits.com:
VBScript |
Set Pdf = Server.CreateObject("Persits.Pdf")
Set Doc = Pdf.CreateDocument
Doc.ImportFromUrl "http://www.persits.com"
Filename = Doc.Save( Server.MapPath("importfromurl.pdf"), False )
|
C# |
IPdfManager objPdf = new PdfManager();
IPdfDocument objDoc = objPdf.CreateDocument( Missing.Value );
objDoc.ImportFromUrl( "http://www.persits.com", Missing.Value, Missing.Value, Missing.Value );
String strFilename = objDoc.Save( Server.MapPath("importfromurl.pdf"), false );
|
Click on the links below to run this code sample:
http://localhost/asppdf/manual_13/13_importfromurl.asp
http://localhost/asppdf/manual_13/13_importfromurl.aspx
The ImportFromUrl method's 2nd argument is a PdfParam object or parameter string
specifying additional parameters controlling the HTML to PDF conversion process.
For example, to create a document in a landscape orientation, the Landscape
parameter must be set to true, for example:
Doc.ImportFromUrl "http://www.persits.com", "landscape=true"
When new pages have to be added to the document during the conversion process,
the default page size is U.S. Letter. This can be changed via the
PageWidth and PageHeight parameters.
When rendering HTML content on a page, AspPDF leaves 0.75" margins around the content area.
That can be changed via the LeftMargin, RightMargin, TopMargin and BottomMargin
parameters.
The full list of ImportFromUrl parameters can be found here.