Forum News

New Node/JavaScript integration API for PrinceXML available!

rse
I've developed a new MIT-licensed Node/JavaScript integration API for PrinceXML:

https://github.com/rse/node-prince

This is a Node API for executing the PrinceXML CLI prince(1) from within JavaScript. The essential point of this Node extension is not primarily to just abstract away the asynchronous CLI execution. Instead there are two other major points: First, this Node extension provides a fixed dependency, as other Node extensions which require PrinceXML can just depend (via their NPM package.json file) onto this extension. Second, as this Node extension can — across platforms — automatically download, locally unpack and use a PrinceXML distribution, there is no need for any previously available global PrinceXML installation. Just depend on this Node extension and PrinceXML is available!

Just run "npm install prince" and you can immediately use PrinceXML form within JavaScript:

var Prince = require("prince");
Prince()
    .inputs("test.html")
    .output("test.pdf")
    .execute()
    .then(function () {
        console.log("OK: done");
    }, function (error) {
        console.log("ERROR: ", error);
    })


I hope this Node extension will be found useful and makes the awesome PrinceXML available to another large community...

Ralf S. Engelschall
rse@engelschall.com
www.engelschall.com

Edited by rse

jim_albright
Cool. I'm just now learning about Node.js.

Jim Albright
Wycliffe Bible Translators

stdi-multimedia
Hi, i've got some trouble implementing that function in node-webkit.
Everything works fine when my app is executed on OSX platform.
However the PDF can't be generated when i test it on a PC (win7 and winXP).

I don't think that it could be a question of permission because i am able to create files/folders from node-webkit.

The error returned by the node-webkit console is the following one :
"Error: err >= 0
at exports._errnoException (util.js:676:20)
at ChildProcess.spawn (child_process.js:973:11)
at exports.spawn (child_process.js:753:9)
at Object.exports.execFile (child_process.js:608:15)
at eval (myProject\Ressources\node_modules\prince\prince-api.js:242:27)
at doResolve (myProject\Ressources\node_modules\prince\node_modules\promise\core.js:91:5)
at new Promise (myProject\Ressources\node_modules\prince\node_modules\promise\core.js:71:3)
at Prince._execute (myProjectRessources\node_modules\prince\prince-api.js:237:12)
at Prince.execute (myProject\Ressources\node_modules\prince\prince-api.js:283:17)
at eval (file:///myProject/Ressources/js/HtmlToPdf.js:102:10)"

Thanks for your help!
STDI-multimedia
mikeday
Perhaps you are not specifying the correct path to the Prince engine executable? For example, "C:\Program Files\Prince\Engine\bin\prince.exe", or something similar.
stdi-multimedia
I am in a node-js environnement.
So i call the Prince npm : require('prince')
and it works fine.

The issue appears when the function .execute() is called.
mikeday
Looking at prince-api.js, it appears to be trying to figure out where the prince.exe file is by checking the PATH? I think you should manually specify it before calling execute(). Can you set prince.config.binary or something like that?
stdi-multimedia
Thanks!!!! I finally found how to do.
You were right, i had to install Prince on my OS. I had to edit prince-api.js in order to correctly choose the executable :

var install = [
{ basedir: "princePC\\Engine", binary: "bin\\prince.exe" },
{ basedir: "prince/lib/prince", binary: "bin/prince" }
];
var basedir;
var binary;
if(/^win/.test(process.platform)) {
basedir = path.resolve(path.join(__dirname, install[0].basedir));
binary = path.join(basedir, install[0].binary);
if (fs.existsSync(binary)) {
this.binary(binary);
this.prefix(basedir);
}
}else{
basedir = path.resolve(path.join(__dirname, install[1].basedir));
binary = path.join(basedir, install[1].binary);
if (fs.existsSync(binary)) {
this.binary(binary);
this.prefix(basedir);
}
}
mikeday
It might be worth passing on any modifications to the author of prince-api.js.
stdi-multimedia
Hi,

Unfortunately, my project has an other similar error...
I made 2 packages with my validated application (.exe and .app)
Everything goes right with the exe file, but i receive that error with the .app : "spawn EACCESS"

I trace my variables, the prince app successfully find the prince location but the pdf generation goes wrong...

Have you got any idea about that issue?
Thanks
mikeday
I don't know much about Node.js, but that sounds like it is trying to execute a file it does not have permission to access.