How to perform HTML to PDF conversion with external CSS in ASP.NET Core HTML file?
The Syncfusion® Essential PDF is a feature-rich and high performance.NET PDF library used to create, read, and edit PDF documents programmatically without Adobe dependencies. Using this library, you can perform HTML to PDF conversion with external CSS in HTML file using C#.
Steps to perform HTML to PDF conversion with external CSS in HTML file programmatically:
- Create a new console application project.
- Install the Syncfusion.HtmlToPdfConverter.Net.Windows NuGet package as a reference to your console application from Nuget.org.
- Include the following namespaces in the Program.cs file.
C#
using Syncfusion.HtmlConverter;
using Syncfusion.Pdf;
- Use the following code sample in Program.cs to perform HTML to PDF conversion with external CSS in HTML file.
C#
//Initialize HTML to PDF converter
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();
BlinkConverterSettings blinkConverterSettings = new BlinkConverterSettings();
blinkConverterSettings.Scale = 1.0f;
blinkConverterSettings.MediaType = MediaType.Print;
//Assign Blink converter settings to HTML converter
htmlConverter.ConverterSettings = blinkConverterSettings;
//Get HTML string from file
string html = File.ReadAllText(Path.GetFullPath("../../../Data/HtmlSample.html"));
//Get the resources path
string baseUrl = Path.GetFullPath("../../../Css/");
//Convert URL to PDF document
PdfDocument document = htmlConverter.Convert(html, baseUrl);
if (File.Exists("HTML-to-PDF.pdf"))
{
File.Delete("HTML-to-PDF.pdf");
}
//Create file stream
MemoryStream stream = new MemoryStream();
//Save the document into stream
document.Save(stream);
//If the position is not set to '0' then the PDF will be empty.
stream.Position = 0;
File.WriteAllBytes("HTML-to-PDF.pdf", stream.ToArray());
//Close the document
document.Close();
stream.Dispose();
A complete working sample can be downloaded from HTML_to_PDF_with_external_CSS.zip
By executing the program, you will get the PDF document as follows.
Take a moment to peruse the documentation for Converting HTML to PDF, where you will find various options for URL to PDF, HTML string to PDF, and Hyperlinks.
Conclusion
I hope you enjoyed learning about how to perform HTML to PDF conversion with external CSS in ASP.NET Core HTML file.
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 forums, Direct-Trac, or feedback portal. We are always happy to assist you!