Skip to content

Commit 6a1ecec

Browse files
Merge pull request #131 from yundi-fu/feature/auto-height
Feature: ✌️added option for full page pdf rendering ✌️
2 parents 5298dcc + d7b9d0f commit 6a1ecec

File tree

5 files changed

+24
-2
lines changed

5 files changed

+24
-2
lines changed

README.md

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

src/core/render-core.js

Lines changed: 19 additions & 1 deletion
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);

src/http/render-http.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ function getOptsFromQuery(query) {
157157
waitUntil: query['goto.waitUntil'],
158158
},
159159
pdf: {
160+
fullPage: query['pdf.fullPage'],
160161
scale: query['pdf.scale'],
161162
displayHeaderFooter: query['pdf.displayHeaderFooter'],
162163
footerTemplate: query['pdf.footerTemplate'],

src/middleware/error-responder.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const http = require('http');
22
const _ = require('lodash');
33

4-
// This reponder is assuming that all <500 errors are safe to be responded
4+
// This responder is assuming that all <500 errors are safe to be responded
55
// with their .message attribute.
66
// DO NOT write sensitive data into error messages.
77
function createErrorResponder(_opts) {

src/util/validation.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ const sharedQuerySchema = Joi.object({
4646
'pdf.format': Joi.string().min(1).max(2000),
4747
'pdf.width': Joi.string().min(1).max(2000),
4848
'pdf.height': Joi.string().min(1).max(2000),
49+
'pdf.fullPage': Joi.boolean(),
4950
'pdf.footerTemplate': Joi.string(),
5051
'pdf.headerTemplate': Joi.string(),
5152
'pdf.margin.top': Joi.string().min(1).max(2000),
@@ -101,6 +102,7 @@ const renderBodyObject = Joi.object({
101102
format: Joi.string().min(1).max(2000),
102103
width: Joi.string().min(1).max(2000),
103104
height: Joi.string().min(1).max(2000),
105+
fullPage: Joi.boolean(),
104106
footerTemplate: Joi.string(),
105107
headerTemplate: Joi.string(),
106108
margin: Joi.object({

0 commit comments

Comments
 (0)