Skip to content

Commit d7b9d0f

Browse files
committed
Feature: ✌️added option for full page pdf rendering ✌️
1 parent 2bd672d commit d7b9d0f

File tree

4 files changed

+25
-3
lines changed

4 files changed

+25
-3
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ pdf.pageRanges | string | - | Paper ranges to print, e.g., '1-5, 8, 11-13'. Defa
197197
pdf.format | string | `A4` | Paper format. If set, takes priority over width or height options.
198198
pdf.width | string | - | Paper width, accepts values labeled with units.
199199
pdf.height | string | - | Paper height, accepts values labeled with units.
200+
pdf.fullPage | boolean | - | Create PDF in a single page
200201
pdf.margin.top | string | - | Top margin, accepts values labeled with units.
201202
pdf.margin.right | string | - | Right margin, accepts values labeled with units.
202203
pdf.margin.bottom | string | - | Bottom margin, accepts values labeled with units.

src/core/render-core.js

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,20 @@ async function createBrowser(opts) {
2424
return puppeteer.launch(browserOpts);
2525
}
2626

27+
async function getFullPageHeight(page) {
28+
const height = await page.evaluate(() => {
29+
const { body, documentElement } = document;
30+
return Math.max(
31+
body.scrollHeight,
32+
body.offsetHeight,
33+
documentElement.clientHeight,
34+
documentElement.scrollHeight,
35+
documentElement.offsetHeight
36+
);
37+
});
38+
return height;
39+
}
40+
2741
async function render(_opts = {}) {
2842
const opts = _.merge({
2943
cookies: [],
@@ -50,7 +64,7 @@ async function render(_opts = {}) {
5064
failEarly: false,
5165
}, _opts);
5266

53-
if (_.get(_opts, 'pdf.width') && _.get(_opts, 'pdf.height')) {
67+
if ((_.get(_opts, 'pdf.width') && _.get(_opts, 'pdf.height')) || _.get(opts, 'pdf.fullPage')) {
5468
// pdf.format always overrides width and height, so we must delete it
5569
// when user explicitly wants to set width and height
5670
opts.pdf.format = undefined;
@@ -154,6 +168,10 @@ async function render(_opts = {}) {
154168
}
155169

156170
if (opts.output === 'pdf') {
171+
if (opts.pdf.fullPage) {
172+
const height = await getFullPageHeight(page);
173+
opts.pdf.height = height;
174+
}
157175
data = await page.pdf(opts.pdf);
158176
} else if (opts.output === 'html') {
159177
data = await page.evaluate(() => document.body.innerHTML);
@@ -171,7 +189,7 @@ async function render(_opts = {}) {
171189
const selElement = await page.$(opts.screenshot.selector);
172190
if (!_.isNull(selElement)) {
173191
data = await selElement.screenshot();
174-
}
192+
}
175193
}
176194
}
177195
} catch (err) {
@@ -184,7 +202,7 @@ async function render(_opts = {}) {
184202
await browser.close();
185203
}
186204
}
187-
205+
188206
return data;
189207
}
190208

src/http/render-http.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ function getOptsFromQuery(query) {
159159
networkIdleTimeout: query['goto.networkIdleTimeout'],
160160
},
161161
pdf: {
162+
fullPage: query['pdf.fullPage'],
162163
scale: query['pdf.scale'],
163164
displayHeaderFooter: query['pdf.displayHeaderFooter'],
164165
footerTemplate: query['pdf.footerTemplate'],

src/util/validation.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ const sharedQuerySchema = Joi.object({
4848
'pdf.format': Joi.string().min(1).max(2000),
4949
'pdf.width': Joi.string().min(1).max(2000),
5050
'pdf.height': Joi.string().min(1).max(2000),
51+
'pdf.fullPage': Joi.boolean(),
5152
'pdf.footerTemplate': Joi.string(),
5253
'pdf.headerTemplate': Joi.string(),
5354
'pdf.margin.top': Joi.string().min(1).max(2000),
@@ -105,6 +106,7 @@ const renderBodyObject = Joi.object({
105106
format: Joi.string().min(1).max(2000),
106107
width: Joi.string().min(1).max(2000),
107108
height: Joi.string().min(1).max(2000),
109+
fullPage: Joi.boolean(),
108110
footerTemplate: Joi.string(),
109111
headerTemplate: Joi.string(),
110112
margin: Joi.object({

0 commit comments

Comments
 (0)