Hi.
I have a simple project replace object html file inside my project folder which i want to convert to pdf and save to inside my folder project
but when i convert using iTextSharp i get error " The document has no pages." when try to
XMLWorkerHelper.GetInstance
public void SaveHtmlAsPdf(string htmlContent, string folderPath , string filename)
{
try
{
// Ensure the folder exists, create if it doesn't
if (!Directory.Exists(folderPath))
{
Directory.CreateDirectory(folderPath);
}
// Combine folder path and filename to get the full file path
string pdfPath = Path.Combine(folderPath, filename);
using (FileStream fs = new FileStream(pdfPath, FileMode.Create))
{
using (Document doc = new Document(PageSize.A4, 50, 50, 50, 50)) // Set page size and margins
{
PdfWriter writer = PdfWriter.GetInstance(doc, fs);
doc.Open();
// Convert HTML to PDF
using (var sr = new StringReader(htmlContent))
{
XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, sr);
}
doc.Close();
}
}
Console.WriteLine("PDF saved successfully at: " + pdfPath);
}
catch (Exception ex)
{
Console.WriteLine("An error occurred while converting HTML to PDF: " + ex.Message);
throw;
}
}