一. 下载 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的更多相关文章

  1. java调用phantomjs采集ajax加载生成的网页

    java调用phantomjs采集ajax加载生成的网页 日前有采集需求,当我把所有的对应页面的链接都拿到手,准备开始根据链接去采集(写爬虫爬取)对应的终端页的时候,发觉用程序获取到的数据根本没有对应 ...

  2. php结合phantomjs实现网页截屏、抓取js渲染的页面

    首先PhantomJS快速入门 PhantomJS是一个基于 WebKit 的服务器端 JavaScript API.它全面支持web而不需浏览器支持,其快速,原生支持各种Web标准: DOM 处理, ...

  3. C#使用phantomjs 进行网页整页截屏

    C#使用phantomjs 进行网页整页截屏 hantomjs 是一个基于js的webkit内核无头浏览器 也就是没有显示界面的浏览器,这样访问网页就省去了浏览器的界面绘制所消耗的系统资源,比较适合用 ...

  4. 使用selenium+phantomJS实现网页爬取

    有些网站反爬虫技术设计的非常好,很难采用WebClient等技术进行网页信息爬取,这时可以考虑采用selenium+phantomJS模拟浏览器(其实是真实的浏览器)的方式进行信息爬取.之前一直使用的 ...

  5. java 调用 phantomjs

    java 调用 phantomjs 2014-11-21 13:55 2034人阅读 评论(2) 收藏 举报  分类: phantomjs(2)  日前有采集需求,当我把所有的对应页面的链接都拿到手, ...

  6. 利用PhantomJS进行网页截屏

    利用PhantomJS进行网页截屏 关于PhantomJS PhantomJS 是一个基于WebKit的服务器端 JavaScript API.它全面支持web而不需浏览器支持,其快速,原生支持各种W ...

  7. 使用python把html网页转成pdf文件

    我们看到一些比较写的比较好文章或者博客的时候,想保存下来到本地当一个pdf文件,当做自己的知识储备,以后即使这个博客或者文章的连接不存在了,或者被删掉,咱们自己也还有. 当然咱们作为一个coder,这 ...

  8. python下载网页转化成pdf

    最近在学习一个网站补充一下cg基础.但是前几天网站突然访问不了了,同学推荐了waybackmachine这个网站,它定期的对网络上的页面进行缓存,但是好多图片刷不出来,很憋屈.于是网站恢复访问后决定把 ...

  9. (转)C#调用默认浏览器打开网页的几种方法

    转载,原文地址:http://blog.csdn.net/testcs_dn/article/details/42246969 CSharp调用默认浏览器打开网页的几种方法 示例界面: 方法一:从注册 ...

随机推荐

  1. kubernetes第二章--集群搭建

  2. vue计算属性的使用

    props:['name'],//接收父组件的数据 computed:{//当数据发生改变时,会自动去计算 zojia:function(){ //zojia是自己声明的函数 let a = null ...

  3. Java 之 Stream 流

    Stream流 在Java 8中,得益于Lambda所带来的函数式编程,引入了一个全新的Stream概念,用于解决已有集合类库既有的弊端 一.传统遍历 1.传统集合的多步遍历代码 几乎所有的集合(如 ...

  4. Step by Step to create orders by consuming SAP Commerce Cloud Restful API

    Recently Jerry is working on an integration project about creating orders in Wechat platform by cons ...

  5. Python学习日记(九) 装饰器函数

    1.import time a.time.time() 获取到当前的时间,返回值为浮点型 import time print(time.time()) #1565422783.6497557 b.ti ...

  6. 数据库系统load飙高问题解决思路

    工作过程中有时候会接收到数据库服务器器load 飙高的报警,比如:  load1 15.25 base: 8.52,collect time:2014-08-30 如何处理load 异常飙高的报警呢? ...

  7. 非正常卸载Chrome浏览器导致无法重新安装

    昨晚在使用selenium的时候,Chrome浏览器和Chromedriver.exe不兼容,前几天使用的时候都没问题,查看浏览器版本后发现浏览器已经自动升级为67版本(我之前安装的是61版本). 于 ...

  8. 使用Restful风格中的post使用过遇到前端数据传送不到后端

    问题描述:使用postman进行请求时候,前端的数据,一直在后端接收不到,找了好多资料,竟然是因为一个注解的原因. 后端接收设置:@RequestParam@ReqeustBody设置的原因 因为sp ...

  9. vue项目中要实现展示markdown文件[转载]

    转载 版权声明:本文为CSDN博主「齐天二圣」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明.原文链接:https://blog.csdn.net/nihaoa5 ...

  10. sitemap怎么制作才适合蜘蛛抓取?

    网站sitemap制作格式与要求 1.sitemap格式说明 <?xml version="1.0" encoding="utf-8"?> < ...