public string Get() { var config = new GlobalConfig(); config.SetMargins(0, 0, 100, 0); config.SetOutputDpi(300); config.SetPaperSize(PaperKind.A4); var converter = Factory.Create(config); var contents = File.ReadAllText(@"C:\Users\janand\Documents\GitHub\Invoicer\InvoicerHost\invoice.html"); var pdfBytes = converter.Convert(contents); using (var writer = new BinaryWriter(new FileStream(@"C:\Users\janand\Documents\test.pdf", FileMode.Create))) { writer.Write(pdfBytes); } return "yeah!"; }
public static void CreatePdfFromHtml(string htmlFilePath) { var htmlFileInfo = new FileInfo(htmlFilePath); if (!htmlFileInfo.Exists) { throw new FileNotFoundException(htmlFileInfo.FullName); } Debug.Assert(htmlFileInfo.DirectoryName != null, "htmlFileInfo.DirectoryName != null"); var tmpPdfFileInfo = new FileInfo(Path.Combine(htmlFileInfo.DirectoryName, "tmp.pdf")); var pdfOutFileInfo = new FileInfo(GetPdfEquivalentPath(htmlFileInfo.FullName)); var gc = new GlobalConfig(); gc.SetImageQuality(100); gc.SetOutputDpi(96); gc.SetPaperSize(1024, 1123); var oc = new ObjectConfig(); oc.SetLoadImages(true); oc.SetAllowLocalContent(true); oc.SetPrintBackground(true); oc.SetZoomFactor(1.093); oc.SetPageUri(htmlFileInfo.FullName); if (tmpPdfFileInfo.Exists) { tmpPdfFileInfo.Delete(); } IPechkin pechkin = new SynchronizedPechkin(gc); pechkin.Error += (converter, text) => { Console.Out.WriteLine("error " + text); }; pechkin.Warning += (converter, text) => { Console.Out.WriteLine("warning " + text); }; using (var file = File.OpenWrite(tmpPdfFileInfo.FullName)) { var bytes = pechkin.Convert(oc); file.Write(bytes, 0, bytes.Length); } if (pdfOutFileInfo.Exists) { pdfOutFileInfo.Delete(); } CreateDirectories(pdfOutFileInfo.DirectoryName); tmpPdfFileInfo.MoveTo(pdfOutFileInfo.FullName); }