PhantomJS wrapper for .NET (C#) Run javascript from .NET code


features

  • Execute javascript, load & render HTML pages with PhantomJS tool (headless browser) from .NET application. Useful for executing javascript code on server-side in real web context.
  • Wrapper provides .NET API for:
    • running PhantomJS process (from a file or just javascript code string)
    • setting input data through stdin with .NET Stream
    • reading result (text or binary) from stdout into .NET Stream
    • events for handling console log and js errors
  • Easy to use: just install NReco.PhantomJS nuget package
  • Usage examples (C#):
    • ReadConsoleLog:
      • how to pass arguments from C# code (and get them in javascript code)
      • how to read console.log in C# code (produced by javascript code)
    • StdinStdout:
      • how to provide input from C# Stream into javascript code
      • how to read stdout into C# Stream from javascript code
      • execute rasterize.js from C# code
    • SvgBatch (commercial package only):
      • how to convert several SVG (or HTML) files to image (PNG,JPG,PDF) with one PhantomJS call
      • how to convert a lot of SVG files with several PhantomJS processes with 100% CPU utilization
    • RenderPdf (commercial package only):
      • implements sample HTML-to-PDF converter that uses PhantomJS for rendering web page to PDF file
      • supports various PDF generation options: page size, orientation, margin, header/footer with page numbering
  • Looking for specialized HTML to PDF converter? Try NReco.PdfGenerator

download and pricing

quick purchase process

  • 1 Choose a package
  • 2 Pay online Online payment methods
  • 3 Download the package

PhantomJS can be used for web scraping, rendering web page with SVG/Canvas to image (get web page thumbnail) or PDF and other server-side page automation.

Don't spend your time and money on PhantomJS integration: it's already done!

how to use

  1. Install NReco.PhantomJS nuget package
  2. Execute javascript with one line of C# code:
    var phantomJS = new PhantomJS();
    phantomJS.OutputReceived += (sender, e) => {
    	Console.WriteLine("PhantomJS output: {0}", e.Data);
    };
    phantomJS.RunScript("for (var i=0; i<10; i++) console.log('hello from js '+i); phantom.exit();", null);
    
  3. That's all! Next steps:
Have a question? Feel free to ask.

online PhantomJS demo

var system = require('system');
var page = require('webpage').create();
page.open(system.args[1], function(status) {
	var divCount = page.evaluate(function() {
		return document.getElementsByTagName('DIV').length;
	});
	console.log(divCount); phantom.exit();
});
Execute Javascript on Server

frequently asked questions

NReco.PhantomJS wrapper can be used for FREE in single-deployment projects (websites, intranet/extranet) or applications for company's internal business purposes (redistributed only internally inside the company).

Commercial license is required for:
  • Applications for external redistribution (ISV)
  • SaaS deployments
  • .NET Core build (requires commercial license key)
Note that support is NOT available for free users.
Component cannot be used (does not work) in the following cases:
  • partial-trust environments where starting new processes is prohibited (shared ASP.NET hostings)
  • special Windows Server configurations with restricted access to GDI API. Azure Apps (former WebSites) are NOT supported!
  • .NET platforms where System.Diagnostics.Process API is not available (UWP/Univeral Apps)
PhantomJS wrapper works fine with usual Azure VM instances (Web Role, Worker Role) and Azure Service Fabric.

NReco.PhantomJS nuget package has dependency on PhantomJS package that copies phantomjs.exe to project output directory (and PhantomJS wrapper uses it from that location by default). You you decided to use different location for phantomjs.exe you may specify it with ToolPath property.

Wrapper can be used from .NET Core applications: portable build that targets .NET Standards 1.5 is included into commercial package.

PhantomJS can render web pages to PDF (including SVG and canvas! Most of js charts may be rendered correctly) but result is highly depends on the HTML layout. There are number of known issues with page breaks and links; also PDF can look a bit different in comparing to the browser view.
If you're looking for the component that can convert almost any existing web page to the nice-looking PDF consider using NReco PdfGenerator: this is .NET wrapper for wkhtmltopdf tool which provides much better PDF results.


RenderPdf example (included into commercial package) implements simple Html-to-PDF converter class that uses PhantomJS and supports the following PDF export options:
  • page size, orientation and margins
  • header/footer (defined with HTML templates) with page numbering
  • extra options like zoom factor or javascript delay

what's new

2023 May 05 v.1.1.0 changes:
  • added netstandard2.0 / net45 builds for better compatibility with modern .NET versions
  • removed legacy targets (netstandard1.5 / net40)
2017 Jun 02 v.1.0.4 changes:
  • workaround for "No process is associated with this object" exception on Process.HasExited check
  • added .NET Core-compatible (netstandard1.5) build into nuget package (requires commercial license key)
2016 Feb 24 v.1.0.3 changes:
  • fixed exception handling issue with RunScriptAsync
  • now PhantomJS implements IDisposable (Dispose kills phantomjs process if needed)
  • now NReco.PhantomJS is a signed assembly
  • Nuget PhantomJS package dependency changed to latest 2.1.1 (wrapper still can work with 2.0.0)
  • added RenderPdf example that illustrates how to use PhantomJS for rendering web pages to PDF (with page header and footer).
2015 Aug 03 v.1.0.2 changes:
  • added RunAsync and RunScriptAsync overloads for asynchronous PhantomJs execution
  • added WriteLine and WriteEnd methods for writing data to PhantomJS stdin (alternative to providing input stream)
  • added TempFilesPath property for specifying custom temp files location (in some environments Path.GetTempPath is not writable).
2015 Apr 05 v.1.0.1 changes:
  • fixed issue with resolving default phantomjs.exe location
  • fixed issue that prevents stdin from working
2015 Apr 3 v.1.0: Initial NReco.PhantomJS release (uses PhantomJS 2.0)

more components

  • HTML-to-PDF Generator

    .NET wrapper for WkHtmlToPdf utility that generates PDF reports by HTML template.

  • Image Generator

    .NET wrapper WkHtmlToImage utility that generate pretty-looking images by HTML layout.

  • PivotData Toolkit for .NET

    .NET components for manipulating multidimensional dataset (in-memory data cube) without OLAP server, aggregating huge datasets, calculating data for pivot tables / charts, web pivot table control for ASP.NET MVC/Blazor.

  • VideoConverter

    .NET wrapper for FFMpeg utility for converting media files (video, audio). Can be used for video thumbnail extraction, live media stream transcoding etc.