public static byte[] Html2Pdf(string strHTML) { //wkHtmlToPdfSharpStatic.InitLib(false); // pc.FinishAction("Library initialized"); SynchronizedwkHtmlToPdfSharp sc = new SynchronizedwkHtmlToPdfSharp(new GlobalConfig() .SetMargins(new System.Drawing.Printing.Margins(0, 0, 0, 0)) .SetDocumentTitle("Ololo") .SetCopyCount(1) .SetImageQuality(50) .SetLosslessCompression(true) .SetMaxImageDpi(20) .SetOutlineGeneration(true) .SetOutputDpi(1200) .SetPaperOrientation(true) .SetPaperSize(250, 200) //.SetPaperSize(PaperKind.Letter) ); // pc.FinishAction("Converter created"); //sc.Begin += OnScBegin; //sc.Error += OnScError; //sc.Warning += OnScWarning; //sc.PhaseChanged += OnScPhase; //sc.ProgressChanged += OnScProgress; //sc.Finished += OnScFinished; //pc.FinishAction("Event handlers installed"); byte[] buf = sc.Convert(new ObjectConfig().SetPrintBackground(true), strHTML); /* * sc.Convert(new ObjectConfig().SetPrintBackground(true).SetProxyString("http://localhost:8888") * .SetAllowLocalContent(true).SetCreateExternalLinks(false).SetCreateForms(false).SetCreateInternalLinks(false) * .SetErrorHandlingType(ObjectConfig.ContentErrorHandlingType.Ignore).SetFallbackEncoding(System.Text.Encoding.ASCII) * .SetIntelligentShrinking(false).SetJavascriptDebugMode(true).SetLoadImages(true).SetMinFontSize(16) * .SetRenderDelay(2000).SetRunJavascript(true).SetIncludeInOutline(true).SetZoomFactor(2.2), strHTML); */ //pc.FinishAction("conversion finished"); if (buf == null) { throw new Exception("Error converting!"); //return null; } return(buf); }
public byte[] Html2Pdf(string strHTML, int width, int height) { //wkHtmlToPdfSharpStatic.InitLib(false); // pc.FinishAction("Library initialized"); SynchronizedwkHtmlToPdfSharp sc = new SynchronizedwkHtmlToPdfSharp(new GlobalConfig() .SetMargins(new System.Drawing.Printing.Margins(0, 0, 0, 0)) .SetDocumentTitle("Legende") .SetCopyCount(1) //.SetImageQuality(50) .SetImageQuality(100) .SetLosslessCompression(true) //.SetMaxImageDpi(20) .SetMaxImageDpi(15200) .SetOutlineGeneration(true) .SetOutputDpi(14200) .SetPaperOrientation(true) //.SetPaperSize(System.Drawing.Printing.PaperKind.Letter) //.SetPaperSize(250, 200) //.SetPaperSize(System.Drawing.Printing.PaperKind.A4) // Width/Height: Gets or sets the width/height of the paper, in hundredths of an inch. // 1 inch = 2.54cm = 25.4mm // 1/100 inch = 0.0254cm = 0.254mm //.SetPaperSize(new System.Drawing.Printing.PaperSize("lala", 800, 625)) //.SetPaperSize(new System.Drawing.Printing.PaperSize("SvgSize", 1297 + 37, 803 + 37)) //.SetPaperSize(new System.Drawing.Printing.PaperSize("SvgSize", 1297, 803)) // Pixel .SetPaperSize(new System.Drawing.Printing.PaperSize("SvgSize", width + 37, height + 37)) //.SetPaperSize(new System.Drawing.Printing.PaperSize("SvgSize", 1170, 827)) // <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="21cm" height="29.7cm" viewBox="0 0 2098 2969.3" version="1.1"> // 29.7 cm = 11.69291 // 21 cm = 8.26772 //.SetPaperSize(new System.Drawing.Printing.PaperSize("lala", iDimX, iDimY)) ); // pc.FinishAction("Converter created"); //sc.Begin += OnScBegin; //sc.Error += OnScError; //sc.Warning += OnScWarning; //sc.PhaseChanged += OnScPhase; //sc.ProgressChanged += OnScProgress; //sc.Finished += OnScFinished; //pc.FinishAction("Event handlers installed"); byte[] buf = sc.Convert(new ObjectConfig() .SetPrintBackground(true) .SetIntelligentShrinking(false) , strHTML ); /* * sc.Convert(new ObjectConfig() * .SetPrintBackground(true) * .SetProxyString("http://localhost:8888") * .SetAllowLocalContent(true) * .SetCreateExternalLinks(false) * .SetCreateForms(false) * .SetCreateInternalLinks(false) * .SetErrorHandlingType(ObjectConfig.ContentErrorHandlingType.Ignore) * .SetFallbackEncoding(System.Text.Encoding.ASCII) * .SetIntelligentShrinking(false) * .SetJavascriptDebugMode(true) * .SetLoadImages(true) * .SetMinFontSize(16) * .SetRenderDelay(2000) * .SetRunJavascript(true) * .SetIncludeInOutline(true) * .SetZoomFactor(2.2) * ,strHTML * ); */ //pc.FinishAction("conversion finished"); if (buf == null) { MessageBox.Show("Error converting!"); return(null); } // End if (buf == null) return(buf); } // End Function Html2Pdf
private void OnConvertButtonClick(object sender, EventArgs e) { PerformanceCollector pc = new PerformanceCollector("PDF creation"); //wkHtmlToPdfSharpStatic.InitLib(false); pc.FinishAction("Library initialized"); SynchronizedwkHtmlToPdfSharp sc = new SynchronizedwkHtmlToPdfSharp(new GlobalConfig().SetMargins(new Margins(0, 0, 0, 0)) .SetDocumentTitle("Ololo").SetCopyCount(1).SetImageQuality(50) .SetLosslessCompression(true).SetMaxImageDpi(20).SetOutlineGeneration(true).SetOutputDpi(1200).SetPaperOrientation(true) .SetPaperSize(250, 200) //.SetPaperSize(PaperKind.Letter) ); pc.FinishAction("Converter created"); sc.Begin += OnScBegin; sc.Error += OnScError; sc.Warning += OnScWarning; sc.PhaseChanged += OnScPhase; sc.ProgressChanged += OnScProgress; sc.Finished += OnScFinished; pc.FinishAction("Event handlers installed"); byte[] buf = sc.Convert(new ObjectConfig(), htmlText.Text); /*sc.Convert(new ObjectConfig().SetPrintBackground(true).SetProxyString("http://localhost:8888") * .SetAllowLocalContent(true).SetCreateExternalLinks(false).SetCreateForms(false).SetCreateInternalLinks(false) * .SetErrorHandlingType(ObjectConfig.ContentErrorHandlingType.Ignore).SetFallbackEncoding(Encoding.ASCII) * .SetIntelligentShrinking(false).SetJavascriptDebugMode(true).SetLoadImages(true).SetMinFontSize(16) * .SetRenderDelay(2000).SetRunJavascript(true).SetIncludeInOutline(true).SetZoomFactor(2.2), htmlText.Text);*/ pc.FinishAction("conversion finished"); if (buf == null) { MessageBox.Show("Error converting!"); return; } //for (int i = 0; i < 1000; i++) { buf = sc.Convert(new ObjectConfig(), htmlText.Text); if (buf == null) { MessageBox.Show("Error converting!"); return; } } pc.FinishAction("1 more conversions finished"); try { string fn = Path.GetTempFileName() + ".pdf"; FileStream fs = new FileStream(fn, FileMode.Create); fs.Write(buf, 0, buf.Length); fs.Close(); pc.FinishAction("dumped file to disk"); Process myProcess = new Process(); myProcess.StartInfo.FileName = fn; myProcess.Start(); pc.FinishAction("opened it"); } catch { } // pc.ShowInMessageBox(null); }