Last post Sep 08, 2013 06:32 PM by jats_ptl
Member
21 Points
104 Posts
Sep 05, 2013 06:39 PM|2bitcoder|LINK
Hello,
I have html pages that are designed specifically for printing (Reports), they work well and look good.
I would like to be able to output these to pdf also, I've previously tried with print drivers, however the results are varied and not easy for users.
Hence, I would like to be able to output the page directly to pdf using itextsharp and the htmlparser. I've been looking at http://www.mikesdotnetting.com/Article/205/Exporting-The-Razor-WebGrid-To-PDF-Using-iTextSharp and itextsharp articals, which I intend on using to write other reports.
I've also looked at this http://aspnettutorialonline.blogspot.com/2012/04/converting-html-to-pdf-using-itextsharp.html however I'm really not comfortable with c# yet, and would much prefer the razor approach.
I would prefer not to have to re-code my html print optimsed pages if I could easily use the htmlparser, which I'm not sure how to implement.
Any tips would be helpful...
Star
12651 Points
2375 Posts
Sep 05, 2013 08:58 PM|jats_ptl|LINK
Hi 2bitcoder,
Please follow the Steps for converting html to pdf using iTextSharp:
1. Take iTextsharp dll from here. under bin folder.
2. in the below code, CreatePDFFromHTMLFile method will convert html to pdf conversion.
3. TestPDF Namespace, will get from here.under app_code folder.
4. Here you can set password to protect your pdf document.
5. After your execution you will get generated pdf document under files folder.
Code:
protected void Page_Load(object sender, EventArgs e) { try { string strHtml = string.Empty; //HTML File path -http://aspnettutorialonline.blogspot.com/ string htmlFileName = Server.MapPath("~") + "\\files\\" + "ConvertHTMLToPDF.htm"; //pdf file path. -http://aspnettutorialonline.blogspot.com/ string pdfFileName = Request.PhysicalApplicationPath + "\\files\\" + "ConvertHTMLToPDF.pdf"; //reading html code from html file FileStream fsHTMLDocument = new FileStream(htmlFileName, FileMode.Open, FileAccess.Read); StreamReader srHTMLDocument = new StreamReader(fsHTMLDocument); strHtml = srHTMLDocument.ReadToEnd(); srHTMLDocument.Close(); strHtml = strHtml.Replace("\r\n", ""); strHtml = strHtml.Replace("\0", ""); CreatePDFFromHTMLFile(strHtml, pdfFileName); Response.Write("pdf creation successfully with password -http://aspnettutorialonline.blogspot.com/"); } catch (Exception ex) { Response.Write(ex.Message); } } public void CreatePDFFromHTMLFile(string HtmlStream, string FileName) { try { object TargetFile = FileName; string ModifiedFileName = string.Empty; string FinalFileName = string.Empty; /* To add a Password to PDF -http://aspnettutorialonline.blogspot.com/ */ TestPDF.HtmlToPdfBuilder builder = new TestPDF.HtmlToPdfBuilder(iTextSharp.text.PageSize.A4); TestPDF.HtmlPdfPage first = builder.AddPage(); first.AppendHtml(HtmlStream); byte[] file = builder.RenderPdf(); File.WriteAllBytes(TargetFile.ToString(), file); iTextSharp.text.pdf.PdfReader reader = new iTextSharp.text.pdf.PdfReader(TargetFile.ToString()); ModifiedFileName = TargetFile.ToString(); ModifiedFileName = ModifiedFileName.Insert(ModifiedFileName.Length - 4, "1"); string password = "password"; iTextSharp.text.pdf.PdfEncryptor.Encrypt(reader, new FileStream(ModifiedFileName, FileMode.Append), iTextSharp.text.pdf.PdfWriter.STRENGTH128BITS, password, "", iTextSharp.text.pdf.PdfWriter.AllowPrinting); //http://aspnettutorialonline.blogspot.com/ reader.Close(); if (File.Exists(TargetFile.ToString())) File.Delete(TargetFile.ToString()); FinalFileName = ModifiedFileName.Remove(ModifiedFileName.Length - 5, 1); File.Copy(ModifiedFileName, FinalFileName); if (File.Exists(ModifiedFileName)) File.Delete(ModifiedFileName); } catch (Exception ex) { throw ex; } }
Source : Amy Peng - MSFT Solution http://forums.asp.net/t/1886131.aspx?Convert+HTML+to+PDF+using+iTextSharp
Hope this helps.
Thanks,
Jatin
Sep 08, 2013 06:29 PM|2bitcoder|LINK
Follow up - I implemented the above solution, however I got errors as I think, my .CSHTML may have been to complex for the htmlparser, simplified html worked fine.
In the end I spent a day learning iTextSharp, then wrote my reports.
The outcome was better than I'd hoped.
Sep 08, 2013 06:32 PM|jats_ptl|LINK
Glad that I could help you!! Cheers
Member
21 Points
104 Posts
HTML to PDF using itextsharp HtmlParser
Sep 05, 2013 06:39 PM|2bitcoder|LINK
Hello,
I have html pages that are designed specifically for printing (Reports), they work well and look good.
I would like to be able to output these to pdf also, I've previously tried with print drivers, however the results are varied and not easy for users.
Hence, I would like to be able to output the page directly to pdf using itextsharp and the htmlparser. I've been looking at http://www.mikesdotnetting.com/Article/205/Exporting-The-Razor-WebGrid-To-PDF-Using-iTextSharp and itextsharp articals, which I intend on using to write other reports.
I've also looked at this http://aspnettutorialonline.blogspot.com/2012/04/converting-html-to-pdf-using-itextsharp.html however I'm really not comfortable with c# yet, and would much prefer the razor approach.
I would prefer not to have to re-code my html print optimsed pages if I could easily use the htmlparser, which I'm not sure how to implement.
Any tips would be helpful...
Star
12651 Points
2375 Posts
Re: HTML to PDF using itextsharp HtmlParser
Sep 05, 2013 08:58 PM|jats_ptl|LINK
Hi 2bitcoder,
Please follow the Steps for converting html to pdf using iTextSharp:
1. Take iTextsharp dll from here. under bin folder.
2. in the below code, CreatePDFFromHTMLFile method will convert html to pdf conversion.
3. TestPDF Namespace, will get from here.under app_code folder.
4. Here you can set password to protect your pdf document.
5. After your execution you will get generated pdf document under files folder.
Code:
Source : Amy Peng - MSFT Solution http://forums.asp.net/t/1886131.aspx?Convert+HTML+to+PDF+using+iTextSharp
Hope this helps.
Thanks,
Jatin
Member
21 Points
104 Posts
Re: HTML to PDF using itextsharp HtmlParser
Sep 08, 2013 06:29 PM|2bitcoder|LINK
Follow up - I implemented the above solution, however I got errors as I think, my .CSHTML may have been to complex for the htmlparser, simplified html worked fine.
In the end I spent a day learning iTextSharp, then wrote my reports.
The outcome was better than I'd hoped.
Star
12651 Points
2375 Posts
Re: HTML to PDF using itextsharp HtmlParser
Sep 08, 2013 06:32 PM|jats_ptl|LINK
Hi 2bitcoder,
Glad that I could help you!! Cheers