wkhtmltopdf 生成pdf
2014-06-28 12:55
246 查看
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, 0, 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;}
相关文章推荐
- php用wkhtmltopdf把html生成pdf
- python之使用 wkhtmltopdf 和 pdfkit 批量加载html生成pdf,适用于博客备份和官网文档打包
- linux下使用wkhtmltopdf
- wkhtmltopdf.exe >>html to PDF
- Install wkhtmltopdf On CentOS 6.5
- 为Odoo安装wkhtmltopdf
- Centos 安装了 Wkhtmltopdf 却依旧显示 无法打印pdf
- HTML转换成PDF工具:wkhtmltopdf
- wkhtmltox安装(wkhtmltox包括wkhtmltoimage和wkhtmltopdf)
- java使用wkhtmltopdf实现Html转pdf,并在centos服务器部署步骤。
- wkhtmltoxwkhtmltopdf linux 安装
- 黄聪: PHP WkHtmlToPdf/WkHtmlToImage 将网页直接转换成pdf和图片
- Html2Pdf:C调用wkhtmltopdf的API来将Html转换为pdf文件
- html to pdf - 使用Wkhtmltopdf.exe - 实现,可导出需登录的页面
- Ubuntu 编译 wkhtmltopdf
- Ubuntu Server中安装wkhtmltopdf Odoo最新版打印需要这个
- html转pdf(rails、jruby、ruby) wkhtmltopdf
- wkhtmltopdf将网页转换成PDF
- ODOO Unable To Find Wkhtmltopdf On This System. Error/Bug ?