)
We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to convert HTML to PDF in Linux docker container

Platform: ASP.NET Core |
Control: PDF |
Published Date: May 15, 2018 |
Last Revised Date: April 30, 2019

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 a HTML to PDF in Linux docker container.

Steps to convert HTML to PDF in Linux docker container:

  1. Create a new ASP.NET Core application and enable the docker support with Linux as target OS.

Create a new core project.

Enable docker support with Linux OS.

  1. Install the Syncfusion.HtmlToPdfConverter.QtWebKit.Net.Core NuGet package as  a reference to your .NET Core application from NuGet.org.

Install the HTML to PDF converter NuGet package.

  1. Copy the QtBinariesLinux folder from the HtmltoPdfConverter NuGet package installed location to project folder, which contains the .csproj file.

Copy the QtBinariesLinux folder from package folder.

Paste the QtBinariesLinux folder to the project folder.

  1. Set Copy to output directory as copy if newer for all the files in QtBinariesLinux folder.

Set copy local true to all QtBinariesLinux files.

  1. Include the following snippet in the docker file to install the dependent packages in the docker container.
    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
    

Install the dependencies

  1. Add an Export To PDF button in index.cshtml.
    <div class="btn">
        @{ Html.BeginForm("ExportToPDF", "Home", FormMethod.Post);
            {
                <input type="submit" value="Export To PDF" class=" btn" />
            }}
    </div>
    

view page screenshot

  1. Include the following namespaces and code snippet in controller for converting HTML to PDF.
    // [C# Code]
    using Syncfusion.HtmlConverter;
    using Syncfusion.Pdf;
    using System.IO;
    using Microsoft.AspNetCore.Hosting;
    

 

// [C# Code]
//To get content root path of the project
private readonly IHostingEnvironment _hostingEnvironment;
public HomeController(IHostingEnvironment hostingEnvironment)
{
     _hostingEnvironment = hostingEnvironment;
}
 
public ActionResult ExportToPDF()
{
     //Initialize HTML to PDF converter 
     HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();
 
     WebKitConverterSettings settings = new WebKitConverterSettings();
 
     //Set WebKit path
     settings.WebKitPath = Path.Combine(_hostingEnvironment.ContentRootPath, "QtBinariesLinux");
 
     //Assign WebKit settings to HTML converter
     htmlConverter.ConverterSettings = settings;
 
     //Convert URL to PDF
     PdfDocument document = htmlConverter.Convert("http://www.google.com");
 
     MemoryStream stream = new MemoryStream();
 
     //Save and close the PDF document 
     document.Save(stream);
 
     return File(stream.ToArray(), System.Net.Mime.MediaTypeNames.Application.Pdf, "Sample.pdf");
}
  1. 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 Export to PDF to convert the Google webpage to a PDF.
  2. By executing the program, you will get the PDF document as follows.

output screenshot

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 peruse the documentation, where you can find other WebKitBlink and IE rendering engines supported by Syncfusion .NET PDF library for converting HTML pages to PDF document along with respective customization options and features.

Click here to explore the rich set of Syncfusion Essential PDF features.

An online sample link to convert HTML to PDF.

2X faster development

The ultimate ASP.NET Core UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment
Comments
Mithun
Jan 31, 2020

Some observations:

  1. The base image needs to be "mcr.microsoft.com/dotnet/core/aspnet:2.1.15-bionic". If you use the regular 2.1 tag instead of 2.1.15-bionic then image nodes cannot be printed/exported (please see this forum issue). The full list of tags can be seen here.
  2. Upgrading to dotnetcore 3.1 (with 3.1-bionic tag) does not resolve the issue.
Reply

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon