How to convert a PDF file with NodeJS + Unoconv - node.js

I need to convert a docx file to pdf but I don't know very well nodejs, however, I know that the following can be done:
There is a project called unoconv-worker and in it, there is a part where the following line appears:
var child = spawn ('unoconv', [
'--stdout',
'--no-launch',
'--format', job.outputExtension,
job.tempPath
]);
https://github.com/koumoul-dev/unoconv-worker/blob/master/route.js
In my terminal I can convert it in the following way and it works perfectly:
unoconv -f pdf --output="something.pdf" docxtoconvert.docx
However, I would like to give you a file that I gave you the route, so I tried it this way:
var filePath = "/tmp/docxtoconvert.docx";
var child = spawn ("unoconv", [
"-f",
"pdf",
"--output",
"/tmp/something.pdf",
filePath
]);
Output:
Unoconv converter received message on stderr function () {
if (arguments.length === 0) {
var result = this.utf8Slice(0, this.length);
} else {
var result = slowToString.apply(this, arguments);
}
if (result === undefined)
throw new Error('toString failed');
return result;
}
But it has not worked. Could you help me? Thank you

Lot of wrapper modules exists for unoconv that can solve your problem.
You can try this
https://www.npmjs.com/package/unoconv

Related

PDF to Image in node

I am using node-express and need to convert pdf into image in the server side.
Pdf-poppler is not working in linux and pdf-image happens to be not working as well.
Any alternate method how we can convert and save pdf to image in the backend.
Pdf-poppler use pdftocairo command provided by the poppler project. Since poppler did support Linux, you can install it by yourself.
For example, on ubuntu
apt-get install poppler-utils
then call pdftocairo command using the code below from Pdf-poppler.
const path = require('path');
const {execFile} = require('child_process');
const pdftocairoBin = '/usr/bin/pdftocairo';
const FORMATS = ['png', 'jpeg', 'tiff', 'pdf', 'ps', 'eps', 'svg'];
let defaultOptions = {
format: 'jpeg',
scale: 1024,
out_dir: null,
out_prefix: null,
page: null
};
function pdf2image(file, opts) {
return new Promise((resolve, reject) => {
opts.format = FORMATS.includes(opts.format) ? opts.format : defaultOptions.format;
opts.scale = opts.scale || defaultOptions.scale;
opts.out_dir = opts.out_dir || defaultOptions.out_dir;
opts.out_prefix = opts.out_prefix || path.dirname(file);
opts.out_prefix = opts.out_prefix || path.basename(file, path.extname(file));
opts.page = opts.page || defaultOptions.page;
let args = [];
args.push([`-${opts.format}`]);
if (opts.page) {
args.push(['-f']);
args.push([parseInt(opts.page)]);
args.push(['-l']);
args.push([parseInt(opts.page)]);
}
if (opts.scale) {
args.push(['-scale-to']);
args.push([parseInt(opts.scale)]);
}
args.push(`${file}`);
args.push(`${path.join(opts.out_dir, opts.out_prefix)}`);
execFile(pdftocairoBin, args, {
encoding: 'utf8',
maxBuffer: 5000*1024,
shell: false
}, (err, stdout, stderr) => {
if (err) {
reject(err);
}
else {
resolve(stdout);
}
});
});
};
Usage of pdf2image function
pdf2image('./input.pdf', {out_dir:'./', out_prefix:'ppp'});
may be you can try this bro:
Create an uploads folder & copy the sample.pdf file which you want to convert as png. since here I am only going to show you how to convert the pdf to png I am using the pdf as a static path you need to upload that pdf file via REST API.
Now open your app.js OR index.js file & paste the below code we are using the pdf2pic packages to covert the pdf pages to png images.
or you visit this link:
https://codinghub.medium.com/how-to-convert-pdf-file-pages-to-png-images-node-js-dccec010bf13

Reading file using Node.js "Invalid Encoding" Error

I am creating an application with Node.js and I am trying to read a file called "datalog.txt." I use the "append" function to write to the file:
//Appends buffer data to a given file
function append(filename, buffer) {
let fd = fs.openSync(filename, 'a+');
fs.writeSync(fd, str2ab(buffer));
fs.closeSync(fd);
}
//Converts string to buffer
function str2ab(str) {
var buf = new ArrayBuffer(str.length*2); // 2 bytes for each char
var bufView = new Uint16Array(buf);
for (var i=0, strLen=str.length; i < strLen; i++) {
bufView[i] = str.charCodeAt(i);
}
return buf;
}
append("datalog.txt","12345");
This seems to work great. However, now I want to use fs.readFileSync to read from the file. I tried using this:
const data = fs.readFileSync('datalog.txt', 'utf16le');
I changed the encoding parameter to all of the encoding types listed in the Node documentation, but all of them resulted in this error:
TypeError: Argument at index 2 is invalid: Invalid encoding
All I want to be able to do is be able to read the data from "datalog.txt." Any help would be greatly appreciated!
NOTE: Once I can read the data of the file, I want to be able to get a list of all the lines of the file.
Encoding and type are an object:
const data = fs.readFileSync('datalog.txt', {encoding:'utf16le'});
Okay, after a few hours of troubleshooting a looking at the docs I figured out a way to do this.
try {
// get metadata on the file (we need the file size)
let fileData = fs.statSync("datalog.txt");
// create ArrayBuffer to hold the file contents
let dataBuffer = new ArrayBuffer(fileData["size"]);
// read the contents of the file into the ArrayBuffer
fs.readSync(fs.openSync("datalog.txt", 'r'), dataBuffer, 0, fileData["size"], 0);
// convert the ArrayBuffer into a string
let data = String.fromCharCode.apply(null, new Uint16Array(dataBuffer));
// split the contents into lines
let dataLines = data.split(/\r?\n/);
// print out each line
dataLines.forEach((line) => {
console.log(line);
});
} catch (err) {
console.error(err);
}
Hope it helps someone else with the same problem!
This works for me:
index.js
const fs = require('fs');
// Write
fs.writeFileSync('./customfile.txt', 'Content_For_Writing');
// Read
const file_content = fs.readFileSync('./customfile.txt', {encoding:'utf8'}).toString();
console.log(file_content);
node index.js
Output:
Content_For_Writing
Process finished with exit code 0

Renaming multiple files asyncroniosly causing error in Node.js

I am trying to rename a bunch of pre-generated testing files (1000+) asynchronously in Node.js.
The code looks like the following:
const fs = require('fs')
const { each } = require('async')
each(files, file => {
let newfile = 'new' + file
fs.rename(file, newfile, err => {
err ? console.log(err) : console.log('renamed')
}
})
This leads to following error:
Uncaught Error: ENOENT: no such file or directory, lstat '8d3320e35d22772f'
at fs.lstatSync (fs.js:902:18)
at Object.fs.lstatSync
It's not async module issue, since replacing each with native forEach leads to the same error. Also, there are no issues when using synchronous version of rename fs.renameSync.
I think it's trying to move some file twice or so but can't figure where exactly mistake is. Made this assumption, because all files have been already renamed successfully and very likely error generated afterward. Can someone advice what causing such behavior?
My bad. Just in case someone curious, this error came from following underlying function:
function rmDir(dir) {
var list = fs.readdirSync(dir);
for(var i = 0; i < list.length; i++) {
var dirOrFile = path.join(dir, list[i]);
var stat = fs.lstatSync(dirOrFile);
if(dirOrFile == "." || dirOrFile == "..") {
// pass these files
} else if (stat.isDirectory()) {
// rmdir recursively
rmDir(dirOrFile);
}
// else { fs.unlinkSync(dirOrFile) } // rm fiilename
}
fs.rmdirSync(dir);
}

Redirecting node.js output to text file using shell script

I want to redirect the output of node.js to txt file.I am able to get the output in console for node.js.
Please help me in this issue.
This is the snippet:
node.forEach(function(elem)
{
var toString=elem.path.toString;
console.log(toString);
}
Output looks something like this
ui.apps/src/main/content/jcr_root/components/content/smartcart/deviceselector/cq_editConfig.xml
ui.apps/src/main/content/jcr_root/components/content/smartcart/deviceselector/deviceselector.html
Writing a string to a file
If you are just trying to save the output from that one particular function, you can use the fs node core module
var fs = require("fs");
var output = "";
for (var i = 0; i < node.length; i++) {
var elem = node[i];
output += elem.path.toString() + "\n";
}
fs.writeFile("output.txt", output, function (err) {
if (err) {
return console.log(err);
}
console.log("output.txt saved");
});
Writing all output to a file
If you are trying to capture everything that is logged to a log file, you have a number of options:
Use a loggin package such as winston.js which provides ways of writing logs to a specified file
On a unix-based system, pipe the output of node to a file
node my-file.js > output.txt
fs = require('fs');
fs.appendFile(__dirname + '/textFileName.txt', toString, function (err) {
if (err) return console.log(err);
console.log('Hello World > helloworld.txt');
});

wkhtmltopdf on nodejs generates corrupt pdfs

I am using wkhtmltopdf to generate pdfs in nodejs
Below is my sample code to generate pdf
var wkhtmltopdf = require('wkhtmltopdf')
, createWriteStream = require('fs').createWriteStream;
var r = wkhtmltopdf('http://www.google.com', { pageSize: 'letter' })
.pipe(createWriteStream('C:/MYUSERNAME/demo.pdf'));
r.on('close', function(){
mycallback();
});
The above code is generating corrupt pdfs. I could not figure out the issue.
Although when I generate pdfs using command prompt it is generating correctly
like when I use below code in windows command prompt
wkhtmltopdf http://www.google.com demo.pdf
I get correct pdf generated,sadly when I try to generate pdf in node environment, it generates corrupt pdfs.
Incase it helps I'm using wkhtmltopdf 0.11.0 rc2
Thanks in advance.
wkhtmltopdf for node has a bug for windows, so you can write a new one.
Like this:
function wkhtmltopdf(input, pageSize) {
var spawn = require('child_process').spawn;
var html;
var isUrl = /^(https?|file):\/\//.test(input);
if (!isUrl) {
html = input;
input = '-';
}
var args = ['wkhtmltopdf', '--quiet', '--page-size', pageSize, input, '-']
if (process.platform === 'win32') {
var child = spawn(args[0], args.slice(1));
} else {
var child = spawn('/bin/sh', ['-c', args.join(' ') + ' | cat']);
}
if (!isUrl) {
child.stdin.end(html);
}
return child.stdout;
}
// usage:
createWriteStream = require('fs').createWriteStream;
wkhtmltopdf('http://google.com/', 'letter')
.pipe(createWriteStream('demo1.pdf'));
wkhtmltopdf('<body>hello world!</body>', 'letter')
.pipe(createWriteStream('demo2.pdf'));
note: the param is now 'letter' not { pageSize: 'letter' }

Resources