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

pdfReport/generate: add --debug flag #4422

Merged
merged 2 commits into from
Mar 5, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/server/service/pdfReport/generate.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { createI18nPromise } from 'i18n/i18nFactory'
import puppeteer, { PDFOptions } from 'puppeteer'
import puppeteer, { PDFOptions, PuppeteerLaunchOptions } from 'puppeteer'
import { Promises } from 'utils/promises'

import { Areas, CountryIso } from 'meta/area'
import { AssessmentName, Assessments, CycleName } from 'meta/assessment'
import { Lang } from 'meta/lang'

import { ProcessEnv } from 'server/utils'
import { Logger } from 'server/utils/logger'

type Props = {
appUri?: string
Expand Down Expand Up @@ -41,10 +42,14 @@ const pdfOptions: PDFOptions = {
scale: 0.7,
}

// Pass --debug to open the browser window and wait for Enter before closing
const debug = process.argv.includes('--debug')
const browserOptions: PuppeteerLaunchOptions = debug ? { headless: false, defaultViewport: null } : { headless: true }

export const generate = async (props: Props): Promise<Buffer> => {
const { appUri, assessmentName, cookies, countryIso, cycleName, lang, onlyTables } = { ...defaultProps, ...props }
const { t } = await createI18nPromise(lang)
const browser = await puppeteer.launch({ headless: true })
const browser = await puppeteer.launch(browserOptions)
const page = await browser.newPage()

const headerText = t('print.header', {
Expand Down Expand Up @@ -74,6 +79,13 @@ export const generate = async (props: Props): Promise<Buffer> => {

await page.goto(url, { waitUntil: 'networkidle0', timeout: 0 })

if (process.stdin.isTTY && debug) {
Logger.debug('Browser window opened. Press Enter to generate PDF and close the browser...')
await new Promise<void>((resolve) => {
process.stdin.once('data', () => resolve())
})
}

const pdf = await page.pdf(pdfOptions)
await browser.close()

Expand Down