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调用默认浏览器打开网页的几种方法 示例界面: 方法一:从注册 ...
随机推荐
- Python 3 MySQL数据库操作
import pymysql class Mysql_db(): def __init__(self,ip,username,password,db_name,table_name): self.ip ...
- iOS - 外加字体(只需三步-教你轻松实现)
外加字体 1.首先info.plist中加入属性Fonts provided by application,在item 0 处填写导入的ttf文件名 eg: <key>UIAppFonts ...
- 【转载】C#使用Math.Floor方法来向下取整
在C#的数值运算中,有时候需要对计算结果舍去小数位保留整数位向下取整即可,此时就可使用内置方法Math.Floor来实现向下取整操作,Math.Floor方法将舍去小数部分,保留整数.Math.Flo ...
- 【转载】C#中使用List集合的Insert方法在指定位置插入数据
在C#的List集合等数据类型变量中,我们可以使用List集合的Insert方法在指定的索引位置插入一个新数据,例如指定在List集合的第一个位置写入一个新数据或者在List集合的中间某个位置插入个新 ...
- Android里的Dalvik、ART、JIT、AOT有什么关系?
JIT,Just-in-time,即时编译,边运行边编译: AOT,Ahead Of Time,提前编译,指运行前编译. 区别 这两种编译方式的主要区别在于是否在“运行时”进行编译 优劣JIT优点: ...
- Python学习日记(六) 浅深copy
浅深copy即完全复制一份和部分复制一份 浅深copy在列表数据量较大时不建议使用,比较消耗内存资源 1.赋值运算 l1 = [1,'s',[1,2,3]] l2 = l1 print(id(l1), ...
- angularcli 第二篇(数据绑定、属性申明、绑定属性)
1.数据绑定 插值绑定: <li>{{ hero.name }} </li> 或 <div class = "{{ demo }}&q ...
- Oracle IMP-00010 不是有效的导出文件,标头验证失败 解决方法
用IMP导入dmp文件时,出现IMP-00010 不是有效的导出文件,标头验证失败问题. 第一种:网上搜索到的大多解决方法是说导出文件时使用的Oracle版本不一致问题,需要修改dmp文件的版本号.如 ...
- Andrew Ng机器学习 二: Logistic Regression
一:逻辑回归(Logistic Regression) 背景:假设你是一所大学招生办的领导,你依据学生的成绩,给与他入学的资格.现在有这样一组以前的数据集ex2data1.txt,第一列表示第一次测验 ...
- [2019/05/17]解决springboot测试List接口时JSON传参异常
报错信息,大致如下 c.c.c.c.a.BaseControllerExceptionHandler : 运行时异常: java.lang.IllegalStateException: No prim ...