Spaces:
Sleeping
Sleeping
fix: use pdf-parse v2 API (PDFParse class, not default export)
Browse files
web/app/api/parse-upload/route.ts
CHANGED
|
@@ -33,14 +33,16 @@ export async function POST(req: NextRequest) {
|
|
| 33 |
if (name.endsWith(".txt") || name.endsWith(".md")) {
|
| 34 |
text = new TextDecoder().decode(buffer);
|
| 35 |
} else if (name.endsWith(".pdf")) {
|
| 36 |
-
//
|
| 37 |
-
// Try the standard pdf-parse import (works with v1.x which is more common)
|
| 38 |
try {
|
| 39 |
-
|
| 40 |
-
const
|
|
|
|
|
|
|
| 41 |
text = result.text;
|
| 42 |
-
|
| 43 |
-
|
|
|
|
| 44 |
return NextResponse.json({
|
| 45 |
error: "PDF parsing failed. Please copy-paste the text directly, or use the Gradio Space which has OCR support."
|
| 46 |
}, { status: 400 });
|
|
|
|
| 33 |
if (name.endsWith(".txt") || name.endsWith(".md")) {
|
| 34 |
text = new TextDecoder().decode(buffer);
|
| 35 |
} else if (name.endsWith(".pdf")) {
|
| 36 |
+
// pdf-parse v2 API: named export PDFParse class + worker import
|
|
|
|
| 37 |
try {
|
| 38 |
+
await import("pdf-parse/worker");
|
| 39 |
+
const { PDFParse } = await import("pdf-parse");
|
| 40 |
+
const parser = new PDFParse({ data: buffer });
|
| 41 |
+
const result = await parser.getText();
|
| 42 |
text = result.text;
|
| 43 |
+
await parser.destroy();
|
| 44 |
+
} catch (pdfErr: any) {
|
| 45 |
+
console.error("PDF parse error:", pdfErr);
|
| 46 |
return NextResponse.json({
|
| 47 |
error: "PDF parsing failed. Please copy-paste the text directly, or use the Gradio Space which has OCR support."
|
| 48 |
}, { status: 400 });
|