首页 > Web开发 > 详细

使用Pechkin将HTML网页转换为PDF

时间:2016-12-27 00:06:42      阅读:1365      评论:0      收藏:0      [点我收藏+]

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);

 

使用Pechkin将HTML网页转换为PDF

原文:http://www.cnblogs.com/guichi/p/6220960.html

(3)
(3)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!