C# wkhtmltopdf 将html转pdf(详解)
https://www.cnblogs.com/louby/p/905198.html转自,看文章只放了代码看起来云里雾里的,在此做些解析
使用说明:
1.首先呢,得安装下软件,地址下面有链接,文件里有使用说明
2.代码里安装路径得改下,改成你安装的路径,或者把wkhtmltopdf.exe 文件放在网站bin目录下
3.设置下输出路径,转好的pdf得有地方存,代码里是放在了D盘,可以变动
4.就可以使用啦,直接调用HtmlToPdf(string url) url为要转的html路径。
一、转换程序代码如下:
public string HtmlToPdf(string url)
{
bool success = true;
// string dwbh = url.Split('?')[1].Split('=')[1];
//CommonBllHelper.CreateUserDir(dwbh);
//url = Request.Url.Host + "/html/" + url;
string guid = DateTime.Now.ToString("yyyyMMddhhmmss");
string pdfName = "1.pdf";
//string path = Server.MapPath("~/kehu/" + dwbh + "/pdf/") + pdfName;
string path = "D:\\" + pdfName;
try
{
if (string.IsNullOrEmpty(url) || string.IsNullOrEmpty(path))
success = false;
//获取安装路径
string str = Server.MapPath("~\\bin\\wkhtmltopdf.exe");
Process p = System.Diagnostics.Process.Start(str, url+" "+path);
p.WaitForExit();
if (!System.IO.File.Exists(str))
success = false;
if (System.IO.File.Exists(path))
{
FileStream fs = new FileStream(path, FileMode.Open);
byte[] bytes = new byte[(int)fs.Length];
fs.Read(bytes, , bytes.Length);
fs.Close();
if (Request.UserAgent != null)
{
string userAgent = Request.UserAgent.ToUpper();
if (userAgent.IndexOf("FIREFOX", StringComparison.Ordinal) <= )
{
Response.AddHeader("Content-Disposition",
"attachment; filename=" + HttpUtility.UrlEncode(pdfName, Encoding.UTF8));
}
else
{
Response.AddHeader("Content-Disposition", "attachment; filename=" + pdfName);
}
}
Response.ContentEncoding = Encoding.UTF8;
Response.ContentType = "application/octet-stream";
//通知浏览器下载文件而不是打开
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
fs.Close();
System.IO.File.Delete(path);
}
else
{
Response.Write("文件未找到,可能已经被删除");
Response.Flush();
Response.End();
}
}
catch (Exception ex)
{
success = false;
}
return "";
}
文件下载地址:https://download.csdn.net/download/licongzhuo/10591217
一、转换程序代码如下:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
<br> public string HtmlToPdf(string url) { bool success = true; // string dwbh = url.Split('?')[1].Split('=')[1]; //CommonBllHelper.CreateUserDir(dwbh); //url = Request.Url.Host + "/html/" + url; string guid = DateTime.Now.ToString("yyyyMMddhhmmss"); string pdfName = "1.pdf"; //string path = Server.MapPath("~/kehu/" + dwbh + "/pdf/") + pdfName; string path = "D:\\" + pdfName; try { if (string.IsNullOrEmpty(url) || string.IsNullOrEmpty(path)) success = false; //获取安装路径 string str = Server.MapPath("~\\bin\\wkhtmltopdf.exe"); Process p = System.Diagnostics.Process.Start(str, url+" "+path); p.WaitForExit(); if (!System.IO.File.Exists(str)) success = false; if (System.IO.File.Exists(path)) { FileStream fs = new FileStream(path, FileMode.Open); byte[] bytes = new byte[(int)fs.Length]; fs.Read(bytes, 0, bytes.Length); fs.Close(); if (Request.UserAgent != null) { string userAgent = Request.UserAgent.ToUpper(); if (userAgent.IndexOf("FIREFOX", StringComparison.Ordinal) <= 0) { Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(pdfName, Encoding.UTF8)); } else { Response.AddHeader("Content-Disposition", "attachment; filename=" + pdfName); } } Response.ContentEncoding = Encoding.UTF8; Response.ContentType = "application/octet-stream"; //通知浏览器下载文件而不是打开 Response.BinaryWrite(bytes); Response.Flush(); Response.End(); fs.Close(); System.IO.File.Delete(path); } else { Response.Write("文件未找到,可能已经被删除"); Response.Flush(); Response.End(); } } catch (Exception ex) { success = false; } return ""; } |
C# wkhtmltopdf 将html转pdf(详解)的更多相关文章
- 《TCP-IP详解卷1:协议》【PDF】下载
<TCP-IP详解卷1:协议>[PDF]下载链接: https://u253469.pipipan.com/fs/253469-230062539 内容简介<TCP/IP详解卷1:协 ...
- 《TCP-IP详解卷2:实现》【PDF】下载
<TCP-IP详解卷2:实现>[PDF]下载链接: https://u253469.pipipan.com/fs/253469-230062539 内容简介 <TCP/IP详解·卷2 ...
- 《TCP-IP详解卷3:TCP 事务协议、HTTP、NNTP和UNIX域协议》【PDF】下载
TCP-IP详解卷3:TCP 事务协议.HTTP.NNTP和UNIX域协议>[PDF]下载链接: https://u253469.pipipan.com/fs/253469-230062539 ...
- Python开发技术详解PDF
Python开发技术详解(高清版)PDF 百度网盘 链接:https://pan.baidu.com/s/1F5J9mFfHKgwhkC5KuPd0Pw 提取码:xxy3 复制这段内容后打开百度网盘手 ...
- HTML完全使用详解 PDF扫描版
<HTML完全使用详解>根据网页制作的实际特点和目前市场需要,全面系统地介绍了最新的HTML4.01.丰富的实例贯穿全书,能帮助您全面掌握HTML,而且本书所有实例均可直接修改使用,可以提 ...
- 《TensorFlow学习指南深度学习系统构建详解》英文PDF+源代码+部分中文PDF
主要介绍如何使用 TensorFlow 框架进行深度学习系统的构建.涉及卷积神经网络.循环神经网络等核心的技术,并介绍了用于图像数据和文本序列数据的模型.给出了分布式深度学习系统在TensorFlow ...
- 学习《深度学习与计算机视觉算法原理框架应用》《大数据架构详解从数据获取到深度学习》PDF代码
<深度学习与计算机视觉 算法原理.框架应用>全书共13章,分为2篇,第1篇基础知识,第2篇实例精讲.用通俗易懂的文字表达公式背后的原理,实例部分提供了一些工具,很实用. <大数据架构 ...
- Java操作wkhtmltopdf实现Html转PDF
做java开发的都知道,java生成pdf大部分都是用itext,itext的确是java开源组件的第一选择.不过itext也有局限,就是要自己写模版,系统中的表单数量有好几百个,为每个表单做一个导出 ...
- Nginx配置文件nginx.conf中文详解(转)
######Nginx配置文件nginx.conf中文详解##### #定义Nginx运行的用户和用户组 user www www; #nginx进程数,建议设置为等于CPU总核心数. worker_ ...
- Tomcat使用详解
Tomcat简介 官网:http://tomcat.apache.org/ Tomcat GitHub 地址:https://github.com/apache/tomcat Tomcat是Apach ...
随机推荐
- Alpha冲刺! Day5 - 砍柴
Alpha冲刺! Day5 - 砍柴 今日已完成 晨瑶:review了业务逻辑表格,确定了工具类具体函数有哪些:讨论确定了记录的存储方式为HTML. 昭锡:继续学习Gson,并写出json基本操作的D ...
- node及socket.io实现简易websocket双向通信
技术栈: vue2.0 + node + websocket( socket.io ) 1. 安装依赖 初始化vue项目后输入下方指令安装依赖包 // 推荐cnpm安装 npm i vue-socke ...
- Ceph的BlueStore总体介绍
整体架构 bluestore的诞生是为了解决filestore自身维护一套journal并同时还需要基于系统文件系统的写放大问题,并且filestore本身没有对SSD进行优化,因此bluestore ...
- ORB-SLAM2(2) ROS下配置和编译
1配置USB相机 1.1网友参考: http://www.liuxiao.org/2016/07/ubuntu-orb-slam2-%E5%9C%A8-ros-%E4%B8%8A%E7%BC%96%E ...
- 转载 .Net多线程编程—并发集合 https://www.cnblogs.com/hdwgxz/p/6258014.html
集合 1 为什么使用并发集合? 原因主要有以下几点: System.Collections和System.Collections.Generic名称空间中所提供的经典列表.集合和数组都不是线程安全的, ...
- jQuery:自定义函数
<!doctype html><html><head><meta charset="utf-8"><title>< ...
- 2018AVA: A Video Dataset of Spatio-temporally Localized Atomic Visual Actions
论文标题:AVA: A Video Dataset of Spatio-temporally Localized Atomic Visual Actions 来源/作者机构情况: 谷歌,http:// ...
- java中使用jxl读取excel中的数据
package bboss; import java.io.File; import java.io.FileInputStream; import java.io.IOException; impo ...
- ASP.NET Core 使用外部登陆提供程序登陆的流程,以及身份认证的流程 (转载)
阅读目录 在Asp.Net Core 中使用外部登陆(google.微博...) 中间件管道 The Authentication Middleware The Challenge 与认证中间件进行交 ...
- UVA1626 - Brackets sequence(区间DP--括号匹配+递归打印)
题目描写叙述: 定义合法的括号序列例如以下: 1 空序列是一个合法的序列 2 假设S是合法的序列.则(S)和[S]也是合法的序列 3 假设A和B是合法的序列.则AB也是合法的序列 比如:以下的都是合法 ...