Nodejs:如何使用nodejs获取pdf页面的维度?

时间:2018-02-15 13:22:53

标签: javascript node.js angularjs pdf-generation

如何使用width获取height页面的pdfnodejs

是否也可以采用文件类型?

此处为angularjs的代码:

$scope.demofun = function () {
    var allowedFormats = ['jpeg', 'jpg', 'png', 'pdf'];
    var unsupportedSizes = false;
    if ($scope.file1[0].size < (5 * 1024 * 1024)) {
        //Here I will check the file size and restrict the file if the size is not satisfy my condition.
        //And I need to allow only A4 size pdf when uploading for that I want to get the dimension of pdf document 
        //but I don't able to get the dimension of pdf for that I write the code like this but it will wont works:

        if (($scope.file1[0].width >= 21 && $scope.file1[0].width <= 22) && ($scope.file1.height >= 29.7 && $scope.file1.height <= 30.7)) {
            //...
        }
    }
}

2 个答案:

答案 0 :(得分:4)

有许多软件包可以提供calipersscissors,但我发现pdf2json很简单,并且在其他模块上也不需要。以下是使用pdf2json

获取宽度和高度的方法
PDFParser = require("pdf2json");
let pdfParser = new PDFParser();

pdfParser.loadPDF("./your_file_path"); // ex: ./abc.pdf

pdfParser.on("pdfParser_dataReady", pdfData => {
  width = pdfData.formImage.Width / 4.5; // pdf width
  height = pdfData.formImage.Pages[0].Height / 4.5; // page height

  console.log(`Height : ${height} in inch`)
  console.log(`Width : ${width} in inch`)
});

答案 1 :(得分:1)

请看这个,   我相信你会找到解决问题的方法。

https://www.npmjs.com/package/pdf2json#output-format-reference

此npm包将返回所有必需的详细信息。