-
Notifications
You must be signed in to change notification settings - Fork 8
/
PageSizes.js
55 lines (48 loc) · 1.74 KB
/
PageSizes.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
const pdfjs = require('pdfjs-dist');
const bereich = require('bereich');
class PageSize {
constructor (width, height) {
this.width = width;
this.height = height;
}
}
function getPageSize (page, ignoreRotation = false) {
const [x, y, w, h] = page.pageInfo.view;
const width = w - x;
const height = h - y;
const rotate = page.pageInfo.rotate;
// Consider rotation
return !ignoreRotation && (rotate === 90 || rotate === 270)
? new PageSize(height, width) : new PageSize(width, height);
}
/**
* Adapted from
* https://techoverflow.net/2018/04/13/extract-pdf-page-sizes-using-pdfjs-nodejs/
* @param {Buffer} buffer A buffer of the PDF
* @param opts ignoreRotation If true, ignore the rotation when computing the page size
* @return Promise of array of PageSize objects
*/
async function pdfPageSizes (buffer, opts = {}) {
const ignoreRotation = opts.ignoreRotation || false;
const pdf = await pdfjs.getDocument({data: buffer});
const numPages = pdf.numPages;
const pageNumbers = Array.from(bereich(1, numPages));
// Start reading all pages 1...numPages
const promises = pageNumbers.map(pageNo => pdf.getPage(pageNo));
// Wait until all pages have been read
const pages = await Promise.all(promises);
// You can do something with pages here.
return pages.map(page => getPageSize(page, ignoreRotation));
}
function convertPtToInch (pt) { return pt / 72; }
function convertInchToMM (inch) { return inch * 25.4; }
function convertPtToMM (pt) {
return convertInchToMM(convertPtToInch(pt));
}
module.exports = {
pdfPageSizes: pdfPageSizes,
PageSize: PageSize,
convertPtToInch: convertPtToInch,
convertInchToMM: convertInchToMM,
convertPtToMM: convertPtToMM
};