1

I tried pdf2json:

const PDFParser = require("pdf2json");
let pdfParser = new PDFParser();

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

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

  console.log(`Height : ${height}`) // logs 70.866
  console.log(`Width : ${width}`) // logs 53.15
});

But it gave the dimensions in a unknown units!

The dimensions in pixels will help me include them in the pdf-poppler module that converts a pdf file to an image and it needs the pdf file height in pixels.

DINO
  • 191
  • 1
  • 12

2 Answers2

2

Try calipers. Code example:

const Calipers = require('calipers')('png', 'pdf');
Calipers.measure('./30x40.pdf')
.then(data => {
  const { width, height } = data.pages[0];
});

Alternatively, try a module which converts it without needing width/height: pdf2pic pdf-image node-imagemagick If you're set on using pdf2json, please read this bit of documentation describing the units of the output.

PiggyPlex
  • 631
  • 1
  • 4
  • 15
  • pdf2pic can convert pdf to image, but default size is landscape (for all pdf sizes), If we need an larger image with pdf ratio, we need to get the pdf real width + height, then set it into pdf2pic convert options – Van Tho Sep 05 '22 at 03:35
2

Bit late to the party, but as discussed here: stackoverflow pdf2json unit you can multiply your width and height by 24 Just like:

width = pdfData.formImage.Width * 24; // pdf width
height = pdfData.formImage.Pages[0].Height * 24; // page height

and you get Pixel.

Henrik
  • 53
  • 6