Howto append existing pdf to a generated?



  • Is it possible to append a uploaded external base64 pdf from POST data?

    To include images is easy, but how can i append complete pdf files?
    I can do it manually in the pdf utils configuration or in the afterRender function, but i have no idea how to append it automatically from the POST data.

    https://playground.jsreport.net/w/fhrtms/9wsUQP9Y



  • I think I'm close...

    async function afterRender(req, res) {
        let pdfBuffer = await downloadPDF('https://web.wpi.edu/Images/CMS/Provost/landscape.pdf')
    
        // THIS WORKS
        res.content = pdfBuffer
    
        // THIS RESULT IN: Error: EOL expected but not found
        //res.content = await jsreport.pdfUtils.append(res.content, pdfBuffer)
    }
    
    const axios = require('axios')
    
    async function downloadPDF(pdfURL) {
        return axios.get(pdfURL, 
            {
                responseType: 'arraybuffer'
            }
        )
        .then(response => Buffer.from(response.data, 'binary'))
    }
    


  • Your code is good, but unfortunately, pdf-utils can't parse all pdf files in all various internal formats which is the case here.

    We are constantly improving this, I have put to our backlog to check this particular pdf. We should get to it soon.
    https://github.com/jsreport/jsreport/issues/1014



  • With npm i ghostscript-node i get a valid PDF.

    const gs = require("ghostscript-node");
    
    async function afterRender(req, res) {
    
        // load PDF as Buffer
        const pdf1 = await downloadPDF('https://www.aubrete.lt/wp-content/uploads/2019/06/1-COVERSTYLE-1.pdf')
    
        // get number of pages of pdf1
        const numPagesPDF1 = await gs.countPDFPages(pdf1);
    
        // get Buffer object with bytes of specified set of pages
        // page numbers begin with 1, last page is included
        const partOfPDF1 = await gs.extractPDFPages(pdf1, 1, numPagesPDF1);
    
        // checks if pdf1 is a valid PDF file
        const isPDF1Valid = await gs.isValidPDF(pdf1);
        
        if(isPDF1Valid) {
            res.content = await jsreport.pdfUtils.append(res.content, partOfPDF1)
        }
    }
    

Log in to reply
 

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