HTML to PDF Using ASP.NET Core in Linux Docker | Syncfusion Blogs
Live Chat Icon For mobile
Live Chat Icon
Popular Categories.NET  (174).NET Core  (29).NET MAUI  (207)Angular  (109)ASP.NET  (51)ASP.NET Core  (82)ASP.NET MVC  (89)Azure  (41)Black Friday Deal  (1)Blazor  (215)BoldSign  (14)DocIO  (24)Essential JS 2  (107)Essential Studio  (200)File Formats  (66)Flutter  (133)JavaScript  (221)Microsoft  (119)PDF  (81)Python  (1)React  (100)Streamlit  (1)Succinctly series  (131)Syncfusion  (915)TypeScript  (33)Uno Platform  (3)UWP  (4)Vue  (45)Webinar  (51)Windows Forms  (61)WinUI  (68)WPF  (159)Xamarin  (161)XlsIO  (36)Other CategoriesBarcode  (5)BI  (29)Bold BI  (8)Bold Reports  (2)Build conference  (8)Business intelligence  (55)Button  (4)C#  (147)Chart  (131)Cloud  (15)Company  (443)Dashboard  (8)Data Science  (3)Data Validation  (8)DataGrid  (63)Development  (628)Doc  (8)DockingManager  (1)eBook  (99)Enterprise  (22)Entity Framework  (5)Essential Tools  (14)Excel  (40)Extensions  (22)File Manager  (7)Gantt  (18)Gauge  (12)Git  (5)Grid  (31)HTML  (13)Installer  (2)Knockout  (2)Language  (1)LINQPad  (1)Linux  (2)M-Commerce  (1)Metro Studio  (11)Mobile  (507)Mobile MVC  (9)OLAP server  (1)Open source  (1)Orubase  (12)Partners  (21)PDF viewer  (43)Performance  (12)PHP  (2)PivotGrid  (4)Predictive Analytics  (6)Report Server  (3)Reporting  (10)Reporting / Back Office  (11)Rich Text Editor  (12)Road Map  (12)Scheduler  (52)Security  (3)SfDataGrid  (9)Silverlight  (21)Sneak Peek  (31)Solution Services  (4)Spreadsheet  (11)SQL  (10)Stock Chart  (1)Surface  (4)Tablets  (5)Theme  (12)Tips and Tricks  (112)UI  (387)Uncategorized  (68)Unix  (2)User interface  (68)Visual State Manager  (2)Visual Studio  (31)Visual Studio Code  (19)Web  (592)What's new  (332)Windows 8  (19)Windows App  (2)Windows Phone  (15)Windows Phone 7  (9)WinRT  (26)

HTML to PDF Conversion Using ASP.NET Core in Linux Docker

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

Tags:

Share this post:

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.

Popular Now

Be the first to get updates

Subscribe RSS feed

Be the first to get updates

Subscribe RSS feed