2014-08-14 05:04:47 +09:00
|
|
|
/* Any copyright is dedicated to the Public Domain.
|
|
|
|
* http://creativecommons.org/publicdomain/zero/1.0/ */
|
|
|
|
|
|
|
|
//
|
|
|
|
// Node tool to dump SVG output into a file.
|
|
|
|
//
|
|
|
|
|
|
|
|
var fs = require('fs');
|
|
|
|
|
|
|
|
// HACK few hacks to let PDF.js be loaded not as a module in global space.
|
2014-10-22 23:59:20 +09:00
|
|
|
require('./domstubs.js');
|
|
|
|
|
2016-03-05 04:30:36 +09:00
|
|
|
// Run `gulp dist` to generate 'pdfjs-dist' npm package files.
|
2016-03-29 06:44:27 +09:00
|
|
|
var pdfjsLib = require('../../build/dist');
|
2014-08-14 05:04:47 +09:00
|
|
|
|
|
|
|
// Loading file from file system into typed array
|
|
|
|
var pdfPath = process.argv[2] || '../../web/compressed.tracemonkey-pldi-09.pdf';
|
|
|
|
var data = new Uint8Array(fs.readFileSync(pdfPath));
|
|
|
|
|
|
|
|
// Dumps svg outputs to a folder called svgdump
|
|
|
|
function writeToFile(svgdump, pageNum) {
|
|
|
|
var name = getFileNameFromPath(pdfPath);
|
|
|
|
fs.mkdir('./svgdump/', function(err) {
|
|
|
|
if (!err || err.code === 'EEXIST') {
|
|
|
|
fs.writeFile('./svgdump/' + name + "-" + pageNum + '.svg', svgdump,
|
|
|
|
function(err) {
|
|
|
|
if (err) {
|
|
|
|
console.log('Error: ' + err);
|
|
|
|
} else {
|
|
|
|
console.log('Page: ' + pageNum);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get filename from the path
|
|
|
|
|
|
|
|
function getFileNameFromPath(path) {
|
|
|
|
var index = path.lastIndexOf('/');
|
|
|
|
var extIndex = path.lastIndexOf('.');
|
|
|
|
return path.substring(index , extIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Will be using promises to load document, pages and misc data instead of
|
|
|
|
// callback.
|
2016-03-29 06:44:27 +09:00
|
|
|
pdfjsLib.getDocument(data).then(function (doc) {
|
2014-08-14 05:04:47 +09:00
|
|
|
var numPages = doc.numPages;
|
|
|
|
console.log('# Document Loaded');
|
|
|
|
console.log('Number of Pages: ' + numPages);
|
|
|
|
console.log();
|
|
|
|
|
|
|
|
var lastPromise = Promise.resolve(); // will be used to chain promises
|
|
|
|
var loadPage = function (pageNum) {
|
|
|
|
return doc.getPage(pageNum).then(function (page) {
|
|
|
|
console.log('# Page ' + pageNum);
|
|
|
|
var viewport = page.getViewport(1.0 /* scale */);
|
|
|
|
console.log('Size: ' + viewport.width + 'x' + viewport.height);
|
|
|
|
console.log();
|
2015-02-03 00:12:52 +09:00
|
|
|
|
2014-08-14 05:04:47 +09:00
|
|
|
return page.getOperatorList().then(function (opList) {
|
2016-03-29 06:44:27 +09:00
|
|
|
var svgGfx = new pdfjsLib.SVGGraphics(page.commonObjs, page.objs);
|
2014-08-15 05:11:27 +09:00
|
|
|
svgGfx.embedFonts = true;
|
2014-08-15 02:56:11 +09:00
|
|
|
return svgGfx.getSVG(opList, viewport).then(function (svg) {
|
|
|
|
var svgDump = svg.toString();
|
2014-08-14 05:04:47 +09:00
|
|
|
writeToFile(svgDump, pageNum);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
})
|
|
|
|
};
|
2015-02-03 00:12:52 +09:00
|
|
|
|
2014-08-14 05:04:47 +09:00
|
|
|
for (var i = 1; i <= numPages; i++) {
|
|
|
|
lastPromise = lastPromise.then(loadPage.bind(null, i));
|
|
|
|
}
|
|
|
|
return lastPromise;
|
|
|
|
}).then(function () {
|
|
|
|
console.log('# End of Document');
|
|
|
|
}, function (err) {
|
|
|
|
console.error('Error: ' + err);
|
|
|
|
});
|