ベスト プラクティスについてはわかりませんが、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、作成するファイル名、および印刷形式を指定して呼び出します。(ラスタライズの詳細はこちら)。
私の場合、本当に印刷したいのは、action
filetoupload によって配信されるコンテンツです。シンプルなビューを返すシンプルなアクションです。PhantomJS は、パラメーターとして渡された URL を呼び出し、すべての魔法を実行します。
phantomJS がまだファイルを作成している間は、(おそらく) クライアントからの要求を返すことができません。そしてそれが私がそのDoWhile
方法を使用した理由です。ファイルがphantomJSによって作成され、アプリによってリクエストに読み込まれるまで、リクエストを保持します。