使用Pechkin将HTML网页转换为PDF

Pechkin开源组件使用wkhtmlbox,可以解析CSS样式,将网页转换为PDF文件,

支持URL,或者HTML字符串

 

 1, 从NuGet程序管理器中获得Pechkin

 GlobalConfig config = new GlobalConfig();
            SimplePechkin pechkin = new SimplePechkin(config);
            ObjectConfig objectConfig = new ObjectConfig();
            objectConfig.SetPrintBackground(true)
                .SetLoadImages(true)
                .SetAffectPageCounts(true)
                .SetPageUri("http://www.abc.com/");
            byte[] bytePDF = pechkin.Convert(objectConfig);,
            File.WriteAllBytes("F:\\PDFName.pdf", bytePDF);
 
2,CPechkin,网友对Pechkin进行了封装,也可以从NuGet获得


SynchronizedPechkin sc = new SynchronizedPechkin(new GlobalConfig()
.SetMargins(new Margins() { Left = 0, Right = 0, Top = 0, Bottom = 0 }) //设置边距
.SetPaperOrientation(true)); //设置纸张方向为横向
//.SetPaperSize(340, 594)); //设置纸张大小50mm * 100mm
ObjectConfig oc = new ObjectConfig();

oc.SetPageUri("http://localhost:40146/Styles/HTMLPage2.htm");
byte[] buf = sc.Convert(oc);
Response.AddHeader("content-disposition", "attachment;filename=1.pdf");

Response.ContentType = "application/octet-stream";
Response.BinaryWrite(buf);

 

posted @ 2016-12-26 22:26  鸡蛋面  阅读(1065)  评论(0编辑  收藏  举报