From 68cde5105f7143012637ad97e9823224d4852bbf Mon Sep 17 00:00:00 2001 From: Axmin Shrestha <56112475+ax-sh@users.noreply.github.com> Date: Tue, 3 Dec 2024 05:43:48 +0545 Subject: [PATCH] feat(pdf): add pdfResponse function for PDF downloads --- api/core/core.ts | 13 ++++++++++++- package.json | 1 + puppet.spec.ts | 8 ++++---- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/api/core/core.ts b/api/core/core.ts index a6ba6a4..77d1874 100644 --- a/api/core/core.ts +++ b/api/core/core.ts @@ -8,4 +8,15 @@ export async function downloadPDF() { } export const cloud = require("chrome-aws-lambda"); -export const desc = "Api for pdf generation"; + +export function pdfResponse(pdf: Buffer, fileName: string) { + const response = { + headers: { + "Content-type": "application/pdf", + "content-disposition": `attachment; filename=${fileName}.pdf`, + }, + statusCode: 200, + body: pdf.toString("base64"), + isBase64Encoded: true, + }; +} diff --git a/package.json b/package.json index 06550f8..8d01bee 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,7 @@ "scripts": { "dev": "ntl dev", "format": "nr biome check --write", + "pretty": "nr biome format --write", "test": "bun test api --watch", "test:puppet": "nlx playwright test --headed puppet.spec.ts" }, diff --git a/puppet.spec.ts b/puppet.spec.ts index c067d78..a5684b0 100644 --- a/puppet.spec.ts +++ b/puppet.spec.ts @@ -25,9 +25,9 @@ test.describe("Playwright Browser Testing", () => { expect(exists).toBe(true); }); test("should download a pdf", async ({ page }) => { - let url:string; - url = "https://example.com" - url = 'https://ax-sh.github.io/' + let url: string; + url = "https://example.com"; + url = "https://ax-sh.github.io/"; await page.goto(url); const pdfBuffer = await page.pdf({ format: "A4", @@ -40,7 +40,7 @@ test.describe("Playwright Browser Testing", () => { const uint8Array = new Uint8Array(pdfBuffer); const fs = await import("node:fs"); - const pdfLocalFilePath = "output.pdf" + const pdfLocalFilePath = "output.pdf"; fs.writeFile(pdfLocalFilePath, uint8Array, (err) => { if (err) { console.error("Error writing the file:", err);