@@ -4,22 +4,23 @@ const hb = require("handlebars");
4
4
const fs = require ( "fs" ) ;
5
5
const inlineCss = require ( "inline-css" ) ;
6
6
module . exports ;
7
+
7
8
async function generatePdf ( file , options , callback , font ) {
8
- // we are using headless mode
9
- let args = [ "--no-sandbox" , "--disable-setuid-sandbox" ] ;
10
- if ( options . args ) {
11
- args = options . args ;
12
- delete options . args ;
13
- }
9
+ // we are using headless mode
10
+ let args = [ "--no-sandbox" , "--disable-setuid-sandbox" ] ;
11
+ if ( options . args ) {
12
+ args = options . args ;
13
+ delete options . args ;
14
+ }
14
15
15
- const browser = await puppeteer . launch ( {
16
- args : args ,
17
- } ) ;
18
- const page = await browser . newPage ( ) ;
19
- let embeddedFontStyle = "" ;
20
- if ( file . content ) {
21
- if ( font ) {
22
- embeddedFontStyle = `
16
+ const browser = await puppeteer . launch ( {
17
+ args : args ,
18
+ } ) ;
19
+ const page = await browser . newPage ( ) ;
20
+ let embeddedFontStyle = "" ;
21
+ if ( file . content ) {
22
+ if ( font ) {
23
+ embeddedFontStyle = `
23
24
<style>
24
25
@font-face {
25
26
font-family: ${ font . name } ;
@@ -29,76 +30,76 @@ async function generatePdf(file, options, callback, font) {
29
30
}
30
31
</style>
31
32
` ;
32
- }
33
- data = await inlineCss ( file . content , { url : "/" } ) ;
34
- data = embeddedFontStyle + data ;
33
+ }
34
+ data = await inlineCss ( file . content , { url : "/" } ) ;
35
+ data = embeddedFontStyle + data ;
35
36
36
- console . log ( "Compiling the template with handlebars" ) ;
37
- // we have compile our code with handlebars
38
- const template = hb . compile ( data , { strict : true } ) ;
39
- const result = template ( data ) ;
40
- const html = result ;
37
+ console . log ( "Compiling the template with handlebars" ) ;
38
+ // we have compile our code with handlebars
39
+ const template = hb . compile ( data , { strict : true } ) ;
40
+ const result = template ( data ) ;
41
+ const html = result ;
41
42
42
- // We set the page content as the generated html by handlebars
43
- await page . setContent ( html , {
44
- waitUntil : "networkidle0" , // wait for page to load completely
45
- } ) ;
46
- } else {
47
- await page . goto ( file . url , {
48
- waitUntil : [ "load" , "networkidle0" ] , // wait for page to load completely
49
- } ) ;
50
- }
43
+ // We set the page content as the generated html by handlebars
44
+ await page . setContent ( html , {
45
+ waitUntil : "networkidle0" , // wait for page to load completely
46
+ } ) ;
47
+ } else {
48
+ await page . goto ( file . url , {
49
+ waitUntil : [ "load" , "networkidle0" ] , // wait for page to load completely
50
+ } ) ;
51
+ }
51
52
52
- return Promise . props ( page . pdf ( options ) )
53
- . then ( async function ( data ) {
54
- await browser . close ( ) ;
53
+ return Promise . props ( page . pdf ( options ) )
54
+ . then ( async function ( data ) {
55
+ await browser . close ( ) ;
55
56
56
- return Buffer . from ( Object . values ( data ) ) ;
57
- } )
58
- . asCallback ( callback ) ;
57
+ return Buffer . from ( Object . values ( data ) ) ;
58
+ } )
59
+ . asCallback ( callback ) ;
59
60
}
60
61
61
62
async function generatePdfs ( files , options , callback ) {
62
- // we are using headless mode
63
- let args = [ "--no-sandbox" , "--disable-setuid-sandbox" ] ;
64
- if ( options . args ) {
65
- args = options . args ;
66
- delete options . args ;
67
- }
68
- const browser = await puppeteer . launch ( {
69
- args : args ,
70
- } ) ;
71
- let pdfs = [ ] ;
72
- const page = await browser . newPage ( ) ;
73
- for ( let file of files ) {
74
- if ( file . content ) {
75
- data = await inlineCss ( file . content , { url : "/" } ) ;
76
- console . log ( "Compiling the template with handlebars" ) ;
77
- // we have compile our code with handlebars
78
- const template = hb . compile ( data , { strict : true } ) ;
79
- const result = template ( data ) ;
80
- const html = result ;
81
- // We set the page content as the generated html by handlebars
82
- await page . setContent ( html , {
83
- waitUntil : "networkidle0" , // wait for page to load completely
84
- } ) ;
85
- } else {
86
- await page . goto ( file . url , {
87
- waitUntil : "networkidle0" , // wait for page to load completely
88
- } ) ;
63
+ // we are using headless mode
64
+ let args = [ "--no-sandbox" , "--disable-setuid-sandbox" ] ;
65
+ if ( options . args ) {
66
+ args = options . args ;
67
+ delete options . args ;
68
+ }
69
+ const browser = await puppeteer . launch ( {
70
+ args : args ,
71
+ } ) ;
72
+ let pdfs = [ ] ;
73
+ const page = await browser . newPage ( ) ;
74
+ for ( let file of files ) {
75
+ if ( file . content ) {
76
+ data = await inlineCss ( file . content , { url : "/" } ) ;
77
+ console . log ( "Compiling the template with handlebars" ) ;
78
+ // we have compile our code with handlebars
79
+ const template = hb . compile ( data , { strict : true } ) ;
80
+ const result = template ( data ) ;
81
+ const html = result ;
82
+ // We set the page content as the generated html by handlebars
83
+ await page . setContent ( html , {
84
+ waitUntil : "networkidle0" , // wait for page to load completely
85
+ } ) ;
86
+ } else {
87
+ await page . goto ( file . url , {
88
+ waitUntil : "networkidle0" , // wait for page to load completely
89
+ } ) ;
90
+ }
91
+ let pdfObj = JSON . parse ( JSON . stringify ( file ) ) ;
92
+ delete pdfObj [ "content" ] ;
93
+ pdfObj [ "buffer" ] = Buffer . from ( Object . values ( await page . pdf ( options ) ) ) ;
94
+ pdfs . push ( pdfObj ) ;
89
95
}
90
- let pdfObj = JSON . parse ( JSON . stringify ( file ) ) ;
91
- delete pdfObj [ "content" ] ;
92
- pdfObj [ "buffer" ] = Buffer . from ( Object . values ( await page . pdf ( options ) ) ) ;
93
- pdfs . push ( pdfObj ) ;
94
- }
95
96
96
- return Promise . resolve ( pdfs )
97
- . then ( async function ( data ) {
98
- await browser . close ( ) ;
99
- return data ;
100
- } )
101
- . asCallback ( callback ) ;
97
+ return Promise . resolve ( pdfs )
98
+ . then ( async function ( data ) {
99
+ await browser . close ( ) ;
100
+ return data ;
101
+ } )
102
+ . asCallback ( callback ) ;
102
103
}
103
104
104
105
module . exports . generatePdf = generatePdf ;
0 commit comments