mirror of
https://github.com/theoleuthardt/werkzeugkiste.git
synced 2026-06-13 09:37:53 +00:00
feat: convert docs to pdf logic with api endpoint and dockerfile dependency
This commit is contained in:
parent
4f119ca750
commit
40032dca69
6 changed files with 124 additions and 23 deletions
43
src/app/api/libre-convert/route.ts
Normal file
43
src/app/api/libre-convert/route.ts
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
"use strict";
|
||||
import libre from "libreoffice-convert";
|
||||
import { promisify } from "util";
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
|
||||
const libreConvertAsync = promisify(libre.convert);
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
if (request.method !== "POST") {
|
||||
return NextResponse.json({ error: "Method not allowed" }, { status: 405 });
|
||||
}
|
||||
|
||||
try {
|
||||
const formData = await request.formData();
|
||||
const file = formData.get("file") as Blob;
|
||||
|
||||
if (!file) {
|
||||
return NextResponse.json({ error: "No file uploaded!" }, { status: 400 });
|
||||
}
|
||||
|
||||
const arrayBuffer = await file.arrayBuffer();
|
||||
const fileBuffer = Buffer.from(arrayBuffer);
|
||||
const pdfBuffer = await libreConvertAsync(
|
||||
fileBuffer,
|
||||
".pdf",
|
||||
"writer_pdf_Export",
|
||||
);
|
||||
|
||||
return new Response(pdfBuffer, {
|
||||
status: 200,
|
||||
headers: {
|
||||
"Content-Type": "application/pdf",
|
||||
"Content-Disposition": "attachment; filename=converted.pdf",
|
||||
},
|
||||
});
|
||||
} catch (error: unknown) {
|
||||
console.error("Convert error: ", error);
|
||||
return NextResponse.json(
|
||||
{ error: "Error while converting!" },
|
||||
{ status: 500 },
|
||||
);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue