Skip to content

Commit

Permalink
PDF logs everything that happens on gs, docsplit and pdf2htmlex to lo…
Browse files Browse the repository at this point in the history
…g/conversions.log
  • Loading branch information
daneryl committed Aug 25, 2016
1 parent 2edbdc1 commit b8ce8bc
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ node_modules
coverage
/dist
/build
/log
*.log
npm-debug
uploads/*
app/api/upload/specs/uploads/*
Expand Down
16 changes: 10 additions & 6 deletions app/api/upload/PDF.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@ let generateOutputPath = (filepath) => {
export default class PDF extends EventEmitter {
constructor(filepath) {
super();
this.logFilePath = __dirname+'/../../../log/conversions.log';
this.filepath = filepath;
}

optimize() {
let logFile = fs.createWriteStream(this.logFilePath, {flags: 'a'});
let options = [ '-sDEVICE=pdfwrite', '-dNOPAUSE', '-dBATCH', `-sOutputFile=${generateOutputPath(this.filepath)}`, this.filepath ];
let conversion = spawn('gs', options);
conversion.stderr.pipe(logFile);
conversion.stdout.pipe(logFile);

let pages = 0;
conversion.stdout.on('data', (data) => {
Expand All @@ -43,9 +47,12 @@ export default class PDF extends EventEmitter {
}

extractText() {
let logFile = fs.createWriteStream(this.logFilePath, {flags: 'a'});
let tmpPath = '/tmp/' + Date.now() + 'docsplit/';
let options = ['text', '-o', tmpPath, this.filepath];
let extraction = spawn('docsplit', options);
extraction.stderr.pipe(logFile);
extraction.stdout.pipe(logFile);

return new Promise((resolve, reject) => {
extraction.stderr.on('data', (error) => reject(error));
Expand All @@ -58,6 +65,7 @@ export default class PDF extends EventEmitter {
}

toHTML() {
let logFile = fs.createWriteStream(this.logFilePath, {flags: 'a'});
let destination = '/tmp/' + Date.now() + '/';
let options = [
this.optimizedPath,
Expand All @@ -76,14 +84,10 @@ export default class PDF extends EventEmitter {
];

let conversion = spawn('pdf2htmlEX', options);
conversion.stderr.pipe(logFile);
conversion.stdout.pipe(logFile);

return new Promise((resolve) => {
//conversion.stderr.on('data', (error) => {
////console.log(error.toString());
//});
//conversion.stdout.on('data', (data) => {
////console.log(data.toString());
//});
conversion.stdout.on('close', () => {
fs.readdir(destination, (err, filenames) => {
let orderedPageFiles = filenames
Expand Down
4 changes: 4 additions & 0 deletions app/api/upload/specs/PDF.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ describe('PDF', function () {
describe('optimize', () => {
let filepath = '/the/pdf/path.pdf';
let commandBeingExecuted = {stdout: new Events(), stderr: new Events()};
commandBeingExecuted.stdout.pipe = () => {};
commandBeingExecuted.stderr.pipe = () => {};
beforeEach(() => {
spyOn(childProcess, 'spawn').and.returnValue(commandBeingExecuted);
pdf = new PDFObject(filepath);
Expand Down Expand Up @@ -85,6 +87,8 @@ describe('PDF', function () {

it('should reject the promise when there is an error', (done) => {
let commandBeingExecuted = {stdout: new Events(), stderr: new Events()};
commandBeingExecuted.stdout.pipe = () => {};
commandBeingExecuted.stderr.pipe = () => {};
spyOn(childProcess, 'spawn').and.returnValue(commandBeingExecuted);

pdf.extractText()
Expand Down
File renamed without changes.

0 comments on commit b8ce8bc

Please sign in to comment.