Skip to content

nulix-dev/browsershot

Repository files navigation

Convert a webpage to an image or pdf using headless Chrome

Latest Version MIT Licensed run-tests

Note

This package is a Typescript version of the PHP package spatie/browsershot.

The package can convert a webpage to an image or pdf. The conversion is done behind the scenes by Puppeteer which controls a headless version of Google Chrome.

npm i @nulix/browsershot

Here's a quick example:

import { Browsershot } from '@nulix/browsershot'

// an image will be saved
Browsershot.url('https://example.com').save(pathToImage);

It will save a pdf if the path passed to the save method has a pdf extension.

// a pdf will be saved
Browsershot.url('https://example.com').save('example.pdf');

You can also use an arbitrary html input, simply replace the url method with html:

Browsershot.html('<h1>Hello world!!</h1>').save('example.pdf');

If your HTML input is already in a file locally use the :

Browsershot.htmlFromFilePath('/local/path/to/file.html').save('example.pdf');

Browsershot also can get the body of an html page after JavaScript has been executed:

Browsershot.url('https://example.com').bodyHtml(); // returns the html of the body

If you wish to retrieve an array list with all of the requests that the page triggered you can do so:

const requests = Browsershot.url('https://example.com')
    .triggeredRequests();

for (const request as requests) {
    const url = request.url; //https://example.com/
}

To use Chrome's new headless mode pass the newHeadless method:

Browsershot.url('https://example.com').newHeadless().save(pathToImage);

Documentation

All documentation is available on our documentation site.

License

The MIT License (MIT). Please see License File for more information.