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