Nodejs: Convert Doc to PDF

I found several repositories that do not look like they are still supported:

I tried the libreoffice approach , but the pdf output is so bad that it cannot be used (text on different pages, etc.).

If possible, I would like to avoid starting any background processes and / or saving the file to the server. A better solution would be where I can use buffers. For privacy reasons, I cannot use any external services.

doc buffer → pdf buffer

Question:

How to convert documents to pdf in nodejs?

+9
source share
3 answers

To convert a document to PDF, we can use the command line utility Universal Office Converter (unoconv) .

It can be installed on your OS by any package manager, for example, to install it on Ubuntu using apt-get

 sudo apt-get install unoconv 

According to unoconv documentation

If you installed unoconv manually, make sure you have the necessary LibreOffice or OpenOffice packages installed

The following example shows how to call unoconv.

 unoconv -f pdf sample_document.py 

It generates a PDF document that contains the contents of the sample_document.py file

If you want to use the nodeJS program, you can invoke the command through a child process

Find the code below that demonstrates how to use the child process to use unoconv to create a 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(); 
0
source

The belated answer, but now you can try the https://www.npmjs.com/package/@nativedocuments/docx-wasm we just released (January 2019).

It will perform the conversion locally and does not require LibreOffice, unoconv or anything else.

 const fs = require('fs'); const docx = require("@nativedocuments/docx-wasm"); // init docx engine docx.init({ // ND_DEV_ID: "XXXXXXXXXXXXXXXXXXXXXXXXXX", // goto https://developers.nativedocuments.com/ to get a dev-id/dev-secret // ND_DEV_SECRET: "YYYYYYYYYYYYYYYYYYYYYYYYYY", // you can also set the credentials in the enviroment variables ENVIRONMENT: "NODE", // required LAZY_INIT: true // if set to false the WASM engine will be initialized right now, usefull pre-caching (like eg for AWS lambda) }).catch( function(e) { console.error(e); }); async function convertHelper(document, exportFct) { const api = await docx.engine(); await api.load(document); const arrayBuffer = await api[exportFct](); await api.close(); return arrayBuffer; } convertHelper("sample.docx", "exportPDF").then((arrayBuffer) => { fs.writeFileSync("sample.pdf", new Uint8Array(arrayBuffer)); }).catch((e) => { console.error(e); }); 

As you can see from the above code, you will need an API key (freemium model).

0
source

Docx to pdf A library that converts a docx file to pdf.

Installation:

 npm install docx-pdf --save 

using

  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); }); 

The output should be output.pdf, which will be produced at the output path you specify

0
source

All Articles