Fdawgs/node-poppler

How to get the page number in a friendly way

MaXiaoCui11 opened this issue · 4 comments

image
This is the data obtained by pdfInfo function. Can this data be directly processed into JSON and returned?

Hi @MaXiaoCui11, that sounds like a great idea! I will investigate how easy this is to do.

sainf commented

A json flag it would be nice!

My approach :

const camelCase = require('camelcase')
const { Poppler } = require('node-poppler')

/**
 * Converts node-poppler pdfInfo to an object
 * 
 * @param {string} pathTo the full path
 * @returns object with pdf information
 */

const pdfInfo = async pathTo => {
  const poppler = new Poppler('/usr/bin')
  const info = {}
  
  const ret = await poppler.pdfInfo(pathTo)

  ret.split('\n').map(r => r.split(': ')).forEach(r => {
    if (r.length > 1)	info[camelCase(r[0])] = r[1].trim()
  })

  return info
}

module.exports = { pdfInfo }

image

@MaXiaoCui11 see #268, shamelessly stole @sainf's solution.

@sainf are you happy for me to credit you in an in-line comment, and in the changelog?

sainf commented

Happy to be part on an solution! Thanks for the comment!