1

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

Natnael
  • 17
  • 7
  • but [scribd](https://www.scribd.com/document/432590569/Grade-10-2nd-Quarter-Exam-health-reviewer) just achieve that with out any third party tool – Natnael Feb 03 '23 at 12:50
  • i am trying all your suggestion now something come to my mind is there **response middleware** for pdfs that only send specific part of the original file during api response, i think what users see they can download in any way – Natnael Feb 03 '23 at 17:50
  • thanks let's do it,i will write conclusion if it works. – Natnael Feb 03 '23 at 18:08

0 Answers0