Skip to content

Commit

Permalink
feat(pdf): add pdfResponse function for PDF downloads
Browse files Browse the repository at this point in the history
  • Loading branch information
ax-sh committed Dec 2, 2024
1 parent 7cb3331 commit 68cde51
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
13 changes: 12 additions & 1 deletion api/core/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
8 changes: 4 additions & 4 deletions puppet.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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);
Expand Down

0 comments on commit 68cde51

Please sign in to comment.