c#调用phantomjs 将 网页 存为 PDF
一. 下载 phantomjs
具体下载方式 不再详细说明了.
二. 创建一个 rasterize.js 文件 (放在哪里都行, 我这里是放在了项目中) . 代码内容如下
var page = require('webpage').create(),
system = require('system'),
address, output, size;
if (system.args.length < 3 || system.args.length > 5) {
console.log('Usage: rasterize.js URL filename [paperwidth*paperheight|paperformat] [zoom]');
console.log(' paper (pdf output) examples: "5in*7.5in", "10cm*20cm", "A4", "Letter"');
phantom.exit(1);
} else {
address = system.args[1];
output = system.args[2];
page.viewportSize = { width: 600, height: 600 };
if (system.args.length > 3 && system.args[2].substr(-4) === ".pdf") {
size = system.args[3].split('*');
page.paperSize = size.length === 2 ? { width: size[0], height: size[1], margin: '0px' }
: { format: system.args[3], orientation: 'portrait', margin: '1cm' };
}
if (system.args.length > 4) {
page.zoomFactor = system.args[4];
}
page.open(address, function (status) {
if (status !== 'success') {
console.log('Unable to load the address!');
phantom.exit();
} else {
window.setTimeout(function () {
page.render(output);
console.log("ok");
phantom.exit();
}, 200);
}
});
}
三 . C# 调用 phantomjs
/// <summary>
/// 生成PDF
/// </summary>
/// <param name="relativeUrl">html网址</param>
/// <param name="pdfName">PDF文件名称前缀</param>
/// <param name="isA4">纸张大小是否使用A4 (默认:0:不使用 1:使用)</param>
/// <returns></returns>
private static string[] GeneratePdf(string relativeUrl, string pdfName, int isA4 = 0)
{
try
{
//需要下载的 投保单页面地址
string url = relativeUrl;
//pdf名称(文件名后面 增加一个时间 防止 文件名重复)
string fileNameWithOutExtention = pdfName + DateTime.Now.ToString("_yyyyMMdd_HHmmssffff");
// 保存文件的 完整 路径 (本地地址+文件名.pdf)
string pdfSavePath = Path.Combine("自己写路径", string.Format("{0}.pdf", fileNameWithOutExtention));
//exe地址 (自己写路径,文件夹放在哪里就写哪. 例如:E:\conf)
string filePath = Path.Combine("自己写路径", "phantomjs.exe");
//rasterize.js 地址
string jsPath = Path.Combine("自己写路径", "rasterize.js");
//传递的参数 (0:js地址 1:打印的URL地址 2:PDF保存地址 3:打印A4纸尺寸) (对应rasterize.js文件中的参数数组, 例如:system.args[0],system.args[1],system.args[2] ..... 可以取出对应的值 )
string argument = string.Empty;
if (isA4 != 0)
{
//0位置的jspath 是给 exe 提供的 js地址, 后面的 123 是参数. 所以 js中并未使用system.args[0]
argument = string.Format(" --ignore-ssl-errors=yes {0} \"{1}\" {2} \"{3}\" ", jsPath, url, pdfSavePath, "A4");
}
else
{
argument = string.Format(" --ignore-ssl-errors=yes {0} \"{1}\" {2} ", jsPath, url, pdfSavePath);
}
//调用 工具
using (System.Diagnostics.Process exep = new System.Diagnostics.Process())
{
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.FileName = filePath;
startInfo.Arguments = argument;
startInfo.CreateNoWindow = true;
startInfo.UseShellExecute = false;
startInfo.RedirectStandardInput = true;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
exep.StartInfo = startInfo;
exep.Start();
exep.WaitForExit();
char[] res = new char[2];
exep.StandardOutput.Read(res, 0, res.Length);
string sRes = new string(res);
if (sRes == "ok")
{
//成功后的操作
}
}
return new string[] { pdfSavePath };
}
catch (Exception e)
{
//log
}
return new string[] { null, null };
}
c#调用phantomjs 将 网页 存为 PDF的更多相关文章
- java调用phantomjs采集ajax加载生成的网页
java调用phantomjs采集ajax加载生成的网页 日前有采集需求,当我把所有的对应页面的链接都拿到手,准备开始根据链接去采集(写爬虫爬取)对应的终端页的时候,发觉用程序获取到的数据根本没有对应 ...
- php结合phantomjs实现网页截屏、抓取js渲染的页面
首先PhantomJS快速入门 PhantomJS是一个基于 WebKit 的服务器端 JavaScript API.它全面支持web而不需浏览器支持,其快速,原生支持各种Web标准: DOM 处理, ...
- C#使用phantomjs 进行网页整页截屏
C#使用phantomjs 进行网页整页截屏 hantomjs 是一个基于js的webkit内核无头浏览器 也就是没有显示界面的浏览器,这样访问网页就省去了浏览器的界面绘制所消耗的系统资源,比较适合用 ...
- 使用selenium+phantomJS实现网页爬取
有些网站反爬虫技术设计的非常好,很难采用WebClient等技术进行网页信息爬取,这时可以考虑采用selenium+phantomJS模拟浏览器(其实是真实的浏览器)的方式进行信息爬取.之前一直使用的 ...
- java 调用 phantomjs
java 调用 phantomjs 2014-11-21 13:55 2034人阅读 评论(2) 收藏 举报 分类: phantomjs(2) 日前有采集需求,当我把所有的对应页面的链接都拿到手, ...
- 利用PhantomJS进行网页截屏
利用PhantomJS进行网页截屏 关于PhantomJS PhantomJS 是一个基于WebKit的服务器端 JavaScript API.它全面支持web而不需浏览器支持,其快速,原生支持各种W ...
- 使用python把html网页转成pdf文件
我们看到一些比较写的比较好文章或者博客的时候,想保存下来到本地当一个pdf文件,当做自己的知识储备,以后即使这个博客或者文章的连接不存在了,或者被删掉,咱们自己也还有. 当然咱们作为一个coder,这 ...
- python下载网页转化成pdf
最近在学习一个网站补充一下cg基础.但是前几天网站突然访问不了了,同学推荐了waybackmachine这个网站,它定期的对网络上的页面进行缓存,但是好多图片刷不出来,很憋屈.于是网站恢复访问后决定把 ...
- (转)C#调用默认浏览器打开网页的几种方法
转载,原文地址:http://blog.csdn.net/testcs_dn/article/details/42246969 CSharp调用默认浏览器打开网页的几种方法 示例界面: 方法一:从注册 ...
随机推荐
- Redis安装--CentOS7上安装Redis
echo编辑整理,欢迎转载,转载请声明文章来源.欢迎添加echo微信(微信号:t2421499075)交流学习. 百战不败,依不自称常胜,百败不颓,依能奋力前行.--这才是真正的堪称强大!!! 1.R ...
- js 移动端之监听软键盘弹出收起
js 移动端关于页面布局,如果底部有position:fixed的盒子,又有input,当软键盘弹出收起都会影响页面布局.这时候Android可以监听resize事件,代码如下,而ios没有相关事件. ...
- javascript原型链[图]
- mysql 的使用
1. 安装 https://dev.mysql.com/downloads/mysql/ 2. 配置 $ vim ~/.bash_profile $ export PATH=$PATH:/usr/lo ...
- MySQL Backup--Xtrabackup备份设置锁等待问题
问题描述 innobackupex备份过程需要保证备份数据一致性,通过刷新表缓存和加全局读锁(FLUSH TABLES WITH READ LOCK)获取备份位点,而为防止锁等待超时,会先设置: SE ...
- MySQL连接查询流程源码
http://blog.itpub.net/29510932/viewspace-2129300/ 初始化: 点击(此处)折叠或打开 main |-mysqld |-my_init // 初始话线程变 ...
- stdClass 标准
在WordPress中很多地方使用stdClass来定义一个对象(而通常是用数组的方式),然后使用get_object_vars来把定义的对象『转换』成数组. 如下代码所示: $tanteng = n ...
- 新添加的磁盘大于2T 的分区方法
环境CentOS7.1 2.9t磁盘 fdisk 只能分区小于2t的磁盘,大于2t的话,就要用到parted 1,将磁盘上原有的分区删除掉: 进入:#parted /dev/sdb 查看:(par ...
- 0032ActiveMQ之java编码实现生产者和消费者操作队列queue
今天学习了入门级的用java编写生产者producer和消费者consumer操作activemq的queue队列,为了之后复习回顾,现做整理如下: maven工程的搭建此处不再讲解,在maven工程 ...
- MyBatis-Plus-Generator配置
本文仅对使用MyBatis-Plus的代码生成器配置做保存,适合使用了该插件的童鞋做参考. 内部有大量默认配置,有性趣的童鞋可以研究下源码. ps:官方文档更齐全http://mp.baomidou. ...