Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ConsoleApp1.PDF
- {
- class PDFManager
- {
- public void CreatePDF(string fileName, string htmlString)
- {
- ByteArrayToFile(fileName, PdfSharpConvert(htmlString));
- Console.WriteLine("Created PDF: " + fileName);
- }
- private bool ByteArrayToFile(string fileName, byte[] byteArray)
- {
- try
- {
- using (var fs = new FileStream(fileName, FileMode.Create, FileAccess.Write))
- {
- fs.Write(byteArray, 0, byteArray.Length);
- return true;
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine("Exception caught in process: {0}", ex);
- return false;
- }
- }
- private Byte[] PdfSharpConvert(String html)
- {
- Byte[] res = null;
- using (MemoryStream ms = new MemoryStream())
- {
- var pdf = TheArtOfDev.HtmlRenderer.PdfSharp.PdfGenerator.GeneratePdf(html, PdfSharp.PageSize.A4);
- pdf.Save(ms);
- res = ms.ToArray();
- }
- return res;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement