通过NuGet安装 

Pechkin
Pechkin.Synchronized

 

一下示例是控制台应用程序

static void btnCreate() {
            SynchronizedPechkin sc = new SynchronizedPechkin(new GlobalConfig()
                .SetMargins(new Margins() { Left = 0, Right = 0, Top = 0, Bottom = 0 }) //设置边距
                .SetPaperOrientation(false) //设置纸张方向为纵向
                .SetPaperSize(MillimetersToPixelsWidth(100), MillimetersToPixelsWidth(150))); //设置纸张大小50mm * 100mm

            ObjectConfig oc = new ObjectConfig();
            oc.SetPrintBackground(true)
                .SetLoadImages(true);

            byte[] buf = sc.Convert(oc, File.ReadAllText(@"C:\Users\Administrator\Documents\WeChat Files\ahjesus\Files\111.html"));

            if (buf == null) {
                Console.WriteLine("Error converting!");
                return;
            }

            try {
                string fn = Path.GetTempFileName() + ".pdf";
                FileStream fs = new FileStream(fn, FileMode.Create);
                fs.Write(buf, 0, buf.Length);
                fs.Close();

                Process myProcess = new Process();
                myProcess.StartInfo.FileName = fn;
                myProcess.Start();
            } catch { }
        }

        static string get_uft8(string unicodeString) {
            UTF8Encoding utf8 = new UTF8Encoding();
            Byte[] encodedBytes = utf8.GetBytes(unicodeString);
            String decodedString = utf8.GetString(encodedBytes);
            return decodedString;
        }

        //length是毫米,1厘米=10毫米
        public static int MillimetersToPixelsWidth(float length) {
            System.Windows.Forms.Panel p = new System.Windows.Forms.Panel();
            System.Drawing.Graphics g = System.Drawing.Graphics.FromHwnd(p.Handle);
            IntPtr hdc = g.GetHdc();
            int width = GetDeviceCaps(hdc, 4);     // HORZRES 
            int pixels = GetDeviceCaps(hdc, 8);     // BITSPIXEL
            g.ReleaseHdc(hdc);
            return (int)(((float)pixels / (float)width) * length);
        }
        [DllImport("gdi32.dll")]
        private static extern int GetDeviceCaps(IntPtr hdc, int Index);

 

posted on 2018-05-09 15:58  深海大虾  阅读(913)  评论(0编辑  收藏  举报