Ejemplo n.º 1
2
 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();
 }
Ejemplo n.º 2
0
// 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();
            }
        }
Ejemplo n.º 3
0
 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);
     }
 }
Ejemplo n.º 4
0
        /// <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;
        }
Ejemplo n.º 5
0
        // 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();
            }
        }
Ejemplo n.º 6
0
        /// <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;
        }
Ejemplo n.º 7
0
 private void OnScWarning(SimplePechkin converter, string warningtext)
 {
     if (InvokeRequired)
     {
         BeginInvoke((Action)(() => { MessageBox.Show("Warning: " + warningtext); }));
     }
     else
     {
         MessageBox.Show("Warning: " + warningtext);
     }
 }
Ejemplo n.º 8
0
 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);
     }
 }
Ejemplo n.º 9
0
 private void OnScPhase(SimplePechkin converter, int phasenumber, string phasedescription)
 {
     if (InvokeRequired)
     {
         BeginInvoke((Action)(() => { Text = ("New Phase " + phasenumber + ": " + phasedescription); }));
     }
     else
     {
         Text = ("New Phase " + phasenumber + ": " + phasedescription);
     }
 }
Ejemplo n.º 10
0
 private void OnScFinished(SimplePechkin converter, bool success)
 {
     if (InvokeRequired)
     {
         BeginInvoke((Action)(() => { Text = ("Finished, Success: " + success); }));
     }
     else
     {
         Text = ("Finished, Success: " + success);
     }
 }
Ejemplo n.º 11
0
 private void OnScError(SimplePechkin converter, string errorText)
 {
     if (InvokeRequired)
     {
         BeginInvoke((Action)(() => { MessageBox.Show("Error: " + errorText); }));
     }
     else
     {
         MessageBox.Show("Error: " + errorText);
     }
 }
Ejemplo n.º 12
0
 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);
        }