Created
February 8, 2019 16:58
-
-
Save rainabba/d6298b46a01f2558fd06b8f8beea1a39 to your computer and use it in GitHub Desktop.
Testing common solutions to PDF -> image conversion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPOILER: Only 1 of these tests passes at this point and it bypasses | |
// all the fluff and complication so it's the one I will end up using | |
const { expect } = require('code'), | |
Lab = require('lab'), | |
lab = exports.lab = Lab.script(), | |
fs = require("fs"), | |
path = require("path"), | |
mkdirp = require('mkdirp'), | |
rimraf = require('rimraf'), | |
tempPath = path.join( __dirname, 'temp' ), | |
pdfPath = path.join( tempPath, 'live-testing.pdf' ), | |
StreamSplitter = require("stream-splitter"), | |
splitBuffer = new Buffer([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00]), // PNG header values; | |
{ spawn } = require('child_process'); | |
lab.experiment("pdfToImageTest", { timeout: 8000 }, () => { | |
lab.before( () => { | |
return new Promise( (resolve, reject) => { | |
mkdirp.sync( tempPath ); | |
expect( fs.existsSync(tempPath) ).to.be.equal( true ); | |
rimraf.sync( tempPath+'/*' ); | |
fs.copyFile( path.join( __dirname, 'assets/test.pdf'), pdfPath, (err) => { | |
if (err) reject(err); | |
expect( fs.existsSync(pdfPath) ).to.be.equal( true ); | |
resolve(); | |
}); | |
}) | |
}); | |
lab.test('convert PDF to images using child.spawn', { timeout: 60000 }, () => { | |
return new Promise( (resolve, reject) => { | |
let extension = require('path').extname(pdfPath), | |
basename = require('path').basename(pdfPath, extension), | |
counter = 0, | |
stream = StreamSplitter(splitBuffer); | |
// `time gm convert -verbose -density 150x150 ./temp/live-testing.pdf -quality 90 +adjoin ./temp/live-testing-%04d.jpg` | |
const child = spawn('gm', [ 'convert', '-verbose', '-density', '150x150', pdfPath, '-quality', '90', '+adjoin', path.join(tempPath, 'live-testing-child-%04d.jpg') ] ); | |
child.stderr.on( 'data', data => { // verbose updates hit stderr, not stdout | |
console.log( `${data}` ); | |
} ); | |
child.on( 'close', code => { | |
if ( code === 0 ) { | |
expect( fs.existsSync(path.join(tempPath, 'live-testing-child-0000.jpg')) ).to.be.equal( true ); | |
expect( fs.existsSync(path.join(tempPath, 'live-testing-child-0011.jpg')) ).to.be.equal( true ); | |
resolve(); | |
} else { | |
console.error( `child process exited with code ${code}` ); | |
reject( code ); | |
} | |
}); | |
}); | |
}); | |
// This test fails with no output, feedback or indication of why 'new token!' is never seen | |
lab.test('convert PDF to images using gm library', { timeout: 60000 }, () => { | |
return new Promise( (resolve, reject) => { | |
const gm = require("gm").subClass({ imageMagick: true }); | |
let extension = require('path').extname(pdfPath), | |
basename = require('path').basename(pdfPath, extension), | |
counter = 0, | |
stream = StreamSplitter(splitBuffer); | |
// `time gm convert -verbose -density 150x150 ./temp/live-testing.pdf -quality 90 +adjoin ./temp/live-testing-%04d.jpg` | |
stream.on('token', token => { | |
console.log('DEBUG: New token?'); | |
if (token.length > 0) { | |
console.log('new token!'); | |
var filename = basename + '-gm-' + counter++ + ".jpg"; | |
var outFile = require('fs').createWriteStream( path.join(tempPath, filename) ); | |
// Since it's split on the splitBuffer insert it back into the output stream | |
outFile.write(splitBuffer); | |
// As well as the rest of the PNG | |
outFile.write(token); | |
outFile.end(); | |
} else { | |
console.log('DEBUG: NO token'); | |
} | |
}); | |
stream.on('close', () => { | |
console.log('DEBUG: gm stream closed'); | |
}); | |
gm( pdfPath ).command( 'convert' ) | |
.density(400, 400) | |
.unsharp(2, 4, 4, 0.05) | |
.resize('50%','50%') | |
.out('-quality', '90') | |
.out('-type', 'TrueColorMatte') | |
.adjoin() | |
.stream('PNG').pipe( stream ); | |
}); | |
}); | |
lab.test('convert PDF to images using PDFImage library', { timeout: 60000 }, () => { | |
// Test fails with the following. Each time I seem to get a randomly different .png though and out of sequence | |
// error: { | |
// cmd: "gm convert \"/usr/src/test/temp/live-testing.pdf[8]\" \"/usr/src/test/temp/live-testing-8.png\"", | |
// code: 1, | |
// killed: false, | |
// signal: null | |
// }, | |
// message: "Failed to convert page to image", | |
// stderr: "gm convert: Unable to open file (/usr/src/test/temp/live-testing.pdf) [No such file or directory].\n", | |
// stdout: "" | |
return new Promise( (resolve, reject) => { | |
const PDFImage = require("pdf-image").PDFImage | |
let pdfImage = new PDFImage( pdfPath, { //pdfPath is set global to all tests and the path works for other tests | |
graphicsMagick: true | |
}) | |
.convertFile() | |
.then( imagePath => { | |
// Never hit because of the error above | |
console.dir( imagePath ); | |
resolve( imagePath ); | |
}) | |
.catch( err => { | |
reject( err ); | |
}); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment