对生成.NET应用程序使用幻影JS PDF文件 [英] Generating PDFs using Phantom JS on .NET applications

查看:109
本文介绍了对生成.NET应用程序使用幻影JS PDF文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找到phantomJS,看起来像它可能是使用PDF文件生成一个伟大的工具。我不知道是否有人已经成功地使用它自己的.NET应用程序。

I have been looking into phantomJS and looks like it could be a great tool to use generating PDFs. I wonder if anyone have successfully used it for their .NET applications.

我的具体问题是:你将如何使用像 rasterize.js 模块上服务器,接收请求,并发回生成PDF文件作为响应。

My specific question is: how would you use modules like rasterize.js on the server, receive requests and send back generated pdfs as a response.

我一般的问题是:有没有使用phantomJS与.NET应用程序的最佳实践。这将是实现这一目标的最佳途径?

My general question is: is there any best practice for using phantomJS with .NET Applications. What would be the best way to achieve it?

我在.NET世界非常新,我愿意AP preciate更详细的解答。感谢大家。 :)

I am fairly new in .NET World and I would appreciate the more detailed answers. Thanks everyone. :)

推荐答案

我不知道的最佳做法,但是,我使用phantomJS具有如下code没有问题。

I don't know about best practices, but, I'm using phantomJS with no problems with the following code.

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操作上。里面有一个新的创建调用 ExecuteCommand 方法。

The user clicks on a link to DownloadStatement Action. Inside there, a new Thread is created to call the ExecuteCommand method.

ExecuteCommand 方法负责调用phantomJS。作为参数传递的字符串做好以下工作。

The ExecuteCommand method is responsible to call phantomJS. The string passed as an argument do the following.

进入位置,在phantomJS应用程序是,此后,称之为 rasterize.js 与URL,要创建的文件名和打印格式。 (更多关于光栅化这里)。

Go to the location where the phantomJS app is and, after that, call rasterize.js with an URL, the filename to be created and a print format. (More about rasterize here).

在我的情况,我真的想打印是由动作 filetoupload传送的内容。这是一个简单的动作,它返回一个简单的观点。 PhantomJS将调用为参数传递的URL,做所有的魔法。

In my case, what I really want to print is the content delivered by the action filetoupload. It's a simple action that returns a simple view. PhantomJS will call the URL passed as parameter and do all the magic.

虽然phantomJS仍然是创建文件,(我想)我不能返回客户端的请求。这就是为什么我用 DoWhile 方法。直到该文件是由phantomJS创建和应用程序的请求加载它会一直保持请求。

While phantomJS is still creating the file, (I guess) I can not return the request made by the client. And that is why I used the DoWhile method. It will hold the request until the file is created by phantomJS and loaded by the app to the request.

这篇关于对生成.NET应用程序使用幻影JS PDF文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆