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:
C# |
PdfManager objPdf = new PdfManager();
PdfDocument objDoc = objPdf.CreateDocument();
objDoc.ImportFromUrl( "http://www.persits.com", "scale=0.6; hyperlinks=true; drawbackground=true" );
string strFilename = objDoc.Save( Server.MapPath("importfromurl.pdf"), false );
|
VB.NET |
Dim objPdf As PdfManager = new PdfManager()
Dim objDoc As PdfDocument = objPdf.CreateDocument()
objDoc.ImportFromUrl( "http://www.persits.com", "scale=0.6; hyperlinks=true; drawbackground=true" )
Dim strFilename As string = objDoc.Save( Server.MapPath("importfromurl.pdf"), false )
|
Click on the links below to run this code sample:
http://localhost/asppdf.net/manual_15/15_importfromurl.cs.aspx
http://localhost/asppdf.net/manual_15/15_importfromurl.vb.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:
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
parameters.
The full list of ImportFromUrl parameters can be found here.
Under .NET Core on
Linux, you must explicitly load all TrueType fonts used by your HTML document
(including their bold, italic, and bold/italic versions)
via the method
objDoc.Fonts.LoadFromFile before calling ImportFromUrl. This is because AspPDF.NET does not know
where to find TrueType fonts on Linux. You
must load at least one font,
Times New Roman, as it is the default font.
For example:
PdfFont objFont1 = objDoc.Fonts.LoadFromFile("fonts/times.ttf");
PdfFont objFont2 = objDoc.Fonts.LoadFromFile("fonts/timesbd.ttf");
PdfFont objFont3 = objDoc.Fonts.LoadFromFile("fonts/timesi.ttf");
PdfFont objFont4 = objDoc.Fonts.LoadFromFile("fonts/timesbi.ttf");
...
objDoc.ImportFromUrl(...);
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."