Chromium Rendering Engine (CEF) | |
Introduced in v26.2 of SelectPdf. The Chromium rendering engine is based on the Chromium Embedded Framework (CEF). It is delivered as a separate Chromium.Windows NuGet package that complements the main SelectPdf package. Pick it when the input HTML relies on modern HTML5/CSS3 or modern JavaScript (ES2020+) that the WebKit engines do not render correctly, or when the application needs to keep up with a current Chromium release. The same engine also powers SelectPdf.Universal, the cross-platform edition of SelectPdf (see Cross-Platform Library) - applications that standardize on the Chromium engine get consistent rendering output if they later move to Linux or Docker.
The Chromium engine is available for every .NET target supported by SelectPdf - .NET Framework 2.0, 4.0, 4.6.1, 4.7.2, and .NET Core 2.0 or later through .NET Standard 2.0 (including .NET 5 - .NET 10). It also runs on Microsoft Azure Web Apps (Basic plan or above) without additional configuration - see Deployment to Microsoft Azure.
The Chromium.Windows package ships a Windows runtime, so the Chromium engine - like the rest of this library - requires Windows. The same engine is available on Linux and macOS, and inside Docker containers, through the native packages of SelectPdf.Universal, where it is the only rendering engine and no engine selection is needed. See Cross-Platform Library and Deployment to Docker.
Pick the Chromium.Windows package that matches the main SelectPdf package already referenced in the project. The Chromium.Windows package installs the Chromium runtime alongside the base library - the main package is still required and is declared as a direct dependency of the Chromium.Windows package.
Companion package for Select.Pdf (.NET Framework 2.0 and .NET 4.0 - 4.5, AnyCPU). Ships the x86 Chromium runtime.
Companion package for Select.Pdf.x64 (.NET Framework 2.0 and .NET 4.0 - 4.5, x64). Ships the x64 Chromium runtime.
https://www.nuget.org/packages/Select.Pdf.Chromium.Windows.x64/
Companion package for Select.Pdf.NetCore (.NET Framework 4.6.1 and 4.7.2, .NET Core 2.0+ through .NET Standard 2.0, modern .NET 5 - .NET 10, AnyCPU). Ships the x86 Chromium runtime.
https://www.nuget.org/packages/Select.Pdf.NetCore.Chromium.Windows/
Companion package for Select.Pdf.NetCore.x64 (.NET Framework 4.6.1 and 4.7.2, .NET Core 2.0+ through .NET Standard 2.0, modern .NET 5 - .NET 10, x64). Ships the x64 Chromium runtime.
https://www.nuget.org/packages/Select.Pdf.NetCore.Chromium.Windows.x64/
The .Windows suffix indicates that the Chromium runtime bundled with the package is the Windows build. The same Chromium engine is planned for other operating systems in future releases.
The Chromium.Windows packages are optional. Only install them when the application actually needs the Chromium engine. The main SelectPdf packages still provide the WebKit, WebKit Restricted, and (on supported targets) Blink engines without them. |
Once the Chromium.Windows package is installed, select the engine by setting RenderingEngine to RenderingEngine.Chromium:
HtmlToPdf converter = new HtmlToPdf(); // select the Chromium rendering engine converter.Options.RenderingEngine = RenderingEngine.Chromium; // standard conversion options can still be used converter.Options.PdfPageSize = PdfPageSize.A4; converter.Options.PdfPageOrientation = PdfPageOrientation.Portrait; converter.Options.MarginTop = 20; converter.Options.MarginBottom = 20; PdfDocument doc = converter.ConvertUrl("https://selectpdf.com"); try { doc.Save("ChromiumSample.pdf"); } finally { doc.Close(); }
The Chromium engine can also be used with HtmlToImage by setting RenderingEngine to RenderingEngine.Chromium:
The Chromium engine (like the Blink engine) can convert pages behind an HTML login form by populating LoginOptions on the converter options before conversion. LoginOptions takes the login page URL, the CSS selectors of the username, password and submit button fields, the credentials to type in, and optional delays before typing and after submitting.
HtmlToPdf converter = new HtmlToPdf(); converter.Options.RenderingEngine = RenderingEngine.Chromium; converter.Options.LoginOptions = new LoginOptions { LoginPage = "https://example.com/login", UsernameFieldSelector = "#username", PasswordFieldSelector = "#password", SubmitButtonSelector = "#submit", Username = "user@example.com", Password = "secret", DelayBefore = 100, DelayAfter = 500 }; PdfDocument doc = converter.ConvertUrl("https://example.com/protected"); try { doc.Save("ProtectedPage.pdf"); } finally { doc.Close(); }
For pages whose content is WebGL (map or 3D chart libraries, for example) the Chromium and Blink engines have to initialize a graphics stack on every conversion. Pages that do not use WebGL would pay for that initialization without getting anything from it, so it is skipped by default: the conversion is noticeably faster, and everything else, 2D canvas included, renders exactly the same.
Set WebGlEnabled to true when the converted pages draw with WebGL. With the property left at false no WebGL context can be created, so a page whose content is WebGL renders empty.
The default value is False. It is honored by the Chromium and Blink rendering engines - the WebKit engines ignore it, and so does a Blink browser connected through ExternalBrowserEndpoint - and is available starting with v26.4. The same property is also available on HtmlToImage, PdfHtmlElement and PdfHtmlSection.
HtmlToPdf converter = new HtmlToPdf(); converter.Options.RenderingEngine = RenderingEngine.Chromium; // the pages being converted draw with WebGL converter.Options.WebGlEnabled = true; PdfDocument doc = converter.ConvertUrl("https://selectpdf.com"); doc.Save("ChromiumSample.pdf"); doc.Close();
The Chromium engine supports all SelectPdf HTML-to-PDF features available with the default WebKit engine - including automatic bookmark generation, POST data, web elements location retrieval, table of contents generation, and partial web page rendering (converting only the section identified by VisibleWebElementId, supported with this engine since v26.4).
Partial web page rendering with Chromium produces the same output as with the WebKit engines: the selected section is rendered alone, starting at the top left corner of the first page, and the rest of the page is left empty. The pdf page size is the one the converter is configured with, and a section taller than one page continues on the following pages. If the page has no element with the specified id, or that element has no visible size, the conversion fails with an error instead of silently returning the whole page - the WebKit engines return the whole page in that situation.
The same applies to the HTML elements added to a pdf document with PdfHtmlElement, which accept the section id through VisibleWebElementId. See Partial Page Conversion.