how to get PDF page count when calling api



  • We are generating some PDF bills and would like to see all the ones that are greater than two pages long (i.e. will not fit on front and back of a single sheet). In searching the web, I saw several older mentions that this was returned in the headers when you call the API, but in debugging (either in jsreport studio or in my own code) I don't see that being returned. Is this possible and if so, how? Using v3.11.3 at the moment if that helps.



  • You can use jsreport script to post-process the output pdf and add numbers of pages to the headers.

    const jsreport = require('jsreport-proxy')
    async function afterRender (req, res) {
       const { External } = await jsreport.npm.require('@jsreport/pdfjs@1.4.1')
       const external = new External(res.content)
       res.meta.headers['MY_NUMBER_OF_PAGES'] = external.pages.length
    }
    

    Demo
    https://playground.jsreport.net/w/anon/DADDujdJ



  • follow up question for the answer :

    as pdfjs is already installed package in JSreport why do we need to use jsreport.npm.require ?



  • to avoid breaking changes affecting your script...

    but you don't need to



  • Thank you - seems to work well and was easy!


Log in to reply
 

Looks like your connection to jsreport forum was lost, please wait while we try to reconnect.