xxxxxxxxxx
using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.tool.xml;
public void ConvertHtmlToPdf(string htmlFilePath, string pdfFilePath)
{
// Create a new PDF document
Document document = new Document();
// Create a PDF writer to write the document to a file
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(pdfFilePath, FileMode.Create));
// Open the document for writing
document.Open();
// Read the HTML file to convert
using (TextReader reader = new StreamReader(htmlFilePath))
{
// Parse the HTML content and write it to the PDF document
XMLWorkerHelper.GetInstance().ParseXHtml(writer, document, reader);
}
// Close the document and writer
document.Close();
writer.Close();
}
xxxxxxxxxx
var htmlContent = String.Format("<body>Hello world: {0}</body>", DateTime.Now);
var pdfBytes = (new NReco.PdfGenerator.HtmlToPdfConverter()).GeneratePdf(htmlContent);
//https://www.nrecosite.com/doc/NReco.PdfGenerator/
xxxxxxxxxx
// PM > Install-Package NReco.PdfGenerator
Create HtmltoPdf(){
if (System.IO.File.Exists("HTMLFile.html"))
{
System.IO.File.Delete("HTMLFile.html");
}
System.IO.File.WriteAllText("HTMLFile.html", html);
var htmlToPdf = new NReco.PdfGenerator.HtmlToPdfConverter();
if (System.IO.File.Exists("export.pdf"))
{
System.IO.File.Delete("export.pdf");
}
htmlToPdf.GeneratePdfFromFile("HTMLFile.html", null, "export.pdf");
}
xxxxxxxxxx
using Syncfusion.HtmlConverter;
using Syncfusion.Pdf;
using System.IO;
namespace HTML_File_To_PDF
{
class Program
{
static void Main(string[] args)
{
//Initialize the HTML to PDF converter with the Blink rendering engine.
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter(HtmlRenderingEngine.Blink);
BlinkConverterSettings blinkConverterSettings = new BlinkConverterSettings();
//Set the BlinkBinaries folder path.
blinkConverterSettings.BlinkPath = @"../../../../../BlinkBinariesWindows/";
//Assign Blink converter settings to HTML converter.
htmlConverter.ConverterSettings = blinkConverterSettings;
//Convert HTML string to PDF.
PdfDocument document = htmlConverter.Convert("<h1>Hello world</h1>","");
FileStream fileStream = new FileStream("Sample.pdf", FileMode.OpenOrCreate, FileAccess.ReadWrite);
//Save and close the PDF document .
document.Save(fileStream);
document.Close(true);
}
}
}