1- const config = require ( ' ./config.js' ) ;
1+ const config = require ( " ./config.js" ) ;
22
33const alternateFormats = config . respecConfig . alternateFormats ;
44if ( alternateFormats === undefined ) {
5- console . warn ( "\'alternateFormats\' not found." ) ;
6- }
7- else {
8- const found = alternateFormats . find ( element => element . label === "pdf" ) ;
9- if ( found === undefined ) {
10- console . warn ( "PDF not selected as alternate format." ) ;
11- }
12- else if ( found . uri === undefined ) {
13- console . warn ( "PDF file name (\'uri\') missing." ) ;
14- }
15- else {
16- const name = found . uri ;
17- console . log ( "Printing PDF with name: " + name ) ;
5+ console . warn ( "'alternateFormats' not found." ) ;
6+ } else {
7+ const found = alternateFormats . find ( ( element ) => element . label === "pdf" ) ;
8+ if ( found === undefined ) {
9+ console . warn ( "PDF not selected as alternate format." ) ;
10+ } else if ( found . uri === undefined ) {
11+ console . warn ( "PDF file name ('uri') missing." ) ;
12+ } else {
13+ const name = found . uri ;
14+ console . log ( "Printing PDF with name: " + name ) ;
1815
19- // The following is based on code from
20- // https://www.bannerbear.com/blog/how-to-convert-html-into-pdf-with-node-js-and-puppeteer/
21- const puppeteer = require ( 'puppeteer' ) ;
16+ const puppeteer = require ( "puppeteer" ) ;
2217
23- ( async ( ) => {
24- const browser = await puppeteer . launch ( ) ;
25- const page = await browser . newPage ( ) ;
26- const website_url = 'http://localhost:8080/snapshot.html' ;
27- await page . goto ( website_url , { waitUntil : 'networkidle0' } ) ;
28- await page . emulateMediaType ( 'print' ) ;
29- await page . addStyleTag ( { content : '.sidelabel {position: absolute}' } )
30- const pdf = await page . pdf ( {
31- path : name ,
32- margin : { top : '100px' , right : '50px' , bottom : '100px' , left : '50px' } ,
33- printBackground : true ,
34- format : 'A4' ,
35- } ) ;
18+ ( async ( ) => {
19+ const browser = await puppeteer . launch ( {
20+ args : [ "--no-sandbox" , "--disable-setuid-sandbox" ] ,
21+ } ) ;
22+ const page = await browser . newPage ( ) ;
23+ const website_url = "http://localhost:8081/snapshot.html" ;
24+ await page . goto ( website_url , { waitUntil : "networkidle0" } ) ;
25+ await page . emulateMediaType ( "print" ) ;
26+ await page . addStyleTag ( { content : ".sidelabel {position: absolute}" } ) ;
27+ const pdf = await page . pdf ( {
28+ path : name ,
29+ margin : { top : "100px" , right : "50px" , bottom : "100px" , left : "50px" } ,
30+ printBackground : true ,
31+ format : "A4" ,
32+ } ) ;
3633
37- await browser . close ( ) ;
38- } ) ( ) ;
39- }
34+ await browser . close ( ) ;
35+ } ) ( ) ;
36+ }
4037}
0 commit comments