@@ -16,7 +16,8 @@ const {
1616
1717const TEMPLATE_FILE_NAMES = require ( './template-file-names' )
1818const {
19- PDFBufferUnderElectronCreationError
19+ PDFBufferUnderElectronCreationError,
20+ PDFBufferUnderFrameworkCreationError
2021} = require ( '@bitfinex/bfx-report/workers/loc.api/errors' )
2122
2223const { decorateInjectable } = require ( '../../di/utils' )
@@ -59,43 +60,51 @@ class PdfWriter extends MainPdfWriter {
5960 * @override
6061 */
6162 async createPDFBuffer ( args ) {
62- const {
63- template = 'No data' ,
64- format = 'portrait' ,
65- orientation = 'Letter'
66- } = args ?? { }
63+ const _args = {
64+ template : 'No data' ,
65+ format : 'portrait' ,
66+ orientation : 'Letter' ,
67+ timeout : 10 * 60 * 1000 ,
68+ ...args
69+ }
6770
6871 if ( this . isElectronjsEnv ) {
69- return await this . createPDFBufferUnderElectron ( {
70- template,
71- format,
72- orientation
73- } )
72+ return await this . createPDFBufferUnderElectron ( _args )
7473 }
7574
76- const browser = await puppeteer . launch ( {
77- args : [ '--no-sandbox' , '--disable-gpu' ]
78- } )
79- const page = await browser . newPage ( )
80- await page . setContent ( template , {
81- waitUntil : 'domcontentloaded'
82- } )
83- await page . emulateMediaType ( 'print' )
84- const u8ArrayPdf = await page . pdf ( {
85- landscape : format !== 'portrait' ,
86- format : orientation ,
87- margins : {
88- top : 0 ,
89- bottom : 0 ,
90- left : 0 ,
91- right : 0
92- } ,
93- displayHeaderFooter : true ,
94- footerTemplate : this . #getFooterTemplate( args )
95- } )
96- await browser . close ( )
75+ return await this . createPDFBufferUnderFramework ( _args )
76+ }
77+
78+ async createPDFBufferUnderFramework ( args ) {
79+ try {
80+ const browser = await puppeteer . launch ( {
81+ args : [ '--no-sandbox' , '--disable-gpu' ] ,
82+ protocolTimeout : args ?. timeout ,
83+ timeout : 60_000
84+ } )
85+ const page = await browser . newPage ( )
86+ await page . setContent ( args ?. template , {
87+ waitUntil : 'domcontentloaded'
88+ } )
89+ await page . emulateMediaType ( 'print' )
90+ const u8ArrayPdf = await page . pdf ( {
91+ landscape : args ?. format !== 'portrait' ,
92+ format : args ?. orientation ,
93+ margins : {
94+ top : 0 ,
95+ bottom : 0 ,
96+ left : 0 ,
97+ right : 0
98+ } ,
99+ displayHeaderFooter : true ,
100+ footerTemplate : this . #getFooterTemplate( args )
101+ } )
102+ await browser . close ( )
97103
98- return Buffer . from ( u8ArrayPdf )
104+ return Buffer . from ( u8ArrayPdf )
105+ } catch ( err ) {
106+ throw new PDFBufferUnderFrameworkCreationError ( err )
107+ }
99108 }
100109
101110 async createPDFBufferUnderElectron ( args ) {
@@ -117,7 +126,8 @@ class PdfWriter extends MainPdfWriter {
117126 templateFilePath : uniqueFileName ,
118127 format : args ?. format ,
119128 orientation : args ?. orientation ,
120- uid
129+ uid,
130+ timeout : args ?. timeout
121131 }
122132 )
123133
0 commit comments