随笔 - 77  文章 - 0 评论 - 12 阅读 - 69354

复制代码
 1    public class PdfHelper
 2     {
 3 
 4         static string RootPath
 5         {
 6             get
 7             {
 8                 string AppPath = "";
 9                 HttpContext HttpCurrent = HttpContext.Current;
10                 if (HttpCurrent != null)
11                 {
12                     AppPath = HttpCurrent.Server.MapPath("~");
13                 }
14 
15                 return AppPath.Trim(new char[]{'\\'});
16             }
17         }
18 
19        
20         public static void GetPdf(HttpResponseBase response,string pdfUrl)
21         {
22             string fileName = Guid.NewGuid().ToString("N") + ".pdf";
23             string filePath = RootPath + "\\pdfs\\" + fileName;
24             string exePath=RootPath + "\\wkhtmltopdf\\wkhtmltopdf.exe";
25             string args=pdfUrl + " --zoom 1.25 \"" + filePath+"\"";
26           
27           
28             Process p =new Process();
29             p.StartInfo.FileName = exePath;
30             p.StartInfo.Arguments = args;
31             p.StartInfo.UseShellExecute = false;
32             p.StartInfo.RedirectStandardInput = true;
33             p.StartInfo.RedirectStandardOutput = true;
34             p.StartInfo.RedirectStandardError = true;
35             p.StartInfo.CreateNoWindow = false;
36 
37             try
38             {
39                 p.Start();
40                 p.WaitForExit();               
41                 p.StandardOutput.ReadToEnd();
42                 p.Close();
43 
44                 FileStream fs = new FileStream(filePath, FileMode.Open);
45                 byte[] file = new byte[fs.Length];
46                 fs.Read(file, 0, file.Length);
47                 fs.Close();
48 
49 
50                 response.Clear();
51                 response.AddHeader("content-disposition", "attachment; filename=" + fileName);
52                 response.ContentType = "application/octet-stream";
53                 response.BinaryWrite(file);
54                 response.Flush();
55                 
56             }
57             catch
58             {
59 
60             }
61         
62         }
63     }
复制代码

 

 对于要打印成pdf的html要求:

    1. dom元素的 height/width/margin/padding以及定位,尽量使用百分比的形式。

    2. 图片的像素质量尽量高一些,因为生成pdf的过程会有缩放。另外加入html的时候设置显示的高度跟宽度。

    3. 对于pdf的分页,可以直接使用一些属性来实现:

          <header page-break-before="always"></header>

         {page-break-before:always;}//添加到header的dom元素实现换页 

          <footer page-break-after="always"></footer>                                  
         {page-break-after:always;}  //添加到footer的dom元素实现换页

                                                    
         {page-break-inside:avoid;} 

 

 

 

 

posted on 2014-06-28 12:55  极简  阅读(583)  评论(2)    收藏  举报
编辑推荐:
· 记一次 .NET 某发证机系统 崩溃分析
· 微服务架构学习与思考:SOA架构与微服务架构对比分析
· tomcat为什么假死了
· 聊一聊 Linux 上对函数进行 hook 的两种方式
· C# 锁机制全景与高效实践:从 Monitor 到 .NET 9 全新 Lock
阅读排行:
· 推荐 3 种 .NET Windows 桌面应用程序自动更新解决方案
· 一周 Star 破万的开源项目「GitHub 热点速览」
· .NET 10 支持Linux/Unix 的Shebang(Hashbang)
· 上周热点回顾(6.9-6.15)
· 记一次 .NET 某SaaS版CRM系统 崩溃分析
点击右上角即可分享
微信分享提示