How to Convert HTML to PDF in ASP.NET Core Linux?
The Syncfusion HTML to PDF converter is a .NET library for converting webpages, SVG, MHTML, and HTML to PDF using C#. It is reliable and accurate. The result preserves all graphics, images, text, fonts, and the layout of the original HTML document or webpage. Using this library, you can convert HTML to PDF in Linux.
Steps to convert HTML to PDF programmatically in .NET Core application on Linux:
1. For converting HTML to PDF in Linux, the following prerequisite packages should be installed on the Linux machine. To do that, enter the following commands in the terminal:
C#:
$ sudo apt-get update
$ sudo apt-get install -yq --no-install-recommends \
libasound2 libatk1.0-0 libc6 libcairo2 libcups2 libdbus-1-3 \
libexpat1 libfontconfig1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 libglib2.0-0 libgtk-3-0 libnspr4 \
libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 \
libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 \
libnss3 libgbm1
2. Execute the following command in the Linux terminal to create a new .NET Core Console application.
C#:
dotnet new console
3. Install the Syncfusion.HtmlToPdfConverter.Net.Linux NuGet package as a reference to your project from NuGet.org by executing the following command:
C#:
dotnet add package Syncfusion.HtmlToPdfConverter.Net.Linux -v 20.3.0.47 -s https://www.nuget.org/
4. Include the following namespaces and code snippet in the Program.cs file.
C#:
// [C# Code] using Syncfusion.HtmlConverter; using Syncfusion.Pdf; using System.IO;
C#:
// [C# Code]
// Initialize HTML to PDF converter
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();
BlinkConverterSettings settings = new BlinkConverterSettings();
// Set command line arguments to run without sandbox.
settings.CommandLineArguments.Add("--no-sandbox");
settings.CommandLineArguments.Add("--disable-setuid-sandbox");
// Assign Blink settings to HTML converter
htmlConverter.ConverterSettings = settings;
// Convert URL to PDF
PdfDocument document = htmlConverter.Convert("https://www.syncfusion.com");
FileStream fileStream = new FileStream("Sample.pdf", FileMode.CreateNew, FileAccess.ReadWrite);
// Save and close the PDF document
document.Save(fileStream);
document.Close(true);
5. Execute the following command to restore the NuGet packages.
C#:
dotnet restore
6. Execute the following command in the terminal to run the application.
C#:
dotnet run
By executing the program, you will get the PDF document as follows. The output will be saved in parallel to program.cs file.
You can download a complete working sample from Linux-HTML-to-PDF-Demo.zip.
Take a moment to peruse the documentation for Converting HTML to PDF, where you will find various options for URL to PDF, HTML string to PDF, and hyperlinks.
I hope you enjoyed learning about how to convert HTML to PDF in ASP.NET Core Linux.
You can refer to our ASP.NET Core PDF feature tour feature tour page to learn about its other groundbreaking features and documentation, and how to quickly get started with configuration specifications. You can also explore our ASP.NET Core PDF Example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums or feedback portal. We are always happy to assist you!