HTML to PDF Using ASP.NET Core in Linux Docker | Syncfusion Blogs
Detailed Blog page Skeleton loader

Docker is a lightweight and very popular container platform that packages an application with everything it needs to run, with the necessary code, system libraries, fonts, other supported libraries, or files. 

Visual Studio 2017 and Docker support building and running .NET applications using a Windows docker container and .NET Core application on Linux docker containers.

Syncfusion HTML to PDF converter can be easily integrated into any application with five simple lines of code. Using this library, you can turn an entire webpage into a PDF file. Or just convert a portion of the page. Your PDF will just look like the webpage you converted—it preserves all graphics, images, text, fonts, links, and the layout of the original HTML document or webpage.

Furthermore, Syncfusion HTML to PDF converter is available in Windows Forms, WPF, ASP.NET, ASP.NET MVC, and ASP.NET Core. In this post, I use ASP.NET Core in a Linux Docker container.

Seamlessly convert webpages and other complex HTML content to PDF with a comprehensive API set.

Setup

  • First, download HTML to PDF Linux from our download page. For this demo, I downloaded HTML to PDF for Linux. But you can just as easily download any other installer, like Mac and Windows.
Download HTML to PDF for Linux
Already installed Docker for Windows? Great! Now we need to create a new ASP.NET Core web application.
Create ASP.NET Core Project
 
  • Make sure to select ASP.NET Core 2.0, enable Docker support, and target the Linux OS.
Enable Docker in Visual Studio
  • Add the Syncfusion.HtmlConverter.NETStandard NuGet packages as a reference to your .NET Core project from the Syncfusion ASP.NET Core NuGet feed. For more information about adding a NuGet feed in Visual Studio and installing the NuGet package, refer to our documentation. You can also install this package from the NuGet package manager console by executing the following command.
> Install-Package Syncfusion.HtmlConverter.NETStandard -source https://nuget.syncfusion.com/nuget_aspnetcore/nuget/getsyncfusionpackages/aspnetcore
  • Extract the downloaded .zip file and refer the QtBinaries physical path to the WebKitPath from the WebKitConverterSettings class.

Can you show us some code?

Looking at some code is always a good idea to give more understanding.
//Initialize HTML to PDF converter
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();
//Set WebKit path
htmlConverter.ConverterSettings.WebKitPath = Path.Combine(_hostingEnvironment.ContentRootPath, "QtBinaries");
//Convert URL to PDF
PdfDocument document = htmlConverter.Convert("https://www.google.com");
MemoryStream ms = new MemoryStream();
//Save and close the PDF document
document.Save(ms);
document.Close(true);

Include the below snippet in the docker file to install the dependent packages in the docker container.

FROM microsoft/aspnetcore:2.0 AS base
RUN apt-get update && apt-get -y install xvfb && apt-get -y install fontconfig && apt-get -y install libssl1.0-dev && apt-get -y install libx11-dev libx11-xcb-dev libxcb-icccm4-dev libxcb-image0-dev libxcb-keysyms1-dev libxcb-randr0-dev libxcb-render-util0-dev libxcb-render0-dev libxcb-shm0-dev libxcb-util0-dev libxcb-xfixes0-dev libxcb-xkb-dev libxcb1-dev libxfixes-dev libxrandr-dev libxrender-dev
WORKDIR /app
EXPOSE 80

Build and run the sample in the docker, it will pull the Linux docker image from the docker hub and run the project. Now, the webpage will open in the browser. Click Convert to PDF to convert the Google webpage to a PDF.

This sample is made as simple as possible for converting a webpage to PDF.

Join thousands of developers who rely on Syncfusion for their PDF needs. Experience the difference today!

Conclusion

Whether you need to convert a URL, webpage, or HTML string to PDF in the docker containers, the Syncfusion HTML to PDF converter libraries make work short for .NET developers. With our libraries, we can even convert SVG and MHTML to PDF. There are advanced features such as form authentication, header, footer, table of contents, and automatic bookmark hierarchy based on heading style. Take a moment to peruse the documentation, where you’ll find other options and features, all with accompanying code examples.

As always, your feedback is highly appreciated, so please do reach out in our comments. You can also contact us through our support forum or Direct-Trac. We are happy to assist you!

If you’re already a Syncfusion user, you can download the product setup on Direct-Trac. If you’re not yet a Syncfusion user, you can download a free, 30-day trial on our website.

If you like this blog post, we think you’ll also like the following free e-books:

PDF Succinctly
Linux Succinctly
ASP.NET Core Succinctly
.NET Core Succinctly

Be the first to get updates

George Livingston

Meet the Author

George Livingston

George Livingston is the Product Manager for PDF at Syncfusion Software. He is passionate about web technologies. He loves creating productive software tools and shooting photographs.

Comments (1)

This is not working with the .net core 3.1 version. Showing the below error.
Package libssl1.0-dev is not available, but is referred to by another package. E: Package ‘libssl1.0-dev’ has no installation candidate

Comments are closed.