一. 下载 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. 5_PHP数组_3_数组处理函数及其应用_9_数组集合运算函数

    以下为学习孔祥盛主编的<PHP编程基础与实例教程>(第二版)所做的笔记. 数组集合运算函数 1. array_merge() 函数 程序: <?php $array1 = array ...

  2. SAP云平台CloudFoundry环境里route 超过quota的错误处理

    试图往SAP Cloud Platform CloudFoundry用命令行CLI部署应用时,遇到如下错误: 原因是因为这个新建的名为Haytham的subaccount没有分配application ...

  3. Python JSON的简单使用

    1          json简介 1.1         json是什么? JSON(JavaScript Object Notation)是一种轻量级的数据交换格式. “在JSON出现之前,大家一 ...

  4. java线程的生命周期及五种基本状态

    一.线程的生命周期及五种基本状态 关于Java中线程的生命周期,首先看一下下面这张较为经典的图: 上图中基本上囊括了Java中多线程各重要知识点.掌握了上图中的各知识点,Java中的多线程也就基本上掌 ...

  5. RobotFramework+Eclipse的安装和配置(一)

    最近想学robotframwork来做自动化,那立马就来开始上手 想动手,起码要先下载工具,工具及框架 工具介绍 Robotframework:一款自动化测试框架. Eclipse:一款编辑工具,可以 ...

  6. mysql8.x 新版本jdbc连接方式

    旧版本,MySQL Connector/J 5.x 版本的连接方式:url = jdbc:mysql://localhost:3306/thrcloud_db01?useUnicode=true&am ...

  7. 使用FastJSON 对Map/JSON/String 进行互转

    Fastjson是一个Java语言编写的高性能功能完善的JSON库,由阿里巴巴公司团队开发的主要特性主要体现在以下几个方面: 1.高性能 fastjson采用独创的算法,将parse的速度提升到极致, ...

  8. Linux命令——tree

    参考:Linux tree Command Tutorial for Beginners (6 Examples) 简介 Linux tree命令用于以树状图列出目录的内容. 执行tree指令,它会列 ...

  9. Bash基础——减号-

    参考:Bash基础——pipe pipe命令在 bash 的连续的处理程序中相当重要.在pipe命令当中,常常会使用到前一个命令的 stdout 作为这次的 stdin , 某些命令需要用到文件名 ( ...

  10. HTML&CSS基础-内联和块元素

    HTML&CSS基础-内联和块元素 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.html源代码 <!DOCTYPE html> <html> ...