經過NuGet安裝 html
Pechkin
Pechkin.Synchronizedspa
一下示例是控制檯應用程序code
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);