Skip to content

Commit bba4e7a

Browse files
committed
small improvements
1 parent e63c741 commit bba4e7a

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

main.mjs

+12-10
Original file line numberDiff line numberDiff line change
@@ -19,28 +19,30 @@ const tempDir = 'temp';
1919
const app = express();
2020
const upload = multer({dest: tempDir});
2121

22+
/**
23+
* Converting the PDF file to images
24+
*/
2225
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`);
3126
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`);
3231
const outPath = `${req.file.path}.pdf`;
3332

33+
// converting the office file to PDF using LibreOffice
3434
execSync(
3535
`libreoffice `
3636
+ `--headless `
3737
+ `--convert-to pdf:writer_pdf_Export `
3838
+ `--outdir ${tempDir} `
3939
+ `${req.file.path}`,
40+
{stdio: 'ignore'}
4041
);
4142

43+
// checking if the PDF file exists
4244
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');
4446
}
4547

4648
// sending the images as a response
@@ -52,7 +54,7 @@ app.post('/convert', upload.single('file'), (req, res) => {
5254
});
5355
}
5456
catch (e) {
55-
console.error(e.message);
57+
console.error(`Failed to convert the office file to PDF: ${e.message}`);
5658
res.status(400);
5759
res.send({error: e.message});
5860
unlinkSync(req.file.path);

0 commit comments

Comments
 (0)