Aspose.CSS for .NET: Browser-grade CSS Rendering to PDF and PNG

Aspose.CSS for .NET Product Logo

Aspose.CSS for .NET brings browser-grade CSS layout to PDF documents and PNG images from .NET applications, inside your own process and without launching or automating a web browser. Design in HTML and CSS, check it in the browser, and ship the same layout from your code; CSS fidelity is measured against Chromium and published with each release.

C# HTML Rendering Topics

Scope and Compatibility

The first release renders HTML and CSS to PDF (CSS @page size, margins and orientation, page margin boxes with page numbers, page breaks, widows and orphans) and to PNG, JPEG and WebP images. JavaScript is off by default; turn it on with Configuration.Scripting.Enabled, and document scripts run before layout within time, statement and memory budgets. Running headers and running elements, tagged PDF, PDF/UA and PDF/A are not part of the first release; see Scope at Launch and the Chromium compatibility report on the family page.

Install Aspose.CSS for .NET

Add the Aspose.CSS NuGet package to your project:

1dotnet add package Aspose.CSS

In the Visual Studio Package Manager Console, run Install-Package Aspose.CSS. The package targets .NET 10 (net10.0) and .NET Standard 2.0 (netstandard2.0, for .NET Framework and the other .NET Standard 2.0 platforms). The DLLs and an MSI installer are on the downloads page.

On Linux, nothing is needed beyond the .NET runtime’s own prerequisites, so a minimal image such as the official .NET runtime image converts as it is. Text is drawn from the fonts you add and the fallback fonts embedded in the package; the machine’s installed fonts are the last fallback, and FontSettings.UseSystemFonts = false turns that off for output that does not depend on the machine.

Convert HTML to PDF in C#

The example converts an HTML file to PDF and prints the number of pages:

 1using System;
 2using Aspose.Css;
 3using Aspose.Css.Saving;
 4
 5class Example
 6{
 7    static void Main()
 8    {
 9        var result = Converter.ConvertHTML("invoice.html", new PdfSaveOptions(), "invoice.pdf");
10        Console.WriteLine(result.PageCount);
11    }
12}

Converter.ConvertHTML also takes a markup string or a stream with a base URL, and writes to a file or a stream.

Render Once, Save PDF and PNG

Load and lay out a document once with RenderDocument, then save it to several outputs:

 1using System.IO;
 2using Aspose.Css;
 3using Aspose.Css.Saving;
 4
 5class Example
 6{
 7    static void Main()
 8    {
 9        using var document = RenderDocument.Load(HtmlSource.FromFile("report.html"), new Configuration());
10        using var pdf = File.Create("report.pdf");
11        using var png = File.Create("preview.png");
12        document.Save(new PdfSaveOptions(), pdf);
13        document.Save(new ImageSaveOptions { Mode = ImageMode.FullContent }, png);
14    }
15}

Licensing

Without a license the library runs in evaluation mode, with evaluation limits. Apply a license once per process:

1new Aspose.Css.License().SetLicense("Aspose.CSS.lic");

The argument is a full or short file name or an embedded resource name; you can also set the ASPOSE_CSS_LICENSE environment variable to the license file’s path. Licenses are available at purchase.aspose.com.

Aspose.CSS for .NET Resources