Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nodejs : How to take dimension of pdf pages with nodejs?

How to take the width and height of the pdf page using nodejs ?

Is it possible to take the file type also?

Here My code for 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)) {
            //...
        }
    }
}
like image 803
Prakash Avatar asked Sep 13 '25 12:09

Prakash


1 Answers

There are many packages which provides this like calipers and scissors, But i found pdf2json is simple and doesn't require on other modules. Here's how you can get width and height using 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`)
});
like image 60
kgangadhar Avatar answered Sep 15 '25 03:09

kgangadhar