js content

using NReco.PdfGenerator; using System; using System.Text; using System.IO; using System.Text.RegularExpressions; using System.Drawing.Imaging; using System.Drawing; namespace GPSConvertor { public class Program { public static void Main(string[] args) { var g = new GPSPdfGenerator(); g.GenerateToc = true; g.FileName = "test.pdf"; Regex imgSrcRegex = new Regex("img src=\"([^\"]+)\"", RegexOptions.IgnoreCase | RegexOptions.Compiled); for (var n = 0; n < 100; n++) { // fake data var htmlFragment = $"

{n}

"; var imgUriMatch = imgSrcRegex.Match(htmlFragment); var imgUri = imgUriMatch.Groups[1].Value; if (!string.IsNullOrEmpty(imgUri)) { using (WebClient client = new WebClient()) { var imgStream = client.OpenRead(imgUri); var base64Img = GetImageAsBase64(imgStream); htmlFragment = imgSrcRegex.Replace(htmlFragment, base64Img, 1); } } g.AddHtmlPage(htmlFragment); } g.SavePdfFile(); Console.ReadLine(); } private static string GetImageAsBase64(Stream imgStream) { var bmp = (Bitmap)Bitmap.FromStream(imgStream); using (MemoryStream m = new MemoryStream()) { bmp.Save(m, ImageFormat.Png); var imageBytes = m.ToArray(); var base64String = Convert.ToBase64String(imageBytes); return string.Format("img src=\"data:image/png;base64,{0}\"", base64String); } } } public class GPSPdfGenerator { private const string HtmlDocTemplate = @"{1}"; private StringBuilder HtmlBuilder { get; set; } private HtmlToPdfConverter PdfConverter { get; set; } public string Css { get; set; } public string CustomWkHtmlArgs { get; set; } public string FileName { get; set; } public bool GenerateToc { get; set; } public PageOrientation Orientation { get; set; } public string TocHeaderText { get; set; } public GPSPdfGenerator() { HtmlBuilder = new StringBuilder(); PdfConverter = new HtmlToPdfConverter(); } /// /// Adds to the string builder /// public void AddHtmlPage(string htmlFragment) { HtmlBuilder.Append(htmlFragment); } /// /// Saves html as PDF to the location specified in the property /// public void SavePdfFile() { InitPdfGenerator(); var resultHtmlFragment = HtmlBuilder.ToString(); var resultHtmlPage = string.Format(HtmlDocTemplate, Css, resultHtmlFragment); var pdfBytes = PdfConverter.GeneratePdf(resultHtmlPage); File.WriteAllBytes(FileName, pdfBytes); } private void InitPdfGenerator() { if (string.IsNullOrEmpty(FileName)) throw new ArgumentNullException("No name was provided for the output pdf file"); Css = Css ?? string.Empty; PdfConverter.CustomWkHtmlArgs = CustomWkHtmlArgs ?? "--margin-top 0 --header-spacing 0 --margin-left 0 --margin-right 0"; PdfConverter.GenerateToc = GenerateToc; PdfConverter.Orientation = Orientation; PdfConverter.TocHeaderText = TocHeaderText ?? string.Empty; PdfConverter.PageFooterHtml = @"
page of
"; } } } //public static Stream GetImageStream(SPWeb web, string imgUri) //{ // Stream imgStream = null; // try // { // SPSecurity.RunWithElevatedPrivileges(delegate () // { // using (SPSite site = new SPSite(web.Url)) // { // using (SPWeb rootWeb = site.RootWeb) // { // SPList spList = rootWeb.Lists.TryGetList(Constants.LISTNAME_SITEASSESTS); // if (spList != null) // { // SPQuery query = new SPQuery(); // query.Query = "" + name + ""; // SPListItemCollection files = spList.GetItems(query); // if (files.Count > 0) // { // imgStream = files[0].File.OpenBinaryStream(SPOpenBinaryOptions.None); // } // } // } // } // }); // } // catch (ex) // { // throw ex; // } // return imgStream; //}

Comments

Popular posts from this blog

CREATE DATABASE (SMO)

Enable JSON serialization in your Visual Studio 2010 project

jQuery | remove inline style