Skip to content

Commit

Permalink
Merge pull request alvarcarto#131 from yundi-fu/feature/auto-height
Browse files Browse the repository at this point in the history
Feature: ✌️added option for full page pdf rendering ✌️
  • Loading branch information
kimmobrunfeldt authored Jul 15, 2020
2 parents 5298dcc + d7b9d0f commit 6a1ecec
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ pdf.pageRanges | string | - | Paper ranges to print, e.g., '1-5, 8, 11-13'. Defa
pdf.format | string | `A4` | Paper format. If set, takes priority over width or height options.
pdf.width | string | - | Paper width, accepts values labeled with units.
pdf.height | string | - | Paper height, accepts values labeled with units.
pdf.fullPage | boolean | - | Create PDF in a single page
pdf.margin.top | string | - | Top margin, accepts values labeled with units.
pdf.margin.right | string | - | Right margin, accepts values labeled with units.
pdf.margin.bottom | string | - | Bottom margin, accepts values labeled with units.
Expand Down
20 changes: 19 additions & 1 deletion src/core/render-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,20 @@ async function createBrowser(opts) {
return puppeteer.launch(browserOpts);
}

async function getFullPageHeight(page) {
const height = await page.evaluate(() => {
const { body, documentElement } = document;
return Math.max(
body.scrollHeight,
body.offsetHeight,
documentElement.clientHeight,
documentElement.scrollHeight,
documentElement.offsetHeight
);
});
return height;
}

async function render(_opts = {}) {
const opts = _.merge({
cookies: [],
Expand All @@ -50,7 +64,7 @@ async function render(_opts = {}) {
failEarly: false,
}, _opts);

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

if (opts.output === 'pdf') {
if (opts.pdf.fullPage) {
const height = await getFullPageHeight(page);
opts.pdf.height = height;
}
data = await page.pdf(opts.pdf);
} else if (opts.output === 'html') {
data = await page.evaluate(() => document.body.innerHTML);
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 @@ -157,6 +157,7 @@ function getOptsFromQuery(query) {
waitUntil: query['goto.waitUntil'],
},
pdf: {
fullPage: query['pdf.fullPage'],
scale: query['pdf.scale'],
displayHeaderFooter: query['pdf.displayHeaderFooter'],
footerTemplate: query['pdf.footerTemplate'],
Expand Down
2 changes: 1 addition & 1 deletion src/middleware/error-responder.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const http = require('http');
const _ = require('lodash');

// This reponder is assuming that all <500 errors are safe to be responded
// This responder is assuming that all <500 errors are safe to be responded
// with their .message attribute.
// DO NOT write sensitive data into error messages.
function createErrorResponder(_opts) {
Expand Down
2 changes: 2 additions & 0 deletions src/util/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const sharedQuerySchema = Joi.object({
'pdf.format': Joi.string().min(1).max(2000),
'pdf.width': Joi.string().min(1).max(2000),
'pdf.height': Joi.string().min(1).max(2000),
'pdf.fullPage': Joi.boolean(),
'pdf.footerTemplate': Joi.string(),
'pdf.headerTemplate': Joi.string(),
'pdf.margin.top': Joi.string().min(1).max(2000),
Expand Down Expand Up @@ -101,6 +102,7 @@ const renderBodyObject = Joi.object({
format: Joi.string().min(1).max(2000),
width: Joi.string().min(1).max(2000),
height: Joi.string().min(1).max(2000),
fullPage: Joi.boolean(),
footerTemplate: Joi.string(),
headerTemplate: Joi.string(),
margin: Joi.object({
Expand Down

0 comments on commit 6a1ecec

Please sign in to comment.