AspPDF.NET 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.
ImportFromUrl recognizes tables, images, lists, cascading style sheets, etc.
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.
You can also specify an HTML string directly via the URL
parameter. This is described in Section 15.5 of this chapter.
The following simple code snippet creates a PDF document out of the Persits Software site persits.com:
PdfManager objPdf = new PdfManager();
PdfDocument objDoc = objPdf.CreateDocument();
objDoc.ImportFromUrl( "http://www.persits.com" );
string strFilename = objDoc.Save( Server.MapPath("importfromurl.pdf"), false );
Dim objPdf As PdfManager = new PdfManager()
Dim objDoc As PdfDocument = objPdf.CreateDocument()
objDoc.ImportFromUrl( "http://www.persits.com" )
Dim strFilename As string = objDoc.Save( Server.MapPath("importfromurl.pdf"), false )
Click on the links below to run this code sample:
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:
objDoc.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.NET leaves 0.75" margins around the content area.
That can be changed via the LeftMargin, RightMargin, TopMargin and BottomMargin
The full list of ImportFromUrl parameters can be found here.
Avoid calling ImportFromUrl
on a URL located in the same virtual directory as the script
that makes the call to ImportFromUrl
. According to Microsoft KB article
"this can result in poor performance due to thread starvation," and may produce
the error exception "MSXML2::ServerXMLHTTP Error: The request has timed out.