Tuesday, October 21, 2014

Print HTML to PDF in Windows Application

Print HTML to PDF in Windows Forms (C#)




using TuesPechkin one can generate good quality PDF from html string both on Web or in windows application. 
First of all Download TuesPechkin.dll  or 
download source code and build it and add this dll in your project.

Link:
https://github.com/tuespetre/TuesPechkin

Now use the following in your main form, It requires html string and file path parameters.

using System;using System.Diagnostics;using System.Drawing.Printing;using System.IO;using System.Linq;using System.Windows.Forms;using TuesPechkin;
      {
            {
                GlobalSettings = {
                    ProduceOutline = true,
                    DocumentTitle = "Invoice Pdf",
                    PaperSize = PaperKind.A4,
                    Margins = {
                        Top = 1.5,
                        Right = 1,
                        Bottom = 1,
                        Left = 1.25,
                        Unit = Unit.Centimeters
                    }
                },
                Objects = {
                    new ObjectSettings { HtmlText = html }  ////                }
            };
            IPechkin sc2 = Factory.Create();
            var buf = sc2.Convert(document);                                 
                string fn = string.Format(PathWithName, Path.GetTempFileName());
                fs.Write(buf, 0, buf.Length);
                fs.Close();
                
                Process myProcess = new Process();
                myProcess.StartInfo.FileName = fn;
                myProcess.Start();
          
         

   private void CreateHtmlToPdf(string html,string PathWithName)
 var document = new HtmlToPdfDocument
                FileStream fs = new FileStream(fn, FileMode.Create);





No comments:

Post a Comment