Modules configuration screen

The module comes with an integration to PhantomJS which allows you to take screenshots or PDFs of a URL.

The module comes with an administration interface that allows you to configure the location of PhantomJS and a default destination folder. It also has a test function to test if your configuration works. PhantomJS works by giving phantom an JavaScript file that tells phantom what to do and this module comes with a default JavaScript file that renders full HD size screenshots.

For more information about PhantomJS possibilities you should look at the wiki pages and the examples that comes with PhantomJS.

Drupal 7

The module comes with a simple field that allows users to enter a absolute URL to a page they want to capture and the field provides a display of the captured site which can be displayed using image style and the image may link to the site captured.

To use this module in your code the phantomjs_capture_screen can be called after you have saved its configuration.

The $url is the page to capture and the $dest is an URI to where the image should be stored and the $filename to store it under.

  phantomjs_capture_screen($url, $dest, $filename);

phantomjs_capture_screen can also be used on its own in custom implementations. Here is an example (7.x) where a user has clicked on a 'print page' button, which is hitting a callback to capture a part of the page as PNG and serve it in the browser, clean up and exit:


  // take the picture, serve it, then delete it
  $pic = phantomjs_capture_screen($url, 'public://phantomjs', REQUEST_TIME . '.' . $format);

  if ($pic) {
    $filepath = file_create_url('public://phantomjs/' . REQUEST_TIME . '.' . $format);
    header("Content-Type: application/octet-stream");
    header("Content-Disposition: attachment; filename = screenshot.$format");
    header("Content-Transfer-Encoding: binary");

    readfile($filepath);
    drupal_unlink('public://phantomjs/' . REQUEST_TIME . '.' . $format);
  } else {
    watchdog('phantomjs_capture', 'Failed to capture image.');
  }

  exit;

Drupal 8

The Drupal 8 version contains various underlying changes for flexibility, and can be used in one of the following ways:

You can still call phantomjs_capture_screen, but it will be deprecated in a future release. It merely calls the global service container to access the PhantomJSCaptureHelper capture method.

You can (in your own code), call it like this:

  \Drupal::service('phantomjs_capture.helper')
     ->capture($url, $destination, $filename, $element = NULL);

Note that in the 8.x version, $url must be an instance of Url, and not a string.

You can inject it into your class when it is instantiated:


 /**
   * MyClass constructor.
   * @param PhantomJSCaptureHelperInterface $capture_helper
   */
  public function __construct(PhantomJSCaptureHelperInterface $capture_helper) {
    $this->captureHelper = $capture_helper;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container->get('phantomjs_capture.helper'));
  }

  /**
   * Take a capture and do other actions in a request.
   */
  public function someMethod(Url $url, $destination, $filename) {
    $this->captureHelper->capture($url, $destination, $filename);
    // do something
  }

In future releases, there will be an effort to make it so you can have multiple config objects. Right now, there is only the global config for PhantomJS Capture. It will be extended to support viewport size, file format arguments and other configuration.

For developers, if you want to branch off and do your own thing, simply implement the PhantomJSCaptureHelperInterface class, and create your own service definitions.

Requirements

The module requires that PhantomJS is installed on your server. See the PhatomJS download page.

Providing your own capture script

Underneath the PHP API, the PhantomJS binary is executing instructions in capture.js of the module, a driver for the PhantomJS WebPage module. A developer can create their own JavaScript file to utilize the WebPage module to their liking, just change the config option that points to the script to use.

This might be necessary in cases which, for instance, you have exact specifications for capturing to PDF - for which the script has no way yet to receive options from the Drupal configuration (paper size, margins, portrait/landscape, etc).

Supporting organizations: 
Support & Development

Project Information

Downloads