一. 下载 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. NIO开发Http服务器(4):Response封装和响应

    最近学习了Java NIO技术,觉得不能再去写一些Hello World的学习demo了,而且也不想再像学习IO时那样编写一个控制台(或者带界面)聊天室.我们是做WEB开发的,整天围着tomcat.n ...

  2. requests模块 简单使用

    目录 requests模块 简单使用 Anaconda简单了解 简单了解 requests模块 使用requests模块 爬取搜狗首页源码数据 实现一个简易的网页采集器 解决乱码问题 解决UA检测问题 ...

  3. Vue项目中使用svg图标

    svg的图片有着颜色自定义大小自定义的优势.在阿里图标库可以下载svg图片. 那么在vue框架中我们该怎么使用svg图片呢 这个时候就用到了 webpack 插件 svg-sprite-loader ...

  4. iOS应用状态保存和恢复

    当应用被后台Kill掉的时候希望从后台返回的时候显示进入后台之前的内容 在Appdelegate中设置 - (BOOL)application:(UIApplication *)application ...

  5. Linux实验:ssh免密码配置

    [实验目的]    1)了解ssh工具的作用    2)熟悉ssh配置过程    3)理解ssh原理[实验原理]    SSH是目前比较可靠的专为远程登录会话和其他网络服务提供安全的协议.不同主机之间 ...

  6. MySQL增加行号rownum

    select * from ( select @rownum:=@rownum+1 AS rownum, app_t.* from ( select * from app_custom where 1 ...

  7. UVA816 Abbott's Revenge (三元组BFS)

    题目描述: 输入输出: 输入样例: SAMPLE 3 1 N 3 3 1 1 WL NR * 1 2 WLF NR ER * 1 3 NL ER * 2 1 SL WR NF * 2 2 SL WF ...

  8. python3 xml_to_dict、dict_to_xml等互相转换的方法

    from basic_config_vars.config_vars import moviesAllFilePath,moviesFilePath #自定义的一些文件变量 import xmltod ...

  9. 1206 BBS注册

    目录 昨日内容 BBS项目 1.项目开发流程 2.表设计 用户表 个人站点表 文章标签表 文章分类表 文章表 文章的点赞点踩表 文章的评论表 项目 昨日内容 昨日内容 基于django中间件实现功能的 ...

  10. discuz论坛门户资讯入库接口【原创】

    最近想打造一个社区门户站点,所以写了这个入库接口,可以对接数据入库. <?php /* * Discuz x3.2 门户免登陆发布接口 * 2018-08-10 * Copyright 68xi ...