Reputation: 4162
I found some repos, which do not look as they are still maintained:
I tried the approach with libreoffice
, but the pdf output is so bad, that it is not useable (text on diff. pages etc.).
If possible I would like to avoid starting any background processes and/or saving the file on the server. Best would be solution where I can use buffers. For privacy reasons, I cannot use any external service.
doc buffer -> pdf buffer
How to convert docs to pdf in nodejs?
Upvotes: 22
Views: 47334
Reputation: 59
One more answer that requires libreOffice (but there is a catch !):
I have created an npm package that allows for multiple file conversions including conversion from docx to pdf:
https://www.npmjs.com/package/file-converter-nodejs
import FileProcessor from 'file-converter-nodejs';
const processor = new FileProcessor();
await processor.convertFile({
filePath: '/path/to/input.docx',
from: 'docx',
to: 'pdf',
outdir: '/path/to/output',
});
The difference with other packages is that this package abstracts a lot of what happens in the background, no need to provide filters or infilters for libreOffice. AND it comes with a docker image that already has all needed dependencies installed (https://hub.docker.com/r/azertyha77/nodejs-python-soffice) Which makes it a lot easier not to have to deal with installing the required dependencies.
If you would like to give it a try and you aren't familiar with Docker, you can take a look at this simple example project: https://gitlab.com/bouhelalhamza/file-converter/-/tree/master/example
Upvotes: -1
Reputation: 1
const { spawn } = require('child_process');
const soffice = spawn('soffice', ['--convert-to', 'pdf', inputFilePath, '--headless']);
Upvotes: -1
Reputation: 1065
For those who might stumble on this question nowadays:
There is cool tool called Gotenberg — Docker-powered stateless API for converting HTML, Markdown and Office documents to PDF. It supports converting DOCs via unoconv.
And I am happen to be an author of JS/TS client for Gotenberg — gotenberg-js-client
I welcome you to use it :)
UPD:
Gotenberg has new website now — https://gotenberg.dev
Upvotes: 10
Reputation: 444
Posting a slightly modified version for excel, based upon the answer provided by @shubham singh. I tried it and it worked perfectly.
const fs = require('fs').promises;
const path = require('path');
const { promisify } = require('bluebird');
const libre = require('libreoffice-convert');
const libreConvert = promisify(libre.convert);
// get current working directory
let workDir = path.dirname(process.mainModule.filename)
// read excel file
let data = await fs.readFile(
`${workDir}/my_excel.xlsx`
);
// create pdf file from excel
let pdfFile = await libreConvert(data, '.pdf', undefined);
// write new pdf file to directory
await fs.writeFile(
`${workDir}/my_pdf.pdf`,
pdfFile
);
Upvotes: 0
Reputation: 559
While I was creating an application I need to convert the doc or docx file uploaded by a user into a pdf file for further analysis. I used npm package libreoffice-convert for this purpose. libreoffice-convert requires libreoffice to be installed on your Linux machine. Here is a sample code that I have used. This code is written in javascript for nodejs based application.
const libre = require('libreoffice-convert');
const path = require('path');
const fs = require('fs').promises;
let lib_convert = promisify(libre.convert)
async function convert(name="myresume.docx") {
try {
let arr = name.split('.')
const enterPath = path.join(__dirname, `/public/Resume/${name}`);
const outputPath = path.join(__dirname, `/public/Resume/${arr[0]}.pdf`);
// Read file
let data = await fs.readFile(enterPath)
let done = await lib_convert(data, '.pdf', undefined)
await fs.writeFile(outputPath, done)
return { success: true, fileName: arr[0] };
} catch (err) {
console.log(err)
return { success: false }
}
}
You will get a very good quality of pdf.
Upvotes: 3
Reputation: 119
Docx to pdf A library that converts docx file to pdf.
Installation:
npm install docx-pdf --save
Usage
var docxConverter = require('docx-pdf');
docxConverter('./input.docx','./output.pdf',function(err,result){
if(err){
console.log(err);
}
console.log('result'+result);
});
its basically docxConverter(inputPath,outPath,function(err,result){
if(err){
console.log(err);
}
console.log('result'+result);
});
Output should be output.pdf which will be produced on the output path your provided
Upvotes: 0
Reputation: 1035
To convert a document into PDF we can use Universal Office Converter (unoconv) command line utility.
It can be installed on your OS by any package manager e.g. To install it on ubuntu using apt-get
sudo apt-get install unoconv
As per documentation of unoconv
If you installed unoconv by hand, make sure you have the required LibreOffice or OpenOffice packages installed
Following example demonstrate how to invoke unoconv utility
unoconv -f pdf sample_document.py
It generates PDF document that contains content of sample_document.py
If you want to use a nodeJS program then you can invoke the command through child process
Find code below that demonstrates how to use child process for using the unoconv for creating PDF
const util = require('util');
const exec = util.promisify(require('child_process').exec);
async function createPDFExample() {
const { stdout, stderr } = await exec('unoconv -f pdf sample.js');
console.log('stdout:', stdout);
console.log('stderr:', stderr);
}
createPDFExample();
Upvotes: 0