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.
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.
Starting with Service Release 1.6.0.8, you can also specify an HTML string directly via the URL
parameter. This is described in Section 13.5 of this chapter.
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", "scale=0.6; hyperlinks=true; drawbackground=true"
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", "scale=0.6; hyperlinks=true; drawbackground=true", 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.
TLS 1.2 Update:
To retrieve URL data, ImportFromUrl internally uses the Microsoft XmlHttp object (residing in the system library msxml3.dll)
which does not support TLS 1.2. Therefore, if a secure URL is protected with TLS 1.2, the error The connection with the server has been terminated
is thrown. As of version 3.4.0.5, ImportFromUrl can instead use the Microsoft WinHttp object (winhttp.dll) which does support TLS 1.2 on Windows 2012+ and Windows 8+.
To switch from XmlHttp to WinHttp, use the parameter WinHttp=true, as follows:
Doc.ImportFromUrl "https://secure.domain.com", "WinHttp=true"
The full list of ImportFromUrl parameters can be found here.
IMPORTANT: 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
Q316451,
"this can result in poor performance due to thread starvation," and may produce
the error exception "
MSXML2::ServerXMLHTTP Error: The request has timed out."