Guest User

Guest User

  • Tech Writer
  • 515
  • 46k

About converting Html content to PDF format

Jul 21 2020 10:56 AM
Hello Everyone,
 
Hope everyone is fine.
 
I have HTML content and want to convert that to pdf format and save it to a local project folder.
 
I have installed HtmlRender.PdfSharp NuGet library in my project.
 
I have also tried a few examples but did not work out. Even I did not find any error in that code.
 
So I am not getting, what I am doing wrong.
 
I have tried the following code :
  1. protected void buttonClick(Object sender, EventArgs e)  
  2. {  
  3. Byte[] fileContent=PdfSharpConvert("<head></head><body style='color:red;'>My HTML Layout</body>");  
  4. string fileName = "New.pdf";  
  5. string[] stringParts = fileName.Split(new char[] { '.' });  
  6. string strType = stringParts[1];  
  7. Response.Clear();  
  8. Response.ClearContent();  
  9. Response.ClearHeaders();  
  10. Response.AddHeader("content-disposition""attachment; filename=" + fileName);  
  11. //Set the content type as file extension type  
  12. Response.ContentType = strType;  
  13. //Write the file content  
  14. this.Response.BinaryWrite(fileContent);  
  15. this.Response.End();  
  16. }  
  17.   
  18. public static Byte[] PdfSharpConvert(String html)  
  19. {  
  20. Byte[] res = null;  
  21. using (MemoryStream ms = new MemoryStream())  
  22. {  
  23. var pdf = TheArtOfDev.HtmlRenderer.PdfSharp.PdfGenerator.GeneratePdf(html, PdfSharp.PageSize.A4);  
  24. pdf.Save(ms);  
  25. res = ms.ToArray();  
  26. }  
  27. return res;  
  28. }  
Please let me know anything can be done to fix these.
 
Thank you in advance.

Answers (4)