我一直在研究phantomJS,看起来它可能是一个使用生成PDF的好工具。我想知道是否有人成功地将其用于他们的 .NET 应用程序。
我的具体问题是:您将如何在服务器上使用光栅化.js等模块,接收请求并发回生成的pdf作为响应。
我的一般问题是:是否有将phantomJS与.NET应用程序一起使用的最佳实践。实现它的最佳方法是什么?
我是 .NET World 的新手,我将不胜感激更详细的答案。谢谢大家。:)
最佳实践,但是,我正在使用phantomJS,以下代码没有问题。
public ActionResult DownloadStatement(int id)
{
string serverPath = HttpContext.Server.MapPath("~/Phantomjs/");
string filename = DateTime.Now.ToString("ddMMyyyy_hhmmss") + ".pdf";
new Thread(new ParameterizedThreadStart(x =>
{
ExecuteCommand("cd " + serverPath + @" & phantomjs rasterize.js http://localhost:8080/filetopdf/" + id.ToString() + " " + filename + @" ""A4""");
})).Start();
var filePath = Path.Combine(HttpContext.Server.MapPath("~/Phantomjs/"), filename);
var stream = new MemoryStream();
byte[] bytes = DoWhile(filePath);
return File(bytes, "application/pdf", filename);
}
private void ExecuteCommand(string Command)
{
try
{
ProcessStartInfo ProcessInfo;
Process Process;
ProcessInfo = new ProcessStartInfo("cmd.exe", "/K " + Command);
ProcessInfo.CreateNoWindow = true;
ProcessInfo.UseShellExecute = false;
Process = Process.Start(ProcessInfo);
}
catch { }
}
public ViewResult FileToPDF(int id)
{
var viewModel = file.Get(id);
return View(viewModel);
}
private byte[] DoWhile(string filePath)
{
byte[] bytes = new byte[0];
bool fail = true;
while (fail)
{
try
{
using (FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
bytes = new byte[file.Length];
file.Read(bytes, 0, (int)file.Length);
}
fail = false;
}
catch
{
Thread.Sleep(1000);
}
}
System.IO.File.Delete(filePath);
return bytes;
}
以下是操作流程:
用户单击指向DownloadStatement Action
的链接。在其中,创建了一个新Thread
来调用 ExecuteCommand
方法。
ExecuteCommand
方法负责调用 phantomJS。作为参数传递的字符串执行以下操作。
转到phantomJS应用程序所在的位置,然后使用URL,要创建的文件名和打印格式调用rasterize.js
。(有关栅格化的更多信息,请点击此处)。
就我而言,我真正想要打印的是action
文件上传提供的内容。这是一个返回简单视图的简单操作。PhantomJS将调用作为参数传递的URL,并执行所有魔术。
虽然 phantomJS 仍在创建文件,(我猜)我无法返回客户端发出的请求。这就是我使用DoWhile
方法的原因。它将保留请求,直到文件由 phantomJS 创建并由应用程序加载到请求中。
如果你愿意使用NReco.PhantomJS,它为PhantomJS提供了一个.NET包装器,你可以非常简洁地做到这一点。
public async Task<ActionResult> DownloadPdf() {
var phantomJS = new PhantomJS();
try {
var temp = Path.Combine(Path.GetTempPath(),
Path.ChangeExtension(Path.GetRandomFileName(), "pdf")); //must end in .pdf
try {
await phantomJS.RunAsync(HttpContext.Server.MapPath("~/Scripts/rasterize.js"),
new[] { "https://www.google.com", temp });
return File(System.IO.File.ReadAllBytes(temp), "application/pdf");
}
finally {
System.IO.File.Delete(temp);
}
}
finally {
phantomJS.Abort();
}
}
以下是使用Phantom生成PDF的一些非常基本的代码.JS但您可以在此处找到更多信息:https://buttercms.com/blog/generating-pdfs-with-node
var webPage = require('webpage');
var page = webPage.create();
page.viewportSize = { width: 1920, height: 1080 };
page.open("http://www.google.com", function start(status) {
page.render('google_home.pdf, {format: 'pdf', quality: '100'});
phantom.exit();
});