Add precheck pdf before return to Client



  • Hello i create a server to generate PDF
    const jsreport = require('jsreport')()

    But sometime PDF generate with error fonts (I try to find problem but still dont know why). So I use pdf-parse to validate if fonts error. But i use it from client. The problem is I have more than 1 client so i want use pdf-parse from server before send PDF to client.

    Is there any way to do that ?



  • The best solution is mostly to a custom script and afterRender hook for the post-processing
    https://jsreport.net/learn/scripts

    Or you can do it also in general, but this will be invoked quite at the end of the pipeline.

    const jsreport = require('jsreport')()
    jsreport.afterRenderListeners.add('someName', async (req, res) => {
        console.log(res.content)
    })
    

    btw, the issue is random right? How do you find out there is an issue in fonts with pdf-parse?



  • yes it happens randomly but not because of pdf-parse but i use pdf-parse to read the text in pdf to confirm that there is no font error i will return it to client and if font error i will output the pdf again As long as there is no font error, that's it.

    [QUESTION]
    jsreport.afterRenderListeners.add('someName', async (req, res) => {
    console.log(res.content)
    })
    how can i use check pdf-parse in this. and when successful, I return the client and if not successful I will generate the PDF again?



  • [QUESTION]
    jsreport.afterRenderListeners.add('someName', async (req, res) => {
    console.log(res.content)
    })
    

    hm, after trying it out, there are too many problems with this solution.

    The custom script could work like this

    async function afterRender (req, res) {    
        // here use extra code to check if res.content is valid
        if (Math.random() > 0.5) {
            // its ok
            return
        }
    
        //retry
        const retryRes = await require('jsreport-proxy').render({...req})
        res.content = retryRes.content
    }
    

    The last option is to use an extra service as middleware that will do the validation and retry.



  • Hello.

    jsreport.afterRenderListeners.add('someName', async (req, res) => {
        console.log("TEST")
        afterRender(req,res)
    })
    async function afterRender (req, res) {    
        console.log(res.content)
    }
    

    0_1667556300473_upload-df85bbfc-1d78-432d-bfc1-57918e30262c
    i push a line console.log("TEST") in this function and it's show 4 times when i generate a report. What happen ? (I use some background, footer, header,... )

    I try

    const jsreport = require('jsreport')()
    
    jsreport.afterRenderListeners.add('someName', async (req, res) => {
        console.log("HAHAHA")
        afterRender(req,res)
    })
    async function afterRender (req, res) {    
        // here use extra code to check if res.content is valid
        if (Math.random() > 0.5) {
            // its ok
            return
        }
    
        //retry
        const retryRes = await require('jsreport-proxy').render({...req})
        res.content = retryRes.content
    }
    if (process.env.JSREPORT_CLI) {
      // export jsreport instance to make it possible to use jsreport-cli
      module.exports = jsreport
    } else {
      jsreport.init().then(() => {
    	console.log("JSreport POWERFUL!")
        // running
      }).catch((e) => {
        // error during startup
        console.error(e.stack)
        process.exit(1)
      })
    }
    

    But it told me that: "Can not find jsreport-proxy module" ?

    Where is the list event listenner i can use ? Example: afterRenderListeners ?

    Can i throw error to client if i detect error font ?



  • But it told me that: "Can not find jsreport-proxy module" ?

    The code I shared should be placed in the custom script entity
    https://jsreport.net/learn/scripts


Log in to reply
 

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