Skip to content

Commit

Permalink
Merge pull request #128 from tomasc/feature/screenshot_selector
Browse files Browse the repository at this point in the history
Feature/screenshot selector
  • Loading branch information
kimmobrunfeldt authored May 20, 2020
2 parents 51723bf + d177c4a commit fabb29a
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 4 deletions.
19 changes: 19 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "${workspaceFolder}/src/index.js",
"env": {
"NODE_ENV": "development",
"PORT": "9000",
"ALLOW_HTTP": "true",
}
}
]
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ The only required parameter is `url`.
Parameter | Type | Default | Description
----------|------|---------|------------
url | string | - | URL to render as PDF. (required)
output | string | pdf | Specify the output format. Possible values: `pdf` or `screenshot`.
output | string | pdf | Specify the output format. Possible values: `pdf` , `screenshot` or `html`.
emulateScreenMedia | boolean | `true` | Emulates `@media screen` when rendering the PDF.
enableGPU | boolean | `false` | When set, enables chrome GPU. For windows user, this will always return false. See https://developers.google.com/web/updates/2017/04/headless-chrome
ignoreHttpsErrors | boolean | `false` | Ignores possible HTTPS errors when navigating to a page.
Expand Down
12 changes: 9 additions & 3 deletions src/core/render-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,14 @@ async function render(_opts = {}) {
if (clipContainsSomething) {
screenshotOpts.clip = opts.screenshot.clip;
}

data = await page.screenshot(screenshotOpts);
if (_.isNil(opts.screenshot.selector)) {
data = await page.screenshot(screenshotOpts);
} else {
const selElement = await page.$(opts.screenshot.selector);
if (!_.isNull(selElement)) {
data = await selElement.screenshot();
}
}
}
} catch (err) {
logger.error(`Error when rendering page: ${err}`);
Expand All @@ -178,7 +184,7 @@ async function render(_opts = {}) {
await browser.close();
}
}

return data;
}

Expand Down
1 change: 1 addition & 0 deletions src/http/render-http.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ function getOptsFromQuery(query) {
width: query['screenshot.clip.width'],
height: query['screenshot.clip.height'],
},
selector: query['screenshot.selector'],
omitBackground: query['screenshot.omitBackground'],
},
};
Expand Down
2 changes: 2 additions & 0 deletions src/util/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ const sharedQuerySchema = Joi.object({
'screenshot.clip.y': Joi.number(),
'screenshot.clip.width': Joi.number(),
'screenshot.clip.height': Joi.number(),
'screenshot.selector': Joi.string().regex(/(#|\.).*/),
'screenshot.omitBackground': Joi.boolean(),
});

Expand Down Expand Up @@ -124,6 +125,7 @@ const renderBodyObject = Joi.object({
width: Joi.number(),
height: Joi.number(),
},
selector: Joi.string().regex(/(#|\.).*/),
omitBackground: Joi.boolean(),
}),
failEarly: Joi.string(),
Expand Down

0 comments on commit fabb29a

Please sign in to comment.