How to Convert HTML to PDF in a Linux Docker Container using ASP.NET Core
The Syncfusion® HTML to PDF converter is a .NET PDF 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 docker container.
Steps to convert HTML to PDF in Linux docker container:
1.Create an ASP.NET Core MVC Application: Initiate a
project using ASP.NET Core Model-View-Controller pattern.
Enable Docker support and select Linux as the target operating system.
2. Install Syncfusion Package: Add Syncfusion.HtmlToPdfConverter.Net.Linux from NuGet as a project reference.
3.Configure the Docker file: Include the following snippet in your Docker file to ensure all necessary dependencies are installed
RUN apt-get update && \
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
4.Add Export to PDF Button: In your index.cshtml, add an "Export To PDF" button using the following code
<div class="btn">
@{ Html.BeginForm("ExportToPDF", "Home", FormMethod.Post);
{
<input type="submit" value="Export To PDF" class=" btn" />
}}
</div>
5. Implement Conversion Logic in Controller: Use the Syncfusion library in your controller to handle the conversion
using Syncfusion.HtmlConverter;
using Syncfusion.Pdf;
using System.IO;
public ActionResult ExportToPDF()
{
// Initialize the HTML to PDF converter
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();
// Set Blink rendering engine settings
BlinkConverterSettings settings = new BlinkConverterSettings();
// Add command-line arguments to support conversion in sandboxed environments (Linux Docker, Azure, etc.)
settings.CommandLineArguments.Add("--no-sandbox");
settings.CommandLineArguments.Add("--disable-setuid-sandbox");
// Assign the settings to the converter
htmlConverter.ConverterSettings = settings;
// Convert the webpage URL to a PDF document
PdfDocument document = htmlConverter.Convert("https://www.syncfusion.com");
// Save the PDF document to a memory stream
MemoryStream stream = new MemoryStream();
document.Save(stream);
// Return the PDF file as a downloadable response
return File(stream.ToArray(), System.Net.Mime.MediaTypeNames.Application.Pdf, "Sample.pdf");
}
6. Run your Project: Build and execute your application within the Docker container. The setup will automatically pull the necessary Linux Docker image to enable the functionality.
By executing the program, the output PDF document will be generated as shown below:
A complete work sample for converting a HTML to PDF in Linux docker container can be downloaded from HTMLtoPDF_LinuxDocker.zip.
Take a moment to explore the documentation, where you can learn how to convert HTML pages to PDF documents, along with various customization options and features.
Conclusion
I hope you enjoyed learning about how to convert HTML to PDF in Linux docker container in ASP.NET Core PDF.
You can refer to our ASP.NET Core PDF feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for 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, Direct-Trac, or feedback portal. We are always happy to assist you!