How to Convert word document to pdf in Nodejs| Javascript example
In this post, You will learn how to convert Docx files to pdf documents in JavaScript and nodejs.
Docx/doc are document file formats from Microsoft, that contains images, text, tables, and styles PDF files are from Adobe company, which is a separate format for representing the content of images, texts, and styles
There are a lot of online tools to do the conversion from doc to pdf. Sometimes, As a programmer, you need to have a conversion of different formats in the JavaScript/NodeJS applications.
JavaScript/NodeJS offers multiple ways to convert using npm packages
- docx-to-pdf
- libreoffice-convert
You can also check other posts on npm command deprecate option is deprecated
How to Convert word document to pdf in Nodejs application
First, Create a Nodejs application from scratch.
Let’s create a nodejs application from scratch using the npm init -y command
in a new folder
B:\blog\jswork\nodework\doctopdf>npm init -y
Wrote to B:\blog\jswork\nodework\doctopdf\package.json:
{
"name": "doctopdf",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
This creates a package.json as follows
{
"name": "doctopdf",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
convert docx to pdf using docx-pdf library
docx-pdf
is a simple library to convert Docx
to a pdf
document.
First, Install Docx-pdf npm library
npm install Docx-pdf --save
This will add a dependency in package.json as follows
{
"dependencies": {
"docx-pdf": "0.0.1"
}
}
In javascript, import docx-pdf using the required for ES5 modules
var converter = require("docx-pdf");
convert objects accepts
- input file which is a word document
- output file is the name of a pdf document
- callback which has
err
for error messages for conversion failed and result in successful conversion - a result is an object containing a filename attribute and the value is the pdf document name and path
var converter = require("docx-pdf");
converter("test.docx", "output.pdf", function (err, result) {
if (err) {
console.log("Converting Doc to PDF failed", err);
}
console.log("Converting Doc to PDF succesfull", result);
});
And same code was written with async and await keywords for the asynchronous process.
convert docx to pdf with async/await
This will be useful for bigger files of sizes
The declared function which accepts input and output filename It returns the promise object with reject
for failed conversions and resolve
for successful conversions. And, docxConverter logic calls inside the async
keyword with an anonymous function for asynchronous processing.
async function ConvertDocToPdf(inputfile, outputfile) {
return new Promise((resolve, reject) =>{
const inputPath = path.join(__dirname, "test.docx");
const outputPath = path.join(__dirname, `/test.pdf`);
let docData = await fs.readFile(inputPath)
docxConverter(inputfile, outputfile, (err, result) => {
return err ?
reject(err) :
resolve(result)
})
})
}
You need to call the same function with the await
keyword
await ConvertDocToPdf("test.docx", "test.pdf");
It is a simple library, the only disadvantage is not able to convert formatting styles.
libreoffice-convert npm package
libreoffice is an open-source office package for managing office documents.
libreoffice-convert is an npm package in nodejs that provides manipulation of word documents.
First, install libreoffice-convert npm package using the npm install command
npm install libreoffice-convert --save
Example code to convert docx to pdf using the libreoffice-convert package:
const libre = require("libreoffice-convert");
const path = require("path");
const fs = require("fs");
async function ConvertDocToPdf() {
try {
const inputPath = path.join(__dirname, "test.docx");
const outputPath = path.join(__dirname, `/test.pdf`);
let docData = await fs.readFile(inputPath);
return new Promise((resolve, reject) => {
libre.convert(docData, ".pdf", undefined, (err, done) => {
if (err) {
reject("Conversion Failed");
}
fs.writeFileSync(outputPath, done);
resolve("Convertion successfull");
});
});
} catch (err) {
console.log("Error in input reading", err);
}
}
a sequence of steps for the above code
Defined function with async keyword for asynchronous processing
import
libreoffice-convert
,fs
, andpath
modules into code
read the input file using readFile method of fs module in NodeJS
libre.convert the docx to pdf file
conversion code is wrapped in a
promise
objectfor conversion failed cases, the reject promise is returned
the promise is resolved for a successful conversion,
Finally written output pdf file using the
writeFileSync
method
Conclusion
To Sum up, Learned how to Convert word to pdf in nodejs in multiple ways.