PDF Reader for nextjs13, user uploaded a file

42 Views Asked by At

I use pdf-parse Library, the general problem is every library needs a 'path' to the file. How I am supposed to send a path, when a user uploads several pdf documents?

In the Documentation it uses:

const fs = require('fs');
const pdf = require('pdf-parse');
 
let dataBuffer = fs.readFileSync('path to PDF file...');

I use here the pdfBuffer. The file is sent correctly to the api, when I log for example file.name I see the filename.

for (let i = 1; i < totalFiles + 1; i++) {
            const file = formData.get(`file${i}`) as unknown as File;
            if (!file) {
                console.log(`File not found at postion file${i}`);
                return NextResponse.json({ success: false });
            }
        
            // Read the content of the PDF file
            const pdfBuffer = Buffer.from(await file.arrayBuffer())

            pdf(pdfBuffer).then(function(data) {
 
                // number of pages
                console.log(data.numpages);
                // number of rendered pages
                console.log(data.numrender);
                // PDF info
                console.log(data.info);
                // PDF metadata
                console.log(data.metadata); 
                // PDF.js version
                // check https://mozilla.github.io/pdf.js/getting_started/
                console.log(data.version);
                // PDF text
                console.log(data.text); 
                    
            });

When I run this code I get an error:

TypeError: a is not a function

I tried many different Libraries, asked on several discord and everyone told my pdf uploading is complicated. I don't know it should be so hard.

0

There are 0 best solutions below