Skip to content

Instantly share code, notes, and snippets.

@ashumkin
Created March 29, 2016 21:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ashumkin/a291dbd17b9ade663afc08b2413e47b3 to your computer and use it in GitHub Desktop.
Save ashumkin/a291dbd17b9ade663afc08b2413e47b3 to your computer and use it in GitHub Desktop.
// Console: SaveAllText
// Description: SaveAllText input file
// Parameters: [input file] [output file]
/** Print help for savealltext */
function savealltext_help() {
print(tr("Usage:"));
print("savealltext ["+tr("input file")+"] ["+tr("output file")+"]");
print(" "+tr("Input file must exist"));
print(" "+tr("Output file must not exist"));
exit(1);
}
function savealltext_fail(err) {
print(tr("savealltext failed!"));
print(err);
exit(2);
}
function saveAsText_save(p,f) {
document=loadPdf(p)
qs="";
pages=document.getPageCount();
for (i=1;i<=pages;i++) {
pg=document.getPage(i);
text=pg.getText();
qs+=text;
qs+="\n";
}
saveFile(f,qs);
}
p=parameters();
if (p.length!=2) {
savealltext_help("savealltext "+tr("is expecting two parameters"));
}
inFile=p[0];
outFile=p[1];
if (!exists(inFile)) savealltext_fail(tr("Input file '%1' does not exist").arg(inFile));
if (exists(outFile)) savealltext_fail(tr("Output file '%1' already exist").arg(outFile));
if (inFile==outFile) savealltext_fail(tr("Input and output files must be different"));
if (saveAsText_save(inFile,outFile)) {
} else {
print(tr("savealltext")+" :"+inFile+" -> "+outFile);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment