I'm trying to generate a PDF from a previously generated DOCX file in NodeJS. I'm using NestJS and TypeScript. I have tried many ways, but everything fails:
Do you know some workaround to the problems I got with any of the previously mentioned docx to pdf alternatives? Or maybe some other alternative that I can use (For free, please)?
Thank you in advance!
The best I know about is using Libreoffice. You don't really need a wrapper library for that.
You can just execute libreoffice directly using exec
:
const { exec } = require("child_process");
exec("libreoffice --headless file.xyz", (error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`);
return;
}
if (stderr) {
console.log(`stderr: ${stderr}`);
return;
}
console.log(`stdout: ${stdout}`);
});
You'll need to first write your DOCX to a file somewhere on the disk where Libreoffice would be able to open it, and the resulting PDF would be written on the disk as well - then you can load it into Node with fs.