@@ -115,6 +115,45 @@ app.get('/thumbnail/:filename', (req, res) => {
115115 } ) ;
116116} ) ;
117117
118+ app . get ( '/convert/:filename' , ( req , res ) => {
119+ const pathname = './files/' ;
120+ const filename = req . params . filename ;
121+ let ext = path . parse ( pathname + filename ) . ext ;
122+
123+ if ( ext === '.pdf' ) {
124+ res . statusCode = 500 ;
125+ res . end ( `File is already PDF.` ) ;
126+ }
127+
128+ const main = async ( ) => {
129+ const pdfdoc = await PDFNet . PDFDoc . create ( ) ;
130+ await pdfdoc . initSecurityHandler ( ) ;
131+ const inputFile = pathname + filename ;
132+ await PDFNet . Convert . toPdf ( pdfdoc , inputFile ) ;
133+ pdfdoc . save ( `${ pathname } ${ filename } .pdf` , PDFNet . SDFDoc . SaveOptions . e_linearized ) ;
134+ ext = '.pdf' ;
135+ } ;
136+
137+ PDFNet . runWithCleanup ( main )
138+ . catch ( function ( error ) {
139+ res . statusCode = 500 ;
140+ res . end ( `Error : ${ JSON . stringify ( error ) } .` ) ;
141+ } )
142+ . then ( function ( ) {
143+ PDFNet . shutdown ( ) ;
144+ const newpath = `${ pathname } ${ filename } .pdf` ;
145+ fs . readFile ( newpath , function ( err , data ) {
146+ if ( err ) {
147+ res . statusCode = 500 ;
148+ res . end ( `Error getting the file: ${ err } .` ) ;
149+ } else {
150+ res . setHeader ( 'Content-type' , mimeType [ ext ] || 'text/plain' ) ;
151+ res . end ( data ) ;
152+ }
153+ } ) ;
154+ } ) ;
155+ } ) ;
156+
118157app . get ( '/files/:filename' , ( req , res ) => {
119158 const pathname = `./files/${ req . params . filename } ` ;
120159 fs . readFile ( pathname , function ( err , data ) {
0 commit comments