0

I have followed this tutorial (https://www.youtube.com/watch?v=enfZAaTRTKU) on youtube which teaches one how to upload a pdf-file a to an express server and read out its content.

I do not want to display the pdf - I only care about the text.

I have followed the tutorial and it all works for me with the express environment but when I try to do the same with the next.js route handlers it just sends me back a 404 as soon as I call pdfParse()

import pdfParse from 'pdf-parse'

import { NextResponse, type NextRequest } from 'next/server';
 
export async function POST(request: NextRequest) {
    const formData = await request.formData();
    const file = formData.get('pdf');
    

    //console.log(file) -- works fine

    // causes the server to send a 404
    const pdfData = await pdfParse(file)

    // it never gets here
    return NextResponse.json({"message": "Error"})
    
}

does anyone know how to deal with this? I would like to avoid having to run a express server just for this.

frankBang
  • 117
  • 1
  • 11

1 Answers1

1

replace the import with the following

import pdf from 'pdf-parse/lib/pdf-parse'

and change pdfParse(file) to pdf(file)

This issue exists because there is a test file in the published library

if (isDebugMode) {
let PDF_FILE = './test/data/05-versions-space.pdf';
let dataBuffer = Fs.readFileSync(PDF_FILE);
Pdf(dataBuffer).then(function(data) {
    Fs.writeFileSync(`${PDF_FILE}.txt`, data.text, {
        encoding: 'utf8',
        flag: 'w'
    });
    debugger;
}).catch(function(err) {
    debugger;
});
}

You can read more about it here