Skip to content

Commit 9a327e4

Browse files
authored
Add Screenshot Functionality (#322)
1 parent dcfea97 commit 9a327e4

File tree

8 files changed

+2090
-284
lines changed

8 files changed

+2090
-284
lines changed

README.md

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
[![Code Coverage](https://codecov.io/gh/westy92/html-pdf-chrome/branch/master/graph/badge.svg)](https://codecov.io/gh/westy92/html-pdf-chrome)
77
[![Known Vulnerabilities](https://snyk.io/test/github/westy92/html-pdf-chrome/badge.svg)](https://snyk.io/test/github/westy92/html-pdf-chrome)
88

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

1111
## Prerequisites
1212

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

102102
View the full documentation in the source code.
103103

104+
### Saving as a Screenshot
105+
106+
By default, pages are saved as a PDF. To save as a screenshot instead, supply `screenshotOptions`.
107+
All supported options can be viewed [here](https://chromedevtools.github.io/devtools-protocol/tot/Page/#method-captureScreenshot).
108+
109+
```js
110+
const htmlPdf = require('html-pdf-chrome');
111+
112+
const html = '<p>Hello, world!</p>';
113+
const options = {
114+
port: 9222, // port Chrome is listening on
115+
screenshotOptions: {
116+
format: 'png', // png, jpeg, or webp. Optional, defaults to png.
117+
// quality: 100, // Optional, quality percent (jpeg only)
118+
119+
// optional, defaults to entire window
120+
clip: {
121+
x: 0,
122+
y: 0,
123+
width: 100,
124+
height: 200,
125+
scale: 1,
126+
},
127+
},
128+
// Optional. Options here: https://chromedevtools.github.io/devtools-protocol/tot/Emulation/#method-setDeviceMetricsOverride
129+
deviceMetrics: {
130+
width: 1000,
131+
height: 1000,
132+
deviceScaleFactor: 0,
133+
mobile: false,
134+
},
135+
};
136+
137+
htmlPdf.create(html, options).then((pdf) => pdf.toFile('test.png'));
138+
```
139+
104140
### Using an External Site
105141

106142
```js

0 commit comments

Comments
 (0)