de36b2aaba
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes). Prettier is being used for a couple of reasons: - To be consistent with `mozilla-central`, where Prettier is already in use across the tree. - To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters. Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some). Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long. *Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit. (On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
76 lines
2.4 KiB
JavaScript
76 lines
2.4 KiB
JavaScript
/* Any copyright is dedicated to the Public Domain.
|
|
* http://creativecommons.org/publicdomain/zero/1.0/ */
|
|
|
|
//
|
|
// Basic node example that prints document metadata and text content.
|
|
// Requires single file built version of PDF.js -- please run
|
|
// `gulp singlefile` before running the example.
|
|
//
|
|
|
|
// Run `gulp dist-install` to generate 'pdfjs-dist' npm package files.
|
|
var pdfjsLib = require("pdfjs-dist");
|
|
|
|
// Loading file from file system into typed array
|
|
var pdfPath = process.argv[2] || "../../web/compressed.tracemonkey-pldi-09.pdf";
|
|
|
|
// Will be using promises to load document, pages and misc data instead of
|
|
// callback.
|
|
var loadingTask = pdfjsLib.getDocument(pdfPath);
|
|
loadingTask.promise
|
|
.then(function(doc) {
|
|
var numPages = doc.numPages;
|
|
console.log("# Document Loaded");
|
|
console.log("Number of Pages: " + numPages);
|
|
console.log();
|
|
|
|
var lastPromise; // will be used to chain promises
|
|
lastPromise = doc.getMetadata().then(function(data) {
|
|
console.log("# Metadata Is Loaded");
|
|
console.log("## Info");
|
|
console.log(JSON.stringify(data.info, null, 2));
|
|
console.log();
|
|
if (data.metadata) {
|
|
console.log("## Metadata");
|
|
console.log(JSON.stringify(data.metadata.getAll(), null, 2));
|
|
console.log();
|
|
}
|
|
});
|
|
|
|
var loadPage = function(pageNum) {
|
|
return doc.getPage(pageNum).then(function(page) {
|
|
console.log("# Page " + pageNum);
|
|
var viewport = page.getViewport({ scale: 1.0 });
|
|
console.log("Size: " + viewport.width + "x" + viewport.height);
|
|
console.log();
|
|
return page
|
|
.getTextContent()
|
|
.then(function(content) {
|
|
// Content contains lots of information about the text layout and
|
|
// styles, but we need only strings at the moment
|
|
var strings = content.items.map(function(item) {
|
|
return item.str;
|
|
});
|
|
console.log("## Text Content");
|
|
console.log(strings.join(" "));
|
|
})
|
|
.then(function() {
|
|
console.log();
|
|
});
|
|
});
|
|
};
|
|
// Loading of the first page will wait on metadata and subsequent loadings
|
|
// will wait on the previous pages.
|
|
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);
|
|
}
|
|
);
|