private void SaveHtmlAsPdf(string fName, string html) { GlobalConfig cfg = new GlobalConfig(); SimplePechkin sp = new SimplePechkin(cfg); ObjectConfig oc = new ObjectConfig(); oc.SetCreateExternalLinks(true) .SetFallbackEncoding(Encoding.ASCII) .SetLoadImages(true) .SetAllowLocalContent(true) .SetPrintBackground(true); byte[] pdfBuf = sp.Convert(oc, "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"><title></title></head><body>" + html + "</body></html>"); FileStream fs = new FileStream(fName, FileMode.Create, FileAccess.ReadWrite); foreach (var byteSymbol in pdfBuf) fs.WriteByte(byteSymbol); fs.Close(); }
// ReSharper restore NotAccessedField.Local /// <summary> /// Initializes wrapped library. This is done automatically when you need it. /// </summary> /// <param name="useGraphics">use X11 graphics, <code>false</code> in most cases.</param> public static void InitLib(bool useGraphics) { if (_inited) { return; } _inited = true; if (Log.IsTraceEnabled) { Log.Trace("T:" + Thread.CurrentThread.Name + " Initializing library (wkhtmltopdf_init)"); } PechkinBindings.wkhtmltopdf_init(useGraphics ? 1 : 0); if (_useHack) { _hackObj = new SimplePechkin(new GlobalConfig()); } if (LibInit != null) { LibInit(); } }
protected void Button1_Click(object sender, EventArgs e) { if (Session["source"] != null) { byte[] pdfBuf = new SimplePechkin(new GlobalConfig()).Convert((string)Session["source"]); System.IO.File.WriteAllBytes("F:\\test.pdf", pdfBuf); } }
/// <summary> /// Deinitializes library. /// </summary> public static void DeinitLib() { if (!_inited) { return; } _inited = false; LibDeInit?.Invoke(); if (Log.IsTraceEnabled) { Log.Trace("T:" + Thread.CurrentThread.Name + " Deinitializing library (wkhtmltopdf_deinit)"); } PechkinBindings.wkhtmltopdf_deinit(); _hackObj = null; }
// ReSharper restore NotAccessedField.Local /// <summary> /// Initializes wrapped library. This is done automatically when you need it. /// </summary> /// <param name="useGraphics">use X11 graphics, <code>false</code> in most cases.</param> public static void InitLib(bool useGraphics) { if (_inited) return; _inited = true; if (Log.IsTraceEnabled) { Log.Trace("T:" + Thread.CurrentThread.Name + " Initializing library (wkhtmltopdf_init)"); } PechkinBindings.wkhtmltopdf_init(useGraphics ? 1 : 0); if (_useHack) { _hackObj = new SimplePechkin(new GlobalConfig()); } if (LibInit != null) { LibInit(); } }
/// <summary> /// Deinitializes library. /// </summary> public static void DeinitLib() { if (!_inited) return; _inited = false; if (LibDeInit != null) { LibDeInit(); } if (Log.IsTraceEnabled) { Log.Trace("T:" + Thread.CurrentThread.Name + " Deinitializing library (wkhtmltopdf_deinit)"); } PechkinBindings.wkhtmltopdf_deinit(); _hackObj = null; }
private void OnScWarning(SimplePechkin converter, string warningtext) { if (InvokeRequired) { BeginInvoke((Action)(() => { MessageBox.Show("Warning: " + warningtext); })); } else { MessageBox.Show("Warning: " + warningtext); } }
private void OnScProgress(SimplePechkin converter, int progress, string progressdescription) { if (InvokeRequired) { // simple Invoke WILL NEVER SUCCEDE, because we're in the button click handler. Invoke will simply deadlock everything BeginInvoke((Action<string>)SetText,"Progress " + progress + ": " + progressdescription); } else { Text = ("Progress " + progress + ": " + progressdescription); } }
private void OnScPhase(SimplePechkin converter, int phasenumber, string phasedescription) { if (InvokeRequired) { BeginInvoke((Action)(() => { Text = ("New Phase " + phasenumber + ": " + phasedescription); })); } else { Text = ("New Phase " + phasenumber + ": " + phasedescription); } }
private void OnScFinished(SimplePechkin converter, bool success) { if (InvokeRequired) { BeginInvoke((Action)(() => { Text = ("Finished, Success: " + success); })); } else { Text = ("Finished, Success: " + success); } }
private void OnScError(SimplePechkin converter, string errorText) { if (InvokeRequired) { BeginInvoke((Action)(() => { MessageBox.Show("Error: " + errorText); })); } else { MessageBox.Show("Error: " + errorText); } }
private void OnScBegin(SimplePechkin converter, int expectedphasecount) { if (InvokeRequired) { BeginInvoke((Action)(() => { Text = ("Begin, PhaseCount: " + expectedphasecount); })); } else { Text = ("Begin, PhaseCount: " + expectedphasecount); } }
private byte[] CreatePDF(HtmlDocument doc) { // Create global configuration object GlobalConfig gc = new GlobalConfig(); // Set it up using fluent notation gc.SetMargins(new Margins(50, 100, 0, 0)) .SetDocumentTitle("Request") .SetPaperSize(PaperKind.A4); // Create converter IPechkin pechkin = new SimplePechkin(gc); // Create document configuration object ObjectConfig oc = new ObjectConfig(); // And set it up using fluent notation oc.SetCreateExternalLinks(true) .SetFallbackEncoding(Encoding.ASCII) .SetZoomFactor(2) .SetIntelligentShrinking(true) .SetLoadImages(true); // Convert document return pechkin.Convert(oc, doc.DocumentNode.OuterHtml); }