Skip to content

Commit 1c1c3e8

Browse files
committed
Added viewportWidth= and viewportHeight=, closes simonw#4
Also documented other options.
1 parent 8434ff9 commit 1c1c3e8

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,11 @@ You can access screenshots for pages using:
1515
https:/.../zeit.co/blog?type=png&key=...
1616

1717
Screenshots can take several seconds to generate so it's a good idea to cache them somewhere.
18+
19+
## Querystring arguments
20+
21+
- `?viewportWidth=` sets the browser viewport width in pixels. This defaults to 800.
22+
- `?viewportHeight=` sets the browser viewport height in pixels. This defaults to 600.
23+
- `?type=` set the output type to `png` or `jpeg`. Default is `png`.
24+
- `?quality=75` set the JPEG output qualit. Ignored for PNG.
25+
- `?fullPage=true` fetch a screenshot of the full page, not just the browser viewport.

chromium.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
const chrome = require('chrome-aws-lambda');
22
const puppeteer = require('puppeteer-core');
33

4-
async function getScreenshot(url, type, quality, fullPage) {
4+
async function getScreenshot(url, type, quality, fullPage, viewportWidth, viewportHeight) {
55
const browser = await puppeteer.launch({
66
args: chrome.args,
77
executablePath: await chrome.executablePath,
88
headless: chrome.headless,
9+
defaultViewport: {
10+
width: viewportWidth,
11+
height: viewportHeight
12+
}
913
});
1014

1115
const page = await browser.newPage();

screenshot.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,14 @@ module.exports = async function (req, res) {
2525
const { type = 'png', quality, fullPage } = query;
2626
const url = getUrlFromPath(pathname);
2727
const qual = getInt(quality);
28+
let viewportWidth = parseInt(req.query.viewportWidth || '800');
29+
let viewportHeight = parseInt(req.query.viewportHeight || '600');
2830
if (!isValidUrl(url)) {
2931
res.statusCode = 400;
3032
res.setHeader('Content-Type', 'text/html');
3133
res.end(`<h1>Bad Request</h1><p>The url <em>${url}</em> is not valid.</p>`);
3234
} else {
33-
const file = await getScreenshot(url, type, qual, fullPage);
35+
const file = await getScreenshot(url, type, qual, fullPage, viewportWidth, viewportHeight);
3436
res.statusCode = 200;
3537
res.setHeader('Content-Type', `image/${type}`);
3638
res.end(file);

0 commit comments

Comments
 (0)