DsPdf library along with DsHtml library, lets you easily render HTML content to PDF document. With a utility library like DsHtml, you can convert HTML files like invoices and reports to PDF documents and print them without worrying about disarranged layouts, styles or formats. You can also add HTML content to PDF documents.
DsHtml is based on the industry standard Chrome web browser engine working in headless mode, offering advantage of rendering HTML to PDF on any platform - Windows, Linux and macOS. It doesn’t matter whether your .NET application is built for x64, x86 or AnyCPU platform target. The browser is always working in a separate process.
The DS.Documents.Html package contains the following namespaces:
Refer the steps below to install DsHtml package in your project:
Open Visual Studio and create a .Net Core Console application.
Right-click Dependencies and select Manage NuGet Packages.
With the Package source set to Nuget website, search for DS.Documents.Pdf under the Browse tab and click Install.
Similarly, install DS.Documents.Html.
Once, the DsHtml package has been installed successfully, add the namespace in Program.cs file.
C# |
Copy Code
|
---|---|
using GrapeCity.Documents.Html; using GrapeCity.Documents.Pdf; using GrapeCity.Documents.Drawing; |
Apply DsPdf license to GcHtmlBrowser class of DsHtml library to convert HTML to PDF. Without proper license, the count is limited to only 5 PDF conversions. The license can be applied in one of the following ways as shown below:
Write the sample code.
DsHtml can render HTML webpage to PDF document. DsPdf provides the PdfOptions class and RenderToPdf method of GcHtmlBrowser class to render HTML files to PDF documents.
To render the HTML webpage to a PDF document, follow the steps below:
C# |
Copy Code
|
---|---|
// Get a temporary file where the web page will be rendered: var tmp = Path.GetTempFileName(); // The Uri of the web page to render: var uri = new Uri("http://www.google.com"); // Create an instance of GcHtmlBrowser that is used to render HTML: var browserPath = BrowserFetcher.GetSystemChromePath(); using var browser = new GcHtmlBrowser(browserPath); // Create an HtmlPage instance rendering the source Uri: using var htmlPage = browser.NewPage(uri); // Render the source Web page to the temporary file: htmlPage.SaveAsPdf(tmp); // Copy the created PDF from the temp file to target stream: using (var ts = File.OpenRead(tmp)) ts.CopyTo(stream); // Clean up: File.Delete(tmp); |
The snapshot of the resulting PDF document is depicted below:
You can add an HTML page or string to a PDF document using the DrawHtml method of GcPdfGraphicsExt class.
To add an HTML formatted string to a PDF document, follow the steps below:
C# |
Copy Code
|
---|---|
// HTML code that represents the content to render: var html = "<!DOCTYPE html>" + "<html>" + "<head>" + "<style>" + "span.bold {" + "font-weight: bold;" + "}" + "p.round {" + "font: 36px arial, sans-serif;" + "color: DarkSlateBlue;" + "border: 4px solid SlateBlue;" + "border-radius: 16px;" + "padding: 3px 5px 3px 5px;" + "text-shadow: 3px 2px LightSkyBlue;" + "}" + "</style>" + "</head>" + "<body>" + "<p class='round'>Hello, World, from <span class='bold'>DsHtml</span>!</p>" + "</body>" + "</html>"; // Create a new PDF document, add a page, get graphics to draw on: var doc = new GcPdfDocument(); var page = doc.NewPage(); var g = page.Graphics; try { // Create an instance of GcHtmlBrowser that is used to render HTML: var browserPath = BrowserFetcher.GetSystemChromePath(); using var browser = new GcHtmlBrowser(browserPath); // Render HTML. var ok = g.DrawHtml(browser, html, 72, 72, new HtmlToPdfFormat(false) { MaxPageWidth = 6.5f, MaxPageHeight = 9f }, out SizeF size); } catch (Exception ex) { throw new Exception($"Error:\n{ex.Message}"); } // Done: doc.Save(stream); |
The resulting image is shown below:
For more information on rendering HTML to PDF using DsPdf, see DsPdf sample browser.
If your application uses obsolete GcHtmlRenderer class to convert the HTML pages or content to a pdf format, you can use following steps to quickly update to the new GcHtmlBrowser class which does not depend on a custom build of Chromium and does not require GPL or LGPL licenses.
C# |
Copy Code
|
---|---|
GcHtmlRenderer.SetGcPdfLicenseKey(key); -> GcHtmlBrowser.SetGcPdfLicenseKey(key); |
C# |
Copy Code
|
---|---|
var path = BrowserFetcher.GetSystemChromePath();
|
C# |
Copy Code
|
---|---|
var tp = Path.GetTempPath(); var bf = new BrowserFetcher() { DestinationFolder = Path.Combine(tp, ".gc-chromium") }; var path = bf.GetDownloadedPath(); |
C# |
Copy Code
|
---|---|
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) return new GcHtmlBrowser(path, new LaunchOptions { RunWithNoSandbox = true }); else return new GcHtmlBrowser(path) |
C# |
Copy Code
|
---|---|
g.DrawHtml(html, ...); -> g.DrawHtml(browser, html, ...); |
C# |
Copy Code
|
---|---|
using var re = new GcHtmlRenderer(uri); ... re.RenderToPdf(file, new PdfSettings() {…}); |
C# |
Copy Code
|
---|---|
// Create an HtmlPage from the URI // (DefaultBackgroundColor and WindowSize options from Pdf/Jpeg/PngSettings // have moved to PageOptions, while some other options are now in LaunchOptions): using var htmlPage = browser.NewPage(uri, new PageOptions() { WindowSize = pixelSize;… }); ... htmlPage.SaveAsPdf(file, new PdfOptions() {...}); |