Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pdf options magicformat, emulatemedia, scale and injectjs #13

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from

Conversation

wingsuitist
Copy link

  • magicformat creates a pdf page format adapted to the website size
  • emulatemedia and scale expose the matching puppeteer options
  • injectjs injects a javascript from a file path before generating the pdf

if (argv.magicformat) {
height = (await page.evaluate(
'Math.max(document.body.scrollHeight, document.body.offsetHeight, document.documentElement.clientHeight, document.documentElement.scrollHeight, document.documentElement.offsetHeight)'));
width = '1366';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how was this value chosen? should we make it an option?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on the most common browser resolution website designs focus on.
As there is no width option, we'd have to add a separate with.

index.js Outdated
string: true,
default: ''
},
'magicformat': {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this option is exclusive with format, could we make the syntax for this be --format=auto so that it's clear this replaces any value for format?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, adapted it. How can we add a comment/info that there is a --format=auto?

index.js Outdated
string: true,
default: ''
},
'injectjs': {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rename to inject-js to be consistent with other multi-term options (will materialize as argv.injectJs then)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

index.js Outdated
@@ -31,6 +32,22 @@ const argv = require('yargs')
desc: 'Print an HTML file or URL to PDF',
builder: {
...commonOptions,
'emulatemedia': {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rename to emulate-media to be consistent with other multi-term options (will materialize as argv.emulateMedia then)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@@ -31,6 +32,22 @@ const argv = require('yargs')
desc: 'Print an HTML file or URL to PDF',
builder: {
...commonOptions,
'emulatemedia': {
string: true,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

configure this as a boolean

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not a boolean, it's "screen" "print" etc.

index.js Outdated
height = (await page.evaluate(
'Math.max(document.body.scrollHeight, document.body.offsetHeight, document.documentElement.clientHeight, document.documentElement.scrollHeight, document.documentElement.offsetHeight)'));
width = '1366';
argv.format = undefined;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's treat argv as immutable, either vary what gets passed to page.pdf(...) with ternary operators or pull out a mutable variable to be built up procedurally like you did with width and height

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

index.js Outdated
await page.evaluate(`(async () => {${fs.readFileSync(argv.injectjs)}})()`);
}

if (argv.emulatemedia) await page.emulateMedia(argv.emulatemedia);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

linter conflict here, please break out the inlining

Suggested change
if (argv.emulatemedia) await page.emulateMedia(argv.emulatemedia);
if (argv.emulatemedia) {
await page.emulateMedia(argv.emulatemedia);
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

const buffer = await page.pdf({
path: argv.output || null,
format: argv.format,
width, height,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we default all these to null here like with path to ensure that omitted options on our end don't prevent page.pdf() from applying its own default behavior?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure. They should be undefined if not set.

@themightychris
Copy link
Member

@wingsuitist these are awesome additions, thanks for contributing! Forgive my slow follow-up, would you mine making some of the tweaks I requested?

@wingsuitist
Copy link
Author

Thx! I'll look at it asap.

@wingsuitist
Copy link
Author

wingsuitist commented Feb 23, 2020

Here are the fixes to my best understanding. Thank you for the outstanding feedback.
0ccc85a

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants