Restrict number of pages on a PDF shown in browser Node server

83 Views Asked by At

I am developing a platform with model preview before paying to download for pdf's my problem is how to restrict the number of pages of pdf showing to the user.

eg. I use pdfjs to render the pdf on canvas but when I see the network request the full file is downloadable.

I tried with hummus js to split on server & display to the client, here my confusion is firstly I put the path on sql db & store pdfs on fs so the response is JSON, so how to handle that? Secondly how to make it dynamic, since hummusJs needs an input and output name and I am working with a lot of pdf's.

There are related questions on Stack Overflow but they are all for PHP.

Here is my try I read the pdf uploaded by user's and write it with hummusjs then send it to the browser but this is not scalable for many files

app.get('/', (req, res) => {
    //hummusjs
    let readPdf = hum.createReader('./uploads/quote.pdf-1675405130171.pdf');
    let pageCount = readPdf.getPagesCount();
    writePdf = hum.createWriter('preview.pdf');
    writePdf
        .createPDFCopyingContext(readPdf)
        .appendPDFPageFromPDF(0);
    writePdf.end()


    const path = './preview.pdf';
    if (fs.existsSync(path)) {
        res.contentType("application/pdf");
        fs.createReadStream(path).pipe(res)
    } else {
        res.status(500)
        console.log('File not found')
        res.send('File not found')
    }

});

Restrict number of pages on a PDF shown in browser Node server, Users can't able to download it in no way without the preview file.

!(Inspect, Print & Network tab) || Pay & get full document

0

There are 0 best solutions below