How to Save a Webpage as a PDF

Why and how to download a webpage as PDF

Converting online content into high-quality, print-ready PDF documents is a common task for developers and users who need to archive, share, or analyze web content offline. If you’re looking to turn a webpage into a PDF or save HTML as PDF, Aspose.HTML for .NET offers a simple and reliable solution. Using this API, you can easily download webpage as PDF, preserving the original layout, images, and styles, ensuring the output matches the live site accurately.

This article explains how to load a webpage from a URL and save it as a PDF using just a few lines of C# code. Before you begin, make sure you have Aspose.HTML for .NET installed. You can add it from NuGet:

1Install-Package Aspose.HTML

Convert a webpage to PDF – C# Example

  1. Load an HTML document. The HTMLDocument class loads and parses the webpage content directly from the provided URL.
  2. Set rendering options. The PdfRenderingOptions allows you to configure page size, margins, and other PDF parameters. In this example, we use a page size of 1920×1080 pixels, which matches a standard Full HD screen resolution and ensures the webpage is rendered exactly as it would appear in a desktop browser window.
  3. Initialize the rendering device. The PdfDevice defines the output target - in this case, a PDF file path.
  4. Render the content. The HtmlRenderer takes the document and outputs it to the device, effectively converting the HTML page into a PDF.

Below is the complete C# example showing how to convert a webpage to PDF using Aspose.HTML for .NET:

1using System;
2using System.IO;
3using Aspose.Html;
4using Aspose.Html.Rendering;
5using Aspose.Html.Rendering.Pdf;
6using Aspose.Html.Drawing;
 1// How to save a website as a pdf using C#
 2
 3// Set the target webpage URL
 4string url = "https://docs.aspose.com/html/";
 5
 6// Define the output path (OutputDir is assumed to exist)
 7string outputPath = Path.Combine(OutputDir, "website.pdf");
 8
 9// Load the HTML document from the specified URL
10HTMLDocument document = new HTMLDocument(url);
11
12// Configure PDF rendering options
13PdfRenderingOptions options = new PdfRenderingOptions();
14options.PageSetup.AnyPage = new Page(new Size(1920, 1080));            
15
16// Create a PDF rendering device with the defined options and output file
17PdfDevice device = new PdfDevice(options, outputPath);
18
19// Initialize the HTML renderer
20HtmlRenderer renderer = new HtmlRenderer();
21
22// Render the document to the specified device
23renderer.Render(device, document);
24
25// Dispose of resources
26renderer.Dispose();
27device.Dispose();
28document.Dispose();

Frequently Asked Questions

1. How do I control page size or orientation?

Use the PageSetup property of the PdfRenderingOptions object. For example:

1options.PageSetup.AnyPage = new Page(new Size(1920, 1080));

2. What is the default unit used in the Size class?

By default, Size values ​​are defined in pixels px. This corresponds to standard screen display and ensures that the web page appears as it does in the browser viewport.

3. How do I set the page size in millimeters or inches? To work with physical dimensions (for printing or precise layout), use the Length type and specify the LengthUnit:

1// Set page size corresponding to a 1920×1080 px screen in millimeters
2options.PageSetup.AnyPage = new Page(new Size(Length.FromMillimeters(508), Length.FromMillimeters(286)));

4. Does Aspose.HTML load all linked resources?

Yes, by default, it loads images, CSS, and other external resources referenced by the page to ensure the rendered PDF matches the original.

5. How to render long web content?

Aspose.HTML automatically breaks long content into pages based on the page size. You can customize this behavior using CSS page rules or PageSetup.

Conclusion

With just a few lines of C# code, you can easily convert any live website into a high-quality, print-ready PDF file quickly, consistently, and professionally! Aspose.HTML for .NET automatically processes complex HTML, CSS, and embedded resources to ensure the output PDF matches the source web page. For optimal results, follow these recommendations:

Text “HTML Web Applications”

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.