*.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.IO;
/// <summary>
///PDFCommon 的摘要说明
/// </summary>
public class PDFCommon
{
public PDFCommon()
{
}
public static string CreatePDF(string url)
{
string fileNameWithOutExtention = Guid.NewGuid().ToString();
string path = System.Web.HttpContext.Current.Server.MapPath(@"~\bin\phantomjs\");
//string paths = System.Web.HttpContext.Current.Server.MapPath(@"~\chartPdfFile");
string savePath = Path.Combine(@"D:/", string.Format("{0}.pdf", Guid.NewGuid()));
string filePath = Path.Combine(path, "phantomjs.exe");
string jsPath = Path.Combine(path, "generate_pdf.js");
string argument = string.Format(" --ignore-ssl-errors=yes {0} \"{1}\" {2}", jsPath, url, savePath);
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();
}
return savePath;
}
}
*.js
// This file is NOT a browser-run javascript but PhantonJS script
var system = require('system');
var address = system.args[1];
var output = system.args[2];
var page = require('webpage').create();
page.paperSize = {
format: 'A4',
orientation: 'landscape',
border: '1cm'
};
page.open(address, function (status) {
if (status !== 'success') {
console.log('Unable to load the address!');
phantom.exit();
} else {
window.setTimeout(function () {
// Remove all low-opacity paths. see PhantomJS issue #364
page.evaluate(function () {
var paths = document.getElementsByTagName("path");
for (var i = paths.length - 1; i >= 0; i--) {
var path = paths[i];
var strokeOpacity = path.getAttribute('stroke-opacity');
if (strokeOpacity != null && strokeOpacity < 0.2)
path.parentNode.removeChild(path);
}
});
page.render(output);
phantom.exit();
}, 5000);
}
});