@@ -19,28 +19,30 @@ const tempDir = 'temp';
19
19
const app = express ( ) ;
20
20
const upload = multer ( { dest : tempDir } ) ;
21
21
22
+ /**
23
+ * Converting the PDF file to images
24
+ */
22
25
app . post ( '/convert' , upload . single ( 'file' ) , ( req , res ) => {
23
- if ( ! req . file . filename ) {
24
- res . status ( 400 ) ;
25
- res . send ( { error : 'No file uploaded' } ) ;
26
- return ;
27
- }
28
-
29
- // converting the PDF file to images
30
- console . log ( `Converting Office file ${ req . file . originalname } to PDF` ) ;
31
26
try {
27
+ if ( ! req . file . filename ) {
28
+ throw new Error ( 'No file uploaded' ) ;
29
+ }
30
+ console . log ( `Converting Office file "${ req . file . originalname } " to PDF` ) ;
32
31
const outPath = `${ req . file . path } .pdf` ;
33
32
33
+ // converting the office file to PDF using LibreOffice
34
34
execSync (
35
35
`libreoffice `
36
36
+ `--headless `
37
37
+ `--convert-to pdf:writer_pdf_Export `
38
38
+ `--outdir ${ tempDir } `
39
39
+ `${ req . file . path } ` ,
40
+ { stdio : 'ignore' }
40
41
) ;
41
42
43
+ // checking if the PDF file exists
42
44
if ( ! existsSync ( outPath ) ) {
43
- throw 'Failed to convert the office file to PDF' ;
45
+ throw new Error ( 'Failed to convert the office file to PDF' ) ;
44
46
}
45
47
46
48
// sending the images as a response
@@ -52,7 +54,7 @@ app.post('/convert', upload.single('file'), (req, res) => {
52
54
} ) ;
53
55
}
54
56
catch ( e ) {
55
- console . error ( e . message ) ;
57
+ console . error ( `Failed to convert the office file to PDF: ${ e . message } ` ) ;
56
58
res . status ( 400 ) ;
57
59
res . send ( { error : e . message } ) ;
58
60
unlinkSync ( req . file . path ) ;
0 commit comments