zoukankan      html  css  js  c++  java
  • c# 通过html导出pdf,带分页

    通过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:UsersAdministratorDocumentsWeChat FilesahjesusFiles111.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);
  • 相关阅读:
    zookeeper的集群搭建
    Java代码操作zookeeper
    zookeeper的简介和相关命令操作
    Linux上搭建zookeeper服务注册中心
    Java8新特性(三)之方法引用和构造器引用
    Java8新特性(一)之Lambda表达式
    react-navigation 3.x版本的使用
    react-navigation 3.x版本的安装以及react-native-gesture-handler配置
    react-native 打包 出apk
    webpack的使用
  • 原文地址:https://www.cnblogs.com/ahjesus/p/9014795.html
Copyright © 2011-2022 走看看