Skip to content

Commit

Permalink
Add Screenshot Functionality (#322)
Browse files Browse the repository at this point in the history
  • Loading branch information
westy92 authored Apr 19, 2022
1 parent dcfea97 commit 9a327e4
Show file tree
Hide file tree
Showing 8 changed files with 2,090 additions and 284 deletions.
38 changes: 37 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
[![Code Coverage](https://codecov.io/gh/westy92/html-pdf-chrome/branch/master/graph/badge.svg)](https://codecov.io/gh/westy92/html-pdf-chrome)
[![Known Vulnerabilities](https://snyk.io/test/github/westy92/html-pdf-chrome/badge.svg)](https://snyk.io/test/github/westy92/html-pdf-chrome)

HTML to PDF converter via Chrome/Chromium.
HTML to PDF or image (jpeg, png, webp) converter via Chrome/Chromium.

## Prerequisites

Expand Down Expand Up @@ -101,6 +101,42 @@ htmlPdf.create(html, options).then((pdf) => pdf.toStream());

View the full documentation in the source code.

### Saving as a Screenshot

By default, pages are saved as a PDF. To save as a screenshot instead, supply `screenshotOptions`.
All supported options can be viewed [here](https://chromedevtools.github.io/devtools-protocol/tot/Page/#method-captureScreenshot).

```js
const htmlPdf = require('html-pdf-chrome');

const html = '<p>Hello, world!</p>';
const options = {
port: 9222, // port Chrome is listening on
screenshotOptions: {
format: 'png', // png, jpeg, or webp. Optional, defaults to png.
// quality: 100, // Optional, quality percent (jpeg only)

// optional, defaults to entire window
clip: {
x: 0,
y: 0,
width: 100,
height: 200,
scale: 1,
},
},
// Optional. Options here: https://chromedevtools.github.io/devtools-protocol/tot/Emulation/#method-setDeviceMetricsOverride
deviceMetrics: {
width: 1000,
height: 1000,
deviceScaleFactor: 0,
mobile: false,
},
};

htmlPdf.create(html, options).then((pdf) => pdf.toFile('test.png'));
```

### Using an External Site

```js
Expand Down
Loading

0 comments on commit 9a327e4

Please sign in to comment.