Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

pdf.js + node.js

2,730 views
Skip to first unread message

François VANHILLE

unread,
Nov 25, 2012, 11:16:52 AM11/25/12
to dev-p...@lists.mozilla.org
Hello erverybody,

Recently I discovered pdf.js and I appreciated to use it.
Unfortunately the system is jsut a little slow when heavy pdf are loaded.
So my idea is to combine pdf.js with node.js to run the code on a server.

I tried something like this :
*
eval(fs.readFileSync('pdf.js')+'');

var canvas = new Canvas(320, 320);
var ctx = canvas.getContext('2d');

function renderPage(num) {
pdf.getPage(num).then(function(page) {
var scale = 0.8;
var viewport = page.getViewport(scale);
canvas.height = viewport.height;
canvas.width = viewport.width;
var renderContext = {
canvasContext: ctx,
viewport: viewport
};
page.render(renderContext);
});
}
http.createServer(function (req, res) {
var file = 'index.pdf';
var array = fs.readFileSync(file);
var uint8Array = new Uint8Array(array);
var parameters = {};
parameters.data = uint8Array;
PDFJS.getDocument(parameters).then(function(pdfDoc) {
pdf = pdfDoc;
renderPage(1);
});
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(''+ '<img src="' + canvas.toDataURL() + '" />');
}).listen(3000);*


I had to make some quick changes in the code to intagrate it with node.js,
but I did not have any result in the client end, I had a blank image with
no changes in the picture byte code.

Have you ever tried to use pdf.js with node.js, or do you have a special
module for node.js ?

Best regards,

François VANHILLE
Java/J2EE Expert - Search&Connect

Julian Viereck

unread,
Nov 27, 2012, 9:44:20 AM11/27/12
to François VANHILLE, dev-p...@lists.mozilla.org
You cannot use node.js direclty, as it doesn't have support for the HTML canvas, HTML images and also lacks a way to load fonts. There is node-canvas, which solves the first two issues. I've worked on solving the last issue of loading fonts. The result is the `node-pdfreader` library:

https://github.com/jviereck/node-pdfreader

it points to a special build of node-canvas, that has font-support.

I've done the node-pdfreader as a proof of concept as it was request may times. Note that it's not really tested, so be careful before you start using it in production.

Best,

Julian
> _______________________________________________
> dev-pdf-js mailing list
> dev-p...@lists.mozilla.org
> https://lists.mozilla.org/listinfo/dev-pdf-js

samuel...@gmail.com

unread,
Dec 20, 2012, 9:22:51 AM12/20/12
to mozilla.d...@googlegroups.com, dev-p...@lists.mozilla.org, François VANHILLE
I'll just post this here, someone made another port for parsing pdf to json
https://github.com/modesty/pdf2json

Still missing some important features tho, i don't understand why the dictionary parts for color and text style should be necissary =/ would be great to access this data directly.
0 new messages