Skip to content
Snippets Groups Projects
Commit 5bff81ab authored by bbaer's avatar bbaer
Browse files

added defaults for screenshot

parent a2b02a8f
No related branches found
No related tags found
No related merge requests found
......@@ -20,6 +20,11 @@ const arguments = argv(hideBin(process.argv))
if (argv.verbose) console.info(`generating pdf from: "${argv.source}" to: ${argv.output}`)
/* program to run */
})
.option('screenshot', {
alias: 's',
type: 'boolean',
description: 'Take a screenshot instead (default output changes to: output.png)'
})
.option('cookies', {
alias: 'c',
type: 'array',
......@@ -45,7 +50,7 @@ const arguments = argv(hideBin(process.argv))
description: 'Print in landscape'
})
.option('paper-size', {
alias: "s",
alias: "p",
description: 'Set papersize to: A0 - A6, Letter, Legal, etc.',
default: "A4",
type: "string"
......@@ -108,6 +113,12 @@ const arguments = argv(hideBin(process.argv))
default: '1280x720',
description: 'Viewport of the opened browser window'
})
.option('clip-to-viewport', {
alias: 'C',
type: 'boolean',
default: 'true',
description: 'Screenshot only shows what would be visible by the viewport size'
})
.epilog('qfqPDF 2021, detailed information and sources: https://git.math.uzh.ch/bbaer/qfqpdf')
.argv
......@@ -115,4 +126,5 @@ const arguments = argv(hideBin(process.argv))
const options = new PuppeteerOptions(arguments);
const puppeteerWrap = new PuppeteerWrapper(arguments.source, options, cookies)
puppeteerWrap.printPDF()
\ No newline at end of file
......@@ -12,9 +12,10 @@ class PuppeteerWrapper {
async printPDF() {
const browser = await this.getBrowserInstance()
const page = await browser.newPage()
await page.setViewport({
width: this.options.viewport.width,
height: this.options.viewport.height,
width: parseInt(this.options.viewport.width),
height: parseInt(this.options.viewport.height),
})
if(this.cookies.hasCookies() && !this.cookies.requireReload())
await page.setCookies(this.cookies.getCookies())
......@@ -29,14 +30,21 @@ class PuppeteerWrapper {
}
if(this.cookies.options.verbose)
console.log("Cookies in use:", await page.cookies())
await page.pdf(this.options);
if(this.options.screenshot) {
await page.screenshot(this.options)
} else {
await page.pdf(this.options);
}
await browser.close()
}
async getBrowserInstance() {
const path = Path.join(process.cwd(), '.qfqpdf-chromium')
const fetcher = puppeteer.createBrowserFetcher({path: path})
const { local, revision, executablePath } = fetcher.revisionInfo('884014')
// Latest supported Revision Number is usually in one of the release notes here: https://github.com/puppeteer/puppeteer/releases
const { local, revision, executablePath } = fetcher.revisionInfo('1181205')
if (!local) {
......
......@@ -3,14 +3,20 @@ const { Template } = require('../helper/template')
class PuppeteerOptions {
constructor(argv) {
this.path = argv.output || "output.pdf"
this.type = argv.type || "pdf"
this.screenshot = !!argv.screenshot
let defaultPath = "output.pdf"
let defaultType = "pdf"
if(!!argv.screenshot) defaultPath = "output.png"
if(!!argv.screenshot) defaultType = "png"
this.path = argv.output || defaultPath
this.type = argv.type || defaultType
this.format = argv.pageSize
this.scale = argv.scale || 1
this.pageRange = argv.pageRange;
this.landscape = !!argv.landscape
this.printBackground = !!argv.printBackground
this.displayHeaderFooter = !!argv.printHeaderAndFooter
if(!!argv.screenshot) this.captureBeyoundViewport = !argv.clipToViewport
this.margin = {
top: argv.marginTop || "18mm",
left: argv.marginLeft || "18mm",
......@@ -18,8 +24,8 @@ class PuppeteerOptions {
bottom: argv.marginBottom || "18mm"
}
this.viewport = {
width: argv.viewport.substring(0, viewport.indexOf('x')),
height: argv.viewport.substring(viewport.indexOf('x') + 1)
width: argv.viewport.substring(0, argv.viewport.indexOf('x')),
height: argv.viewport.substring(argv.viewport.indexOf('x') + 1)
}
const template = new Template(argv)
if (template.hasHeader())
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment