Converting HTML to PDF in a Windows Docker Container using ASP.NET Core
The Syncfusion® HTML to PDF converter for .NET Core is a powerful library for transforming webpages, SVG, MHTML, and HTML content into PDF documents. By employing the Linux HTML converter within a Windows Docker environment, developers can achieve reliable HTML to PDF conversions seamlessly. This guide provides a step-by-step approach to performing these conversions in a Windows Docker container.
Prerequisites:
- Docker for Windows: Download Docker for Windows from docker.com.
- Switching Containers: Use the Docker for Windows menu to toggle between Windows and Linux containers as needed, selecting "Switch to Windows containers" for this specific guide.
Steps to convert HTML to PDF in windows docker container programmatically:
1.Create an ASP.NET Core MVC Application: Initiate a
project using ASP.NET Core Model-View-Controller pattern.
2. Enable Docker support and select Windows as the target
operating system.
3. Install Syncfusion Package: Add Syncfusion.HtmlToPdfConverter.Net.Windows from NuGet as a project reference.
4. Add PDF Conversion Functionality: Insert the following element in index.cshtml.
<h2>Click the button to generate PDF</h2>
@using (Html.BeginForm("ExportToPDF", "Home", FormMethod.Post))
{
<input type="submit" value="Export to PDF" />
}
5. Implement the Conversion Logic in the Controller: Include the required namespaces:
C#
using Syncfusion.Pdf;
using Syncfusion.HtmlConverter;
6. Add the conversion code snippet in HomeController.cs.
C#
public IActionResult ExportToPDF()
{
// Initialize the HTML to PDF converter
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();
// Convert the specified URL to a PDF document
PdfDocument document = htmlConverter.Convert("https://www.syncfusion.com");
// Create a memory stream to hold the PDF data
MemoryStream stream = new MemoryStream();
// Save the PDF document to the memory stream
document.Save(stream);
// Return the PDF file as a downloadable response
return File(stream.ToArray(), System.Net.Mime.MediaTypeNames.Application.Pdf, "Sample.pdf");
}
7.Build and Run the Application: Deploy the application in Docker, which will open the webpage in a browser. Use the "Export to PDF" button to perform the conversion.
Take a moment to peruse the documentation for converting HTML to PDF, where you will find other options like HTML string to PDF, partial webpage to PDF, HTML to single PDF page, and HTML to PDF conversion using IE Rendering with code examples.
A complete work sample can be downloaded from HTMLToPDF_WindowsDocker.zip
Conclusion
I hope you enjoyed learning about how to convert HTML to PDF in windows 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.