Skip to content

Instantly share code, notes, and snippets.

@yurydelendik
Last active December 20, 2015 09:38
Show Gist options
  • Select an option

  • Save yurydelendik/6108796 to your computer and use it in GitHub Desktop.

Select an option

Save yurydelendik/6108796 to your computer and use it in GitHub Desktop.
Utility to extract PDF info
fs = require('fs');
function fromPdfString(s) {
var t = '';
for (var i = 0; i < s.length; i++) {
if (s[i] != '\\') {
t += s[i];
continue;
}
if (s[i + 1] < "A") {
if (s[i + 1] === '0' || s[i + 1] === '1' ||
s[i + 1] === '2' || s[i + 1] === '3') {
t += String.fromCharCode(parseInt(s.substring(i + 1, i + 4), 8));
i += 3;
} else {
t += s[++i];
}
continue;
}
var j = s[i + 1] === 'x' ? i + 4 : i + 2;
t += JSON.parse('"' + s.substring(i, j) + '"');
i = j - 1;
}
if (t.charCodeAt(0) === 0xFE && t.charCodeAt(1) === 0xFF) {
var u = '';
for (var i = 2; i < t.length; i += 2)
u += String.fromCharCode((t.charCodeAt(i) << 8) |
t.charCodeAt(i + 1));
t = u;
}
return t;
}
process.argv.forEach(function (val, index, array) {
if (index < 2) return;
try {
var data = fs.readFileSync(val, {encoding: 'binary'})
var m = /^%PDF-(\d\.\d)/.exec(data);
var version = m ? m[1] : '';
m = /\/Producer\s*\((.*?)\)\s*[\r\n\/>]/.exec(data);
var producer = m ? fromPdfString(m[1]) : '';
m = /\/Creator\s*\((.*?)\)\s*[\r\n\/>]/.exec(data);
var creator = m ? fromPdfString(m[1]) : '';
console.log(val + '\t' + version + '\t' + producer + '\t' + creator);
} catch (ex) {}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment