Skip to content

Commit

Permalink
Invert the Y coordinate of each item to make PDF.js happy
Browse files Browse the repository at this point in the history
  • Loading branch information
MrOrz committed Jul 13, 2015
1 parent 091c598 commit 01c4dda
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ export default function parse(pdfData, options = {}) {
return;
}

machine.push(word.text, pageIdx + 1, [word.xMin, word.yMin]);
// y coordinate must be inverted, since PDF.js uses bottom-left as (0,0).
machine.push(word.text, pageIdx + 1, [word.xMin, page.height - word.yMin]);
});
}

Expand Down
9 changes: 9 additions & 0 deletions test/unit/indexSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,13 @@ describe('parse', () => {
expect(parsedData[0].items[0].text.slice(0, 4)).to.equal('產業城市');

});

it('should invert Y coordinate of each bounding box', () => {
// PDF.js uses bottom-left corner as (0,0), thus inverting the Y-coord here.

const pdfData = require('../fixture/桃園1040417.json'),
parsedData = parser(pdfData);

expect(parsedData[0].coord[1]).to.equal(842 - 98.279297);
});
});

0 comments on commit 01c4dda

Please sign in to comment.