8

私はphantomJSを調べていて、PDFを生成するための優れたツールになる可能性があるようです. .NET アプリケーションでそれをうまく使った人はいるのだろうか。

私の具体的な質問は、サーバーでrasterize.jsなどのモジュールをどのように使用し、リクエストを受け取り、生成されたpdfを応答として送り返すかです。

私の一般的な質問は、.NET アプリケーションで phantomJS を使用するためのベスト プラクティスはありますか? それを達成するための最良の方法は何ですか?

私は .NET World にかなり慣れていないので、より詳細な回答をいただければ幸いです。みんな、ありがとう。:)

4

3 に答える 3

13

ベスト プラクティスについてはわかりませんが、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を呼び出すための newが作成されます。ExecuteCommand

このExecuteCommandメソッドは、phantomJS を呼び出す責任があります。引数として渡された文字列は、次のことを行います。

phantomJS アプリがある場所に移動し、その後rasterize.js、URL、作成するファイル名、および印刷形式を指定して呼び出します。(ラスタライズの詳細はこちら)。

私の場合、本当に印刷したいのは、actionfiletoupload によって配信されるコンテンツです。シンプルなビューを返すシンプルなアクションです。PhantomJS は、パラメーターとして渡された URL を呼び出し、すべての魔法を実行します。

phantomJS がまだファイルを作成している間は、(おそらく) クライアントからの要求を返すことができません。そしてそれが私がそのDoWhile方法を使用した理由です。ファイルがphantomJSによって作成され、アプリによってリクエストに読み込まれるまで、リクエストを保持します。

于 2013-11-12T15:58:23.980 に答える