diff --git a/app/api/shopping-list/[id]/pdf/route.ts b/app/api/shopping-list/[id]/pdf/route.ts new file mode 100644 index 0000000..0e45a8d --- /dev/null +++ b/app/api/shopping-list/[id]/pdf/route.ts @@ -0,0 +1,176 @@ +import { NextRequest, NextResponse } from "next/server"; +import { getSession } from "@/lib/auth"; +import prisma from "@/lib/prisma"; +import { PDFDocument, rgb } from "pdf-lib"; +import fontkit from "@pdf-lib/fontkit"; +import { promises as fs } from "fs"; + +type Ctx = { + params: Promise<{ id: string }>; +}; + +type ShoppingItem = { name: string; amount: number }; + +function formatDate(d: Date) { + return d.toISOString().slice(0, 10); +} + +export async function GET(_req: NextRequest, context: Ctx) { + const session = await getSession(); + if (!session?.user?.id) { + return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); + } + + const { id } = await context.params; + const listId = Number(id); + + if (!Number.isFinite(listId)) { + return NextResponse.json({ error: "Invalid id" }, { status: 400 }); + } + + const list = await prisma.shoppingList.findUnique({ + where: { id: listId }, + }); + + if (!list || list.userId !== session.user.id) { + return NextResponse.json({ error: "Not found" }, { status: 404 }); + } + + const items = (list.items as ShoppingItem[]) || []; + + const pdfDoc = await PDFDocument.create(); + pdfDoc.registerFontkit(fontkit); + + const fontBytes = await fs.readFile( + process.cwd() + "/public/fonts/Roboto-Regular.ttf", + ); + const page = pdfDoc.addPage(); + const { width } = page.getSize(); + + const font = await pdfDoc.embedFont(fontBytes); + const boldFont = font; + + let y = page.getSize().height - 72; + + const title = "Lista zakupów"; + const titleSize = 20; + const titleWidth = boldFont.widthOfTextAtSize(title, titleSize); + page.drawText(title, { + x: (width - titleWidth) / 2, + y, + size: titleSize, + font: boldFont, + }); + + y -= 30; + + const dateText = `Zakres: ${formatDate(list.startDate)} - ${formatDate( + list.endDate, + )}`; + const dateSize = 12; + const dateWidth = font.widthOfTextAtSize(dateText, dateSize); + page.drawText(dateText, { + x: (width - dateWidth) / 2, + y, + size: dateSize, + font, + }); + + y -= 40; + + if (items.length === 0) { + page.drawText("Brak produktów na liście.", { + x: 72, + y, + size: 12, + font, + }); + } else { + // header + const nameX = 72; + const amountX = width - 72 - 80; + + page.drawText("Produkt", { + x: nameX, + y, + size: 12, + font: boldFont, + }); + + page.drawText("Ilość [g]", { + x: amountX + 40, + y, + size: 12, + font: boldFont, + }); + + y -= 18; + + page.drawLine({ + start: { x: nameX, y: y + 4 }, + end: { x: width - 72, y: y + 4 }, + thickness: 0.5, + }); + + y -= 14; + + const lineHeight = 16; + let rowIndex = 0; + for (const item of items) { + const isEvenRow = rowIndex % 2 === 0; + const textColor = isEvenRow ? rgb(0, 0, 0) : rgb(0.4, 0.4, 0.4); + + if (y < 72) { + const newPage = pdfDoc.addPage(); + y = newPage.getSize().height - 72; + + page.drawText("Produkt", { + x: nameX, + y, + size: 12, + font: boldFont, + color: rgb(0, 0, 0), + }); + page.drawText("Ilość [g]", { + x: amountX, + y, + size: 12, + font: boldFont, + color: rgb(0, 0, 0), + }); + y -= 24; + } + + page.drawText(item.name, { + x: nameX, + y, + size: 12, + font, + color: textColor, + }); + + const amountText = String(item.amount); + const amountWidth = font.widthOfTextAtSize(amountText, 12); + page.drawText(amountText, { + x: amountX + 80 - amountWidth, + y, + size: 12, + font, + color: textColor, + }); + + y -= lineHeight; + rowIndex += 1; + } + } + + const pdfBytes = await pdfDoc.save(); + + return new NextResponse(Buffer.from(pdfBytes), { + status: 200, + headers: { + "Content-Type": "application/pdf", + "Content-Disposition": `attachment; filename="shopping-list-${listId}.pdf"`, + }, + }); +} diff --git a/app/shopping-list/[id]/page.tsx b/app/shopping-list/[id]/page.tsx index db86c12..82c1359 100644 --- a/app/shopping-list/[id]/page.tsx +++ b/app/shopping-list/[id]/page.tsx @@ -41,12 +41,21 @@ export default async function ShoppingListDetailPage({ params }: Props) {

- - ← Wróć - +
+ + ← Wróć + + + + Pobierz PDF + +
diff --git a/package-lock.json b/package-lock.json index d9761b9..f7aba88 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,12 +12,14 @@ "@fullcalendar/interaction": "^6.1.19", "@fullcalendar/react": "^6.1.19", "@fullcalendar/timegrid": "^6.1.19", + "@pdf-lib/fontkit": "^1.1.1", "@prisma/client": "^6.19.0", "bcrypt": "^6.0.0", "dotenv": "^17.2.3", "lucide-react": "^0.556.0", "next": "^16.0.10", "next-auth": "^4.24.13", + "pdf-lib": "^1.17.1", "react": "19.2.0", "react-dom": "19.2.0", "strftime": "^0.10.3", @@ -1883,6 +1885,30 @@ "url": "https://github.com/sponsors/panva" } }, + "node_modules/@pdf-lib/fontkit": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/@pdf-lib/fontkit/-/fontkit-1.1.1.tgz", + "integrity": "sha512-KjMd7grNapIWS/Dm0gvfHEilSyAmeLvrEGVcqLGi0VYebuqqzTbgF29efCx7tvx+IEbG3zQciRSWl3GkUSvjZg==", + "dependencies": { + "pako": "^1.0.6" + } + }, + "node_modules/@pdf-lib/standard-fonts": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/@pdf-lib/standard-fonts/-/standard-fonts-1.0.0.tgz", + "integrity": "sha512-hU30BK9IUN/su0Mn9VdlVKsWBS6GyhVfqjwl1FjZN4TxP6cCw0jP2w7V3Hf5uX7M0AZJ16vey9yE0ny7Sa59ZA==", + "dependencies": { + "pako": "^1.0.6" + } + }, + "node_modules/@pdf-lib/upng": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/@pdf-lib/upng/-/upng-1.0.1.tgz", + "integrity": "sha512-dQK2FUMQtowVP00mtIksrlZhdFXQZPC+taih1q4CvPZ5vqdxR/LKBaFg0oAfzd1GlHZXXSPdQfzQnt+ViGvEIQ==", + "dependencies": { + "pako": "^1.0.10" + } + }, "node_modules/@prisma/client": { "version": "6.19.0", "resolved": "https://registry.npmjs.org/@prisma/client/-/client-6.19.0.tgz", @@ -7396,6 +7422,11 @@ "url": "https://github.com/sponsors/sindresorhus" } }, + "node_modules/pako": { + "version": "1.0.11", + "resolved": "https://registry.npmjs.org/pako/-/pako-1.0.11.tgz", + "integrity": "sha512-4hLB8Py4zZce5s4yd9XzopqwVv/yGNhV1Bl8NTmCq1763HeK2+EwVTv+leGeL13Dnh2wfbqowVPXCIO0z4taYw==" + }, "node_modules/parent-module": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/parent-module/-/parent-module-1.0.1.tgz", @@ -7455,6 +7486,22 @@ "devOptional": true, "license": "MIT" }, + "node_modules/pdf-lib": { + "version": "1.17.1", + "resolved": "https://registry.npmjs.org/pdf-lib/-/pdf-lib-1.17.1.tgz", + "integrity": "sha512-V/mpyJAoTsN4cnP31vc0wfNA1+p20evqqnap0KLoRUN0Yk/p3wN52DOEsL4oBFcLdb76hlpKPtzJIgo67j/XLw==", + "dependencies": { + "@pdf-lib/standard-fonts": "^1.0.0", + "@pdf-lib/upng": "^1.0.1", + "pako": "^1.0.11", + "tslib": "^1.11.1" + } + }, + "node_modules/pdf-lib/node_modules/tslib": { + "version": "1.14.1", + "resolved": "https://registry.npmjs.org/tslib/-/tslib-1.14.1.tgz", + "integrity": "sha512-Xni35NKzjgMrwevysHTCArtLDpPvye8zV/0E4EyYn43P7/7qvQwPh9BGkHewbMulVntbigmcT7rdX3BNo9wRJg==" + }, "node_modules/perfect-debounce": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/perfect-debounce/-/perfect-debounce-1.0.0.tgz", diff --git a/package.json b/package.json index df47aee..444b309 100644 --- a/package.json +++ b/package.json @@ -22,6 +22,8 @@ "lucide-react": "^0.556.0", "next": "^16.0.10", "next-auth": "^4.24.13", + "pdf-lib": "^1.17.1", + "@pdf-lib/fontkit": "^1.1.1", "react": "19.2.0", "react-dom": "19.2.0", "strftime": "^0.10.3", diff --git a/public/fonts/Roboto-Regular.ttf b/public/fonts/Roboto-Regular.ttf new file mode 100644 index 0000000..7e3bb2f Binary files /dev/null and b/public/fonts/Roboto-Regular.ttf differ