Advertisement
mrkarp

PDFManager.cs

Dec 28th, 2017
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace ConsoleApp1.PDF
  9. {
  10. class PDFManager
  11. {
  12.  
  13. public void CreatePDF(string fileName, string htmlString)
  14. {
  15. ByteArrayToFile(fileName, PdfSharpConvert(htmlString));
  16. Console.WriteLine("Created PDF: " + fileName);
  17. }
  18.  
  19. private bool ByteArrayToFile(string fileName, byte[] byteArray)
  20. {
  21. try
  22. {
  23. using (var fs = new FileStream(fileName, FileMode.Create, FileAccess.Write))
  24. {
  25. fs.Write(byteArray, 0, byteArray.Length);
  26. return true;
  27. }
  28. }
  29. catch (Exception ex)
  30. {
  31. Console.WriteLine("Exception caught in process: {0}", ex);
  32. return false;
  33. }
  34. }
  35.  
  36. private Byte[] PdfSharpConvert(String html)
  37. {
  38. Byte[] res = null;
  39. using (MemoryStream ms = new MemoryStream())
  40. {
  41. var pdf = TheArtOfDev.HtmlRenderer.PdfSharp.PdfGenerator.GeneratePdf(html, PdfSharp.PageSize.A4);
  42. pdf.Save(ms);
  43. res = ms.ToArray();
  44. }
  45. return res;
  46. }
  47. }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement