Articles in this section
Category / Section

How to convert HTML to PDF in Linux docker container?

5 mins read

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 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 new core MVC project in ASP.NET Core PDF

Select NET Core 3.1 framework in ASP.NET Core PDF

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

Install latest Linux package in ASP.NET Core PDF

 

 

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

 

Install the dependent packages in the docker container in ASP.NET Core PDF

 

  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>
    

Add an Export To PDF button in index.cshtml in ASP.NET Core PDF

  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;
    

 

// [C# Code]
public ActionResult ExportToPDF()
{
      //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");
 
     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 Syncfusion webpage to a PDF.

Click Export to PDF to convert the Syncfusion webpage to a PDF in ASP.NET Core PDF

  1. By executing the program, you will get the PDF document as follows.

Output document in ASP.NET Core PDF

 

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 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.


Conclusion

I hope you enjoyed learning about how to convert HTML to PDF in Linux docker container ASP.NET 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 forumsDirect-Trac, or feedback portal. We are always happy to assist you!


Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments
Please sign in to leave a comment
Access denied
Access denied