public class PdfHelper
{ static string RootPath
{
get
{
string AppPath = "";
HttpContext HttpCurrent = HttpContext.Current;
if (HttpCurrent != null)
{
AppPath = HttpCurrent.Server.MapPath("~");
} return AppPath.Trim(new char[]{'\\'});
}
} public static void GetPdf(HttpResponseBase response,string pdfUrl)
{
string fileName = Guid.NewGuid().ToString("N") + ".pdf";
string filePath = RootPath + "\\pdfs\\" + fileName;
string exePath=RootPath + "\\wkhtmltopdf\\wkhtmltopdf.exe";
string args=pdfUrl + " --zoom 1.25 \"" + filePath+"\""; Process p =new Process();
p.StartInfo.FileName = exePath;
p.StartInfo.Arguments = args;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = false; try
{
p.Start();
p.WaitForExit();
p.StandardOutput.ReadToEnd();
p.Close(); FileStream fs = new FileStream(filePath, FileMode.Open);
byte[] file = new byte[fs.Length];
fs.Read(file, , file.Length);
fs.Close(); response.Clear();
response.AddHeader("content-disposition", "attachment; filename=" + fileName);
response.ContentType = "application/octet-stream";
response.BinaryWrite(file);
response.Flush(); }
catch
{ } }
}

对于要打印成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;}

最新文章

  1. JsonPropertyOrder无法为DTO对象进行属性排序
  2. wordpress 安装 &quot;Table Prefix&quot; must not be empty.
  3. tengine-2.1.0 源码安装
  4. 好看的css3按钮和文本框
  5. jquery------脚注的使用
  6. Android 和iOS中 View的滚动
  7. SVN switch 用法详解
  8. Leeo 智能夜灯:默默守护你的家
  9. Qt 学习之路:Canvas
  10. 自己写的python脚本(抄的别人的,自己改了改,用于整理大量txt数据并插入到数据库)
  11. Word Ladder 2015年6月3日
  12. Java集合(5)一 HashMap与HashSet
  13. awk取每行最大值
  14. 1.9 list 列表
  15. HMM隐马尔科夫算法(Hidden Markov Algorithm)初探
  16. bbs项目学习到的知识点(orm中的extra)
  17. 【ARTS】01_15_左耳听风-20190218~20190224
  18. hihocoder 前两题思路
  19. As3截图转换为ByteArray传送给后台node的一种方法
  20. BZOJ2756 SCOI2012奇怪的游戏(二分答案+最大流)

热门文章

  1. Hadoop平台安装前准备
  2. TCP/IP学习笔记
  3. 监测scroll
  4. [置顶] IT屌丝的离职申请
  5. zkw好写吗
  6. java学习之xml
  7. hadoop源代码解读namenode高可靠:HA;web方式查看namenode下信息;dfs/data决定datanode存储位置
  8. python文件I/O
  9. HashMap,LinkedHashMap,TreeMap的区别(转)
  10. unity学习中经常要碰到的几种数据结构