Generate PDF, screenshot of a webpage in c# using phantomjs

In this post i will explain how to capture screenshot and how to create PDF of a webpage using Phantomjs.

I searched a lot for html to pdf converter and found some of the solution but they were not rendering webpage currectly and even not supporting css and styling, eventually i found two best solution which supports latest css, styling and render webpage as it is in browsers, first of them is Phantomjs and second is Pechkin.

Here i going to use Phontomjs and in my next article i will explain how to generate PDF from webpage through Pechkin.

Here is little bit about Phontomjs -

PhantomJS is a headless WebKit scriptable with a JavaScript API. It has fast and native support for various web standards: DOM handling, CSS selector, JSON, Canvas, and SVG, this is a command line utility it is always try to start command prompt process to work.

You can find more about phantomjs and how to install it in your project from this link http://phantomjs.org/

Creating PDF of a webpage using phantomjs -

protected void btnPdf_Click(object sender, EventArgs e)
{
  string serverPath = Server.MapPath("~/Phantomjs/");
  string filename = DateTime.Now.ToString("ddMMyyyy_hhmmss") + ".pdf";

  new Thread(new ParameterizedThreadStart(x =>
  {
    ExecuteCommand(string.Format("cd {0} & phantomjs rasterize.js {1} {2} \"A4\"", serverPath, txtUrl.Text, filename));
  })).Start();

  var filePath = Path.Combine(Server.MapPath("~/Phantomjs/"), filename);

  var stream = new MemoryStream();
  byte[] bytes = DoWhile(filePath);

  Response.ContentType = "application/pdf";
  Response.AddHeader("content-disposition", "attachment;filename=Image.pdf");
  Response.OutputStream.Write(bytes, 0, bytes.Length);

}

Capture screenshot of a webpage using phontomjs -

protected void btnScreenShot_Click(object sender, EventArgs e)
{
 string serverPath = Server.MapPath("~/Phantomjs/");
 string filename = DateTime.Now.ToString("ddMMyyyy_hhmmss") + ".png";

 ExecuteCommand(string.Format("cd {0} & phantomjs phantomimage.js {1} {2}", serverPath, txtUrl.Text, filename));

 var filePath = Path.Combine(Server.MapPath("~/Phantomjs/"), filename);

 var stream = new MemoryStream();
 byte[] bytes = DoWhile(filePath);

 Response.AddHeader("content-disposition", "attachment;filename=HtmlToImage.png");
 Response.OutputStream.Write(bytes, 0, bytes.Length);
}

Helper Method -


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 { }
}


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;
}

Html Part -


<asp:TextBox runat="server" ID="txtUrl" Text="http://www.dotnetbull.com"></asp:TextBox>

<asp:Button runat="server" ID="btnPdf" Text="Generate PDF" onclick="btnPdf_Click" />
<asp:Button runat="server" ID="btnImage" Text="Capture Image" onclick="btnScreenShot_Click"/>

Download Sample Code Click Here



Recent Article