diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 00000000..01ce85a9 --- /dev/null +++ b/.vscode/launch.json @@ -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", + } + } + ] +} \ No newline at end of file diff --git a/README.md b/README.md index 6e10502a..630817df 100644 --- a/README.md +++ b/README.md @@ -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. ignoreHttpsErrors | boolean | `false` | Ignores possible HTTPS errors when navigating to a page. scrollPage | boolean | `false` | Scroll page down before rendering to trigger lazy loading elements. diff --git a/src/core/render-core.js b/src/core/render-core.js index 1a6942ba..67315309 100644 --- a/src/core/render-core.js +++ b/src/core/render-core.js @@ -162,8 +162,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}`); @@ -175,7 +181,7 @@ async function render(_opts = {}) { await browser.close(); } } - + return data; } diff --git a/src/http/render-http.js b/src/http/render-http.js index 383c9c1a..aacef6c1 100644 --- a/src/http/render-http.js +++ b/src/http/render-http.js @@ -185,6 +185,7 @@ function getOptsFromQuery(query) { width: query['screenshot.clip.width'], height: query['screenshot.clip.height'], }, + selector: query['screenshot.selector'], omitBackground: query['screenshot.omitBackground'], }, }; diff --git a/src/util/validation.js b/src/util/validation.js index 6a1adadf..542793a7 100644 --- a/src/util/validation.js +++ b/src/util/validation.js @@ -61,6 +61,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(), }); @@ -123,6 +124,7 @@ const renderBodyObject = Joi.object({ width: Joi.number(), height: Joi.number(), }, + selector: Joi.string().regex(/(#|\.).*/), omitBackground: Joi.boolean(), }), failEarly: Joi.string(),