Skip to content

Commit

Permalink
Add support for buffers
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcus Gartner committed Jun 19, 2017
1 parent daa281d commit 22cbf39
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 27 deletions.
5 changes: 4 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
extends: "eslint-config-lob"
extends: "lob",
globals: {
"expect": false
}
}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@ node_modules

# Users Environment Variables
.lock-wscript

# Project-specific .vimrc
.vimrc
8 changes: 4 additions & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

var Promise = require('bluebird');
var PopplerDocument = require('poppler-simple').PopplerDocument;
var utils = require('./utils');
var Utils = require('./utils');

exports.detect = function (buffer) {
return utils.ascii(buffer, 0, 4) === '%PDF';
return Utils.ascii(buffer, 0, 4) === '%PDF';
}

exports.measure = function (path) {
exports.measure = function (pathOrBuffer) {
return new Promise(function (resolve, reject) {
try {
var doc = new PopplerDocument(path);
var doc = new PopplerDocument(pathOrBuffer);

if (doc.pageCount < 1) {
return reject(new Error('Invalid PDF'));
Expand Down
68 changes: 48 additions & 20 deletions test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,31 @@
'use strict';

var fs = require('fs');
var path = require('path');
var expect = require('chai').expect;
var pdf = require('../lib/index');
var Fs = require('fs');
var Path = require('path');
var Pdf = require('../lib/index');

describe('pdf', function () {

describe('detect', function () {

it('should return true for a PDF', function () {
var pdfPath = path.resolve(__dirname, 'fixtures/pdf/123x456.1.pdf');
var result = pdf.detect(fs.readFileSync(pdfPath));
var pdfPath = Path.resolve(__dirname, 'fixtures/pdf/123x456.1.pdf');
var result = Pdf.detect(Fs.readFileSync(pdfPath));
expect(result).to.eql(true);
});

it('should return false for a non-PDF', function () {
var pngPath = path.resolve(__dirname, 'fixtures/png/123x456.png');
var result = pdf.detect(fs.readFileSync(pngPath));
var pngPath = Path.resolve(__dirname, 'fixtures/png/123x456.png');
var result = Pdf.detect(Fs.readFileSync(pngPath));
expect(result).to.eql(false);
});

});

describe('measure', function () {

var fixtures = path.resolve(__dirname, 'fixtures/pdf');
var files = fs.readdirSync(fixtures);
var fixtures = Path.resolve(__dirname, 'fixtures/pdf');
var files = Fs.readdirSync(fixtures);

files.forEach(function (file) {
var fileSplit = file.split(/x|\./);
Expand All @@ -40,10 +41,24 @@ describe('pdf', function () {
expectedOutput.pages[i] = { width: width, height: height };
}

it('should return the correct dimensions for ' + file, function () {
var pdfPath = path.resolve(fixtures, file);
return pdf.measure(pdfPath)
.bind({})
it('should return the correct dimensions for a path to ' + file, function () {
var pdfPath = Path.resolve(fixtures, file);

return Pdf.measure(pdfPath)
.then(function (result) {
for (var i = 0; i < result.pages.length; i++) {
result.pages[i].width = Math.round(result.pages[i].width);
result.pages[i].height = Math.round(result.pages[i].height);
}
expect(result).to.eql(expectedOutput);
});
});

it('should return the correct dimensions for a buffer of ' + file, function () {
var pdfPath = Path.resolve(fixtures, file);
var buffer = Fs.readFileSync(pdfPath);

return Pdf.measure(buffer)
.then(function (result) {
for (var i = 0; i < result.pages.length; i++) {
result.pages[i].width = Math.round(result.pages[i].width);
Expand All @@ -52,16 +67,29 @@ describe('pdf', function () {
expect(result).to.eql(expectedOutput);
});
});

});

it('should error with a path to a corrupt PDF', function () {
var pdfPath = Path.resolve(__dirname, 'fixtures/corrupt/corrupt.pdf');
return expect(Pdf.measure(pdfPath)).to.be.rejectedWith(Error);
});

it('should error with a buffer of a corrupt PDF', function () {
var pdfPath = Path.resolve(__dirname, 'fixtures/corrupt/corrupt.pdf');
var buffer = Fs.readFileSync(pdfPath);
return expect(Pdf.measure(buffer)).to.be.rejectedWith(Error);
});

it('should error with a corrupt PDF', function () {
var pdfPath = path.resolve(__dirname, 'fixtures/corrupt/corrupt.pdf');
return expect(pdf.measure(pdfPath)).to.be.rejectedWith(Error);
it('should error with a path to a PDF with no pages', function () {
var pdfPath = Path.resolve(__dirname, 'fixtures/corrupt/no_pages.pdf');
return expect(Pdf.measure(pdfPath)).to.be.rejectedWith(Error);
});

it('should error with a PDF with no pages', function () {
var pdfPath = path.resolve(__dirname, 'fixtures/corrupt/no_pages.pdf');
return expect(pdf.measure(pdfPath)).to.be.rejectedWith(Error);
it('should error with a buffer of a PDF with no pages', function () {
var pdfPath = Path.resolve(__dirname, 'fixtures/corrupt/no_pages.pdf');
var buffer = Fs.readFileSync(pdfPath);
return expect(Pdf.measure(buffer)).to.be.rejectedWith(Error);
});

});
Expand Down
6 changes: 4 additions & 2 deletions test/setup.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

var chai = require('chai');
var Chai = require('chai');

chai.use(require('chai-as-promised'));
Chai.use(require('chai-as-promised'));

global.expect = Chai.expect;

0 comments on commit 22cbf39

Please sign in to comment.