Create PDF files using Phantom JS applications in .NET

I was looking for phantomJS and it seems to be a great tool for using PDF generating files. Interestingly, someone successfully used it for their .NET applications.

My specific question is: how would you use modules like rasterize.js on the server to receive requests and send back the created pdf files in response.

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

I am new to .NET World and I would appreciate more detailed answers. Thanks to all.:)

+8
asp.net-mvc phantomjs
source share
3 answers

I do not know about best practices, but I use phantomJS without 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; } 

Here is the flow of action:

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

The ExecuteCommand method ExecuteCommand responsible for calling phantomJS. The string passed as an argument does the following.

Go to the place where the phantomJS application is located, and then call rasterize.js with the URL created by the file name and print format. ( Read more about rasterization here ).

In my case, what I really want to print is the content provided by the action filetoupload file. This is a simple action that returns a simple look. PhantomJS will call the URL passed as a parameter and do all the magic.

While phantomJS is still creating the file (I think), I cannot return the request made by the client. This is why I used the DoWhile method. It will keep the request until the file is created by phantomJS and loaded by the application into the request.

+13
source share

If you are open to using NReco.PhantomJS , which provides the .NET wrapper for PhantomJS, you can do it very succinctly.

 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(); } } 
+1
source share
 var page = require('webpage').create(); page.open('http:/www.google.com', function () { page.render('c:\test.pdf'); phantom.exit(); }); 

Here you can see an example of Capturing a web page screen in different formats.

-2
source share

All Articles