Hi,
I need some sample examples regarding convertions of html to pdf,i tried with many examples that one one is working for form type and another one is working with file type.Essential objects is working for both but while converting to pdf some contents in the html are missings can i know the reason. this is my code
//string Url = "https://v16smoke.smartappbeta.com/enterprisedesktop/DocumentItemDetails.aspx?smartItemId=530085&sessionid=b4ff4d2474b74e23b946cba377c4e6db&pageMode=view";
//HtmlToPdfOptions pdfOptions = new HtmlToPdfOptions();
//pdfOptions.AutoFitX = HtmlToPdfAutoFitMode.ScaleToFit;
//pdfOptions.TriggerMode = HtmlToPdfTriggerMode.Auto;
//HtmlToPdf.Options.StartPageIndex = 1;
//HtmlToPdf.Options.StartPosition = 0;
//pdfOptions.AutoAdjustForDPI = false;
//pdfOptions.PageSize = PdfPageSizes.A4;
//EO.Pdf.HtmlToPdf.Options.OutputArea = new System.Drawing.RectangleF(0.5f, 0.5f, 10f, 10f);
//HtmlToPdf.Options.ZoomLevel = 1;
//HtmlToPdfResult pdf = EO.Pdf.HtmlToPdf.ConvertUrl(Url, @"C:\Pictures\Documenttype.pdf", pdfOptions);
can any solve my prblm,
Thanks

Amit MohantyRank #112 · 20K reputationPosted Apr 5, 2023, 11:48 PM EDT
To convert an HTML string to a PDF file, you can use a third-party library like iTextSharp, IronPDF, or Select.Pdf.
Here is an example using iTextSharp:
using iTextSharp.text; using iTextSharp.text.pdf; using iTextSharp.tool.xml; Document document = new Document(); PdfWriter writer = PdfWriter.GetInstance(document, new FileStream("output.pdf", FileMode.Create)); document.Open(); string html = "Hello, world!"; MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(html)); XMLWorkerHelper.GetInstance().ParseXHtml(writer, document, stream); document.Close();Here is an example using Select.Pdf:
using SelectPdf; PdfDocument document = new PdfDocument(); HtmlToPdf converter = new HtmlToPdf(); converter.Options.PdfPageSize = PdfPageSize.A4; converter.Options.PdfPageOrientation = PdfPageOrientation.Portrait; converter.Options.MarginTop = 10; converter.Options.MarginBottom = 10; // convert the HTML page to PDF PdfDocument doc = converter.ConvertUrl("http://example.com"); doc.Save("output.pdf"); doc.Close();Here is an example using IronPDF:
using IronPdf; using IronPdf.Rendering; var Renderer = new HtmlToPdf(); Renderer.PrintOptions.Header = new HtmlHeaderFooter() { Height = 30, HtmlFragment = "Header content " }; Renderer.PrintOptions.Footer = new HtmlHeaderFooter() { Height = 30, HtmlFragment = "Footer content " }; Renderer.PrintOptions.PaperSize = PaperSize.A4; Renderer.PrintOptions.MarginTop = 20; Renderer.PrintOptions.MarginBottom = 20; Renderer.PrintOptions.MarginLeft = 20; Renderer.PrintOptions.MarginRight = 20; var PDF = Renderer.RenderUrlAsPdf("https://www.example.com/"); PDF.SaveAs("output.pdf");Rahul PrasadRank #2,557 · 546 reputationPosted Sep 8, 2016, 5:22 PM EDT
Marvin ReidRank #1,963 · 746 reputationPosted Apr 5, 2023, 5:23 PM EDT
You can pass the URL into the DocumentConverter and convert the HTML to PDF. This should include the missing contents. Here is some sample code which demonstrates this.
using (DocumentConverter documentConverter = new DocumentConverter()) { var input = DocumentFactory.LoadFromUri(new Uri("HTML file URL"), new LoadDocumentOptions()); // load HTML var output = @"C:\Output\output.pdf"); var jobData = DocumentConverterJobs.CreateJobData(input, output, DocumentFormat.Pdf); jobData.JobName = "save to pdf"; var documentWriter = new DocumentWriter(); documentConverter.SetDocumentWriterInstance(documentWriter); var renderingEngine = new AnnWinFormsRenderingEngine(); documentConverter.SetAnnRenderingEngineInstance(renderingEngine); var job = documentConverter.Jobs.CreateJob(jobData); documentConverter.Jobs.RunJob(job); }https://www.leadtools.com/help/sdk/tutorials/dotnet-convert-files-with-the-document-converter.html
Tuhin PaulRank #7 · 71.9K reputationPosted Apr 27, 2023, 10:19 PM EDT
Marvelous reply @Amit Mohanty , +50 upvotes!
Here I am sharing my version!
Sharing you the code for iTextSharp which is used to convert from HTML to PDF.
using System.IO; using iTextSharp.text; using iTextSharp.text.pdf; using iTextSharp.tool.xml; public void ConvertHtmlToPdf(string htmlFilePath, string pdfFilePath) { using (var fs = new FileStream(pdfFilePath, FileMode.Create, FileAccess.Write, FileShare.None)) { using (var document = new Document()) { var writer = PdfWriter.GetInstance(document, fs); document.Open(); using (var reader = new StreamReader(htmlFilePath)) { var html = reader.ReadToEnd(); using (var stringReader = new StringReader(html)) { XMLWorkerHelper.GetInstance().ParseXHtml(writer, document, stringReader); } } } } }This code reads the contents of an HTML file and writes it to a PDF file using iTextSharp's XMLWorkerHelper class. Some content from the HTML file may be missing in the resulting PDF file, depending on the complexity of the HTML and the capabilities of the PDF conversion library.