|
6 | 6 | [](https://codecov.io/gh/westy92/html-pdf-chrome)
|
7 | 7 | [](https://snyk.io/test/github/westy92/html-pdf-chrome)
|
8 | 8 |
|
9 |
| -HTML to PDF converter via Chrome/Chromium. |
| 9 | +HTML to PDF or image (jpeg, png, webp) converter via Chrome/Chromium. |
10 | 10 |
|
11 | 11 | ## Prerequisites
|
12 | 12 |
|
@@ -101,6 +101,42 @@ htmlPdf.create(html, options).then((pdf) => pdf.toStream());
|
101 | 101 |
|
102 | 102 | View the full documentation in the source code.
|
103 | 103 |
|
| 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 | + |
104 | 140 | ### Using an External Site
|
105 | 141 |
|
106 | 142 | ```js
|
|
0 commit comments