|
|
@ -9,7 +9,10 @@ Logger.config.defaultLevel = LogLevel.Debug; |
|
|
|
const logger = Logger.getLogger('pdf'); |
|
|
|
const logger = Logger.getLogger('pdf'); |
|
|
|
|
|
|
|
|
|
|
|
// Configuration
|
|
|
|
// Configuration
|
|
|
|
const output = 'static/schedule/schedule.pdf'; |
|
|
|
const outputs = { |
|
|
|
|
|
|
|
'schedule/index.html': 'public/schedule/schedule-en.pdf', |
|
|
|
|
|
|
|
'fr/schedule/index.html': 'public/schedule/schedule-fr.pdf', |
|
|
|
|
|
|
|
}; |
|
|
|
const serverConf = { |
|
|
|
const serverConf = { |
|
|
|
port: 8765, |
|
|
|
port: 8765, |
|
|
|
options: { |
|
|
|
options: { |
|
|
@ -70,13 +73,28 @@ async function cleanupBeforePrint(page) { |
|
|
|
|
|
|
|
|
|
|
|
await page.$$eval(toHide.join(','), elts => |
|
|
|
await page.$$eval(toHide.join(','), elts => |
|
|
|
elts.forEach(elt => |
|
|
|
elts.forEach(elt => |
|
|
|
elt.parentNode.removeChild(elt))); |
|
|
|
elt && elt.parentNode ? elt.parentNode.removeChild(elt) : elt)); |
|
|
|
|
|
|
|
|
|
|
|
await page.addStyleTag({ |
|
|
|
await page.addStyleTag({ |
|
|
|
content: '@page { size: auto; }', |
|
|
|
content: '@page { size: auto; }', |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async function renderPdf(page, file, output) { |
|
|
|
|
|
|
|
const url = `http://localhost:${serverConf.port}/${file}`; |
|
|
|
|
|
|
|
logger.info("go to", url); |
|
|
|
|
|
|
|
const pageResponse = await page.goto(url, {waitUntil: 'networkidle2'}); |
|
|
|
|
|
|
|
logger.debug("done", pageResponse.statusText()); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
await cleanupBeforePrint(page); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
logger.info('export pdf', output); |
|
|
|
|
|
|
|
const format = 'A3'; |
|
|
|
|
|
|
|
const scale = .4; |
|
|
|
|
|
|
|
const printBackground = true; |
|
|
|
|
|
|
|
return page.pdf({path: output, format, scale, printBackground}); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
(async () => { |
|
|
|
(async () => { |
|
|
|
const server = await startServer(serverConf); |
|
|
|
const server = await startServer(serverConf); |
|
|
|
|
|
|
|
|
|
|
@ -87,18 +105,10 @@ async function cleanupBeforePrint(page) { |
|
|
|
const page = await browser.newPage(); |
|
|
|
const page = await browser.newPage(); |
|
|
|
logger.debug("opened new page"); |
|
|
|
logger.debug("opened new page"); |
|
|
|
|
|
|
|
|
|
|
|
const file = 'fr/schedule/index.html'; |
|
|
|
for (let [file, output] of Object.entries(outputs)) { |
|
|
|
const url = `http://localhost:${serverConf.port}/${file}`; |
|
|
|
await renderPdf(page, file, output); |
|
|
|
logger.info("go to", url); |
|
|
|
} |
|
|
|
const pageResponse = await page.goto(url, {waitUntil: 'networkidle2'}); |
|
|
|
|
|
|
|
logger.debug("done", pageResponse.statusText()); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
await cleanupBeforePrint(page); |
|
|
|
|
|
|
|
logger.info('export pdf', output); |
|
|
|
|
|
|
|
const format = 'A3'; |
|
|
|
|
|
|
|
const scale = .4; |
|
|
|
|
|
|
|
const printBackground = true; |
|
|
|
|
|
|
|
await page.pdf({path:output, format, scale, printBackground}); |
|
|
|
|
|
|
|
logger.debug("pdf done"); |
|
|
|
logger.debug("pdf done"); |
|
|
|
|
|
|
|
|
|
|
|
} catch (e) { |
|
|
|
} catch (e) { |
|
|
|