From a7fb9d6478ea46fbc58b0e4a0909c1ec202f598d Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Wed, 12 Jul 2023 15:39:30 +0100 Subject: [PATCH 1/7] feat: generate pdf function --- generate-pdf/.gitignore | 130 +++++++++++++++++++++++++++++++++ generate-pdf/.prettierrc.json | 6 ++ generate-pdf/README.md | 13 ++++ generate-pdf/package-lock.json | 87 ++++++++++++++++++++++ generate-pdf/package.json | 19 +++++ generate-pdf/src/main.js | 70 ++++++++++++++++++ 6 files changed, 325 insertions(+) create mode 100644 generate-pdf/.gitignore create mode 100644 generate-pdf/.prettierrc.json create mode 100644 generate-pdf/README.md create mode 100644 generate-pdf/package-lock.json create mode 100644 generate-pdf/package.json create mode 100644 generate-pdf/src/main.js diff --git a/generate-pdf/.gitignore b/generate-pdf/.gitignore new file mode 100644 index 00000000..6a7d6d8e --- /dev/null +++ b/generate-pdf/.gitignore @@ -0,0 +1,130 @@ +# Logs +logs +*.log +npm-debug.log* +yarn-debug.log* +yarn-error.log* +lerna-debug.log* +.pnpm-debug.log* + +# Diagnostic reports (https://nodejs.org/api/report.html) +report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json + +# Runtime data +pids +*.pid +*.seed +*.pid.lock + +# Directory for instrumented libs generated by jscoverage/JSCover +lib-cov + +# Coverage directory used by tools like istanbul +coverage +*.lcov + +# nyc test coverage +.nyc_output + +# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) +.grunt + +# Bower dependency directory (https://bower.io/) +bower_components + +# node-waf configuration +.lock-wscript + +# Compiled binary addons (https://nodejs.org/api/addons.html) +build/Release + +# Dependency directories +node_modules/ +jspm_packages/ + +# Snowpack dependency directory (https://snowpack.dev/) +web_modules/ + +# TypeScript cache +*.tsbuildinfo + +# Optional npm cache directory +.npm + +# Optional eslint cache +.eslintcache + +# Optional stylelint cache +.stylelintcache + +# Microbundle cache +.rpt2_cache/ +.rts2_cache_cjs/ +.rts2_cache_es/ +.rts2_cache_umd/ + +# Optional REPL history +.node_repl_history + +# Output of 'npm pack' +*.tgz + +# Yarn Integrity file +.yarn-integrity + +# dotenv environment variable files +.env +.env.development.local +.env.test.local +.env.production.local +.env.local + +# parcel-bundler cache (https://parceljs.org/) +.cache +.parcel-cache + +# Next.js build output +.next +out + +# Nuxt.js build / generate output +.nuxt +dist + +# Gatsby files +.cache/ +# Comment in the public line in if your project uses Gatsby and not Next.js +# https://nextjs.org/blog/next-9-1#public-directory-support +# public + +# vuepress build output +.vuepress/dist + +# vuepress v2.x temp and cache directory +.temp +.cache + +# Docusaurus cache and generated files +.docusaurus + +# Serverless directories +.serverless/ + +# FuseBox cache +.fusebox/ + +# DynamoDB Local files +.dynamodb/ + +# TernJS port file +.tern-port + +# Stores VSCode versions used for testing VSCode extensions +.vscode-test + +# yarn v2 +.yarn/cache +.yarn/unplugged +.yarn/build-state.yml +.yarn/install-state.gz +.pnp.* \ No newline at end of file diff --git a/generate-pdf/.prettierrc.json b/generate-pdf/.prettierrc.json new file mode 100644 index 00000000..fa51da29 --- /dev/null +++ b/generate-pdf/.prettierrc.json @@ -0,0 +1,6 @@ +{ + "trailingComma": "es5", + "tabWidth": 2, + "semi": false, + "singleQuote": true +} diff --git a/generate-pdf/README.md b/generate-pdf/README.md new file mode 100644 index 00000000..76a3183a --- /dev/null +++ b/generate-pdf/README.md @@ -0,0 +1,13 @@ +# PDF Generator Function + +This function allows you to generate a PDF document, specifically tailored towards creating an invoice. The function generates random order information and converts it into a structured PDF invoice. + +## Usage + +This function doesn't expect any parameters and only supports one type of request: + +1. **Generating a PDF Invoice** + + - **Request Type:** GET + - **Response:** + - On success, the function will respond with a binary stream of the generated PDF document. The `Content-Type` of the response will be set as `application/pdf`. diff --git a/generate-pdf/package-lock.json b/generate-pdf/package-lock.json new file mode 100644 index 00000000..0d2f8aad --- /dev/null +++ b/generate-pdf/package-lock.json @@ -0,0 +1,87 @@ +{ + "name": "generate-pdf", + "version": "1.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "generate-pdf", + "version": "1.0.0", + "license": "MIT", + "dependencies": { + "@faker-js/faker": "^8.0.2", + "pdf-lib": "^1.17.1" + }, + "devDependencies": { + "prettier": "^3.0.0" + } + }, + "node_modules/@faker-js/faker": { + "version": "8.0.2", + "resolved": "https://registry.npmjs.org/@faker-js/faker/-/faker-8.0.2.tgz", + "integrity": "sha512-Uo3pGspElQW91PCvKSIAXoEgAUlRnH29sX2/p89kg7sP1m2PzCufHINd0FhTXQf6DYGiUlVncdSPa2F9wxed2A==", + "funding": [ + { + "type": "opencollective", + "url": "https://opencollective.com/fakerjs" + } + ], + "engines": { + "node": "^14.17.0 || ^16.13.0 || >=18.0.0", + "npm": ">=6.14.13" + } + }, + "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/pako": { + "version": "1.0.11", + "resolved": "https://registry.npmjs.org/pako/-/pako-1.0.11.tgz", + "integrity": "sha512-4hLB8Py4zZce5s4yd9XzopqwVv/yGNhV1Bl8NTmCq1763HeK2+EwVTv+leGeL13Dnh2wfbqowVPXCIO0z4taYw==" + }, + "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/prettier": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-3.0.0.tgz", + "integrity": "sha512-zBf5eHpwHOGPC47h0zrPyNn+eAEIdEzfywMoYn2XPi0P44Zp0tSq64rq0xAREh4auw2cJZHo9QUob+NqCQky4g==", + "dev": true, + "bin": { + "prettier": "bin/prettier.cjs" + }, + "engines": { + "node": ">=14" + }, + "funding": { + "url": "https://github.com/prettier/prettier?sponsor=1" + } + }, + "node_modules/tslib": { + "version": "1.14.1", + "resolved": "https://registry.npmjs.org/tslib/-/tslib-1.14.1.tgz", + "integrity": "sha512-Xni35NKzjgMrwevysHTCArtLDpPvye8zV/0E4EyYn43P7/7qvQwPh9BGkHewbMulVntbigmcT7rdX3BNo9wRJg==" + } + } +} diff --git a/generate-pdf/package.json b/generate-pdf/package.json new file mode 100644 index 00000000..de6b8773 --- /dev/null +++ b/generate-pdf/package.json @@ -0,0 +1,19 @@ +{ + "name": "generate-pdf", + "version": "1.0.0", + "description": "", + "main": "src/main.js", + "type": "module", + "scripts": { + "format": "prettier --write src/**/*.js" + }, + "author": "", + "license": "MIT", + "dependencies": { + "@faker-js/faker": "^8.0.2", + "pdf-lib": "^1.17.1" + }, + "devDependencies": { + "prettier": "^3.0.0" + } +} diff --git a/generate-pdf/src/main.js b/generate-pdf/src/main.js new file mode 100644 index 00000000..c6d34ccc --- /dev/null +++ b/generate-pdf/src/main.js @@ -0,0 +1,70 @@ +import { PDFDocument } from 'pdf-lib' +import { faker } from '@faker-js/faker' + +export default async ({ res, log }) => { + const fakeOrder = generateFakeOrder() + log(`Generated fake order: ${JSON.stringify(fakeOrder, null, 2)}`) + + const pdfBuffer = await createPdf(fakeOrder) + log('PDF created.') + + return res.send(pdfBuffer, 200, { 'Content-Type': 'application/pdf' }) +} + +function generateFakeOrder() { + const items = Array.from( + { length: faker.number.int({ min: 1, max: 5 }) }, + () => ({ + description: faker.commerce.productName(), + quantity: faker.number.int({ min: 1, max: 10 }), + cost: faker.commerce.price(), + }) + ) + + return { + id: faker.string.uuid(), + date: faker.date.past(), + name: faker.person.fullName(), + items, + total: items.reduce((acc, { cost }) => acc + parseFloat(cost), 0), + } +} + +async function createPdf({ id, date, name, items, total }) { + const document = await PDFDocument.create() + const page = document.addPage([595.28, 841.89]) // A4 size + + page.drawText('Sample Invoice', { x: 50, y: 750, size: 20 }) + page.drawText(new Date(date).toLocaleDateString(), { + x: 400, + y: 750, + size: 15, + }) + + page.drawText(`Hello, ${name}!`, { + x: 50, + y: 700, + size: 30, + }) + + page.drawText(`Order ID: ${id}`, { + x: 50, + y: 650, + size: 10, + }) + + page.drawText(`Total: $${total}`, { x: 50, y: 600, size: 15 }) + + const orderList = items + .map( + ({ description, quantity, cost }) => + `${description} x ${quantity} = $${cost}` + ) + .join('\n') + + page.drawText(orderList, { x: 50, y: 550, size: 15 }) + + const pdfBytes = await document.save() + + return Buffer.from(pdfBytes.buffer) +} From 6ef0ae853b6deaebfc0634eb89589b238026b077 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Wed, 12 Jul 2023 15:41:47 +0100 Subject: [PATCH 2/7] docs: update readme --- generate-pdf/README.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/generate-pdf/README.md b/generate-pdf/README.md index 76a3183a..2a18fd0c 100644 --- a/generate-pdf/README.md +++ b/generate-pdf/README.md @@ -1,13 +1,12 @@ -# PDF Generator Function +# Generate PDF -This function allows you to generate a PDF document, specifically tailored towards creating an invoice. The function generates random order information and converts it into a structured PDF invoice. +This function demonstrates using functions to generate a PDF document. The function generates random order information and converts it into a structured PDF invoice. ## Usage This function doesn't expect any parameters and only supports one type of request: 1. **Generating a PDF Invoice** - - **Request Type:** GET - **Response:** - On success, the function will respond with a binary stream of the generated PDF document. The `Content-Type` of the response will be set as `application/pdf`. From 12821d6aefd3ae2a7bc3bbfe0c75c7dad8f75f3d Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Wed, 19 Jul 2023 11:01:16 +0100 Subject: [PATCH 3/7] chore: add semis, remove pjson extras --- generate-pdf/.prettierrc.json | 2 +- generate-pdf/package.json | 2 -- generate-pdf/src/main.js | 42 +++++++++++++++++------------------ 3 files changed, 22 insertions(+), 24 deletions(-) diff --git a/generate-pdf/.prettierrc.json b/generate-pdf/.prettierrc.json index fa51da29..0a725205 100644 --- a/generate-pdf/.prettierrc.json +++ b/generate-pdf/.prettierrc.json @@ -1,6 +1,6 @@ { "trailingComma": "es5", "tabWidth": 2, - "semi": false, + "semi": true, "singleQuote": true } diff --git a/generate-pdf/package.json b/generate-pdf/package.json index de6b8773..5ee5cd7c 100644 --- a/generate-pdf/package.json +++ b/generate-pdf/package.json @@ -7,8 +7,6 @@ "scripts": { "format": "prettier --write src/**/*.js" }, - "author": "", - "license": "MIT", "dependencies": { "@faker-js/faker": "^8.0.2", "pdf-lib": "^1.17.1" diff --git a/generate-pdf/src/main.js b/generate-pdf/src/main.js index c6d34ccc..722d8fe6 100644 --- a/generate-pdf/src/main.js +++ b/generate-pdf/src/main.js @@ -1,15 +1,15 @@ -import { PDFDocument } from 'pdf-lib' -import { faker } from '@faker-js/faker' +import { PDFDocument } from 'pdf-lib'; +import { faker } from '@faker-js/faker'; export default async ({ res, log }) => { - const fakeOrder = generateFakeOrder() - log(`Generated fake order: ${JSON.stringify(fakeOrder, null, 2)}`) + const fakeOrder = generateFakeOrder(); + log(`Generated fake order: ${JSON.stringify(fakeOrder, null, 2)}`); - const pdfBuffer = await createPdf(fakeOrder) - log('PDF created.') + const pdfBuffer = await createPdf(fakeOrder); + log('PDF created.'); - return res.send(pdfBuffer, 200, { 'Content-Type': 'application/pdf' }) -} + return res.send(pdfBuffer, 200, { 'Content-Type': 'application/pdf' }); +}; function generateFakeOrder() { const items = Array.from( @@ -19,7 +19,7 @@ function generateFakeOrder() { quantity: faker.number.int({ min: 1, max: 10 }), cost: faker.commerce.price(), }) - ) + ); return { id: faker.string.uuid(), @@ -27,44 +27,44 @@ function generateFakeOrder() { name: faker.person.fullName(), items, total: items.reduce((acc, { cost }) => acc + parseFloat(cost), 0), - } + }; } async function createPdf({ id, date, name, items, total }) { - const document = await PDFDocument.create() - const page = document.addPage([595.28, 841.89]) // A4 size + const document = await PDFDocument.create(); + const page = document.addPage([595.28, 841.89]); // A4 size - page.drawText('Sample Invoice', { x: 50, y: 750, size: 20 }) + page.drawText('Sample Invoice', { x: 50, y: 750, size: 20 }); page.drawText(new Date(date).toLocaleDateString(), { x: 400, y: 750, size: 15, - }) + }); page.drawText(`Hello, ${name}!`, { x: 50, y: 700, size: 30, - }) + }); page.drawText(`Order ID: ${id}`, { x: 50, y: 650, size: 10, - }) + }); - page.drawText(`Total: $${total}`, { x: 50, y: 600, size: 15 }) + page.drawText(`Total: $${total}`, { x: 50, y: 600, size: 15 }); const orderList = items .map( ({ description, quantity, cost }) => `${description} x ${quantity} = $${cost}` ) - .join('\n') + .join('\n'); - page.drawText(orderList, { x: 50, y: 550, size: 15 }) + page.drawText(orderList, { x: 50, y: 550, size: 15 }); - const pdfBytes = await document.save() + const pdfBytes = await document.save(); - return Buffer.from(pdfBytes.buffer) + return Buffer.from(pdfBytes.buffer); } From fabead8098ccc49bd2cceef5c01a9277d5cbcfcf Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Thu, 20 Jul 2023 10:36:44 +0100 Subject: [PATCH 4/7] chore: new structure --- {generate-pdf => node/generate-pdf}/.gitignore | 0 {generate-pdf => node/generate-pdf}/.prettierrc.json | 0 {generate-pdf => node/generate-pdf}/README.md | 0 {generate-pdf => node/generate-pdf}/package-lock.json | 0 {generate-pdf => node/generate-pdf}/package.json | 0 {generate-pdf => node/generate-pdf}/src/main.js | 0 6 files changed, 0 insertions(+), 0 deletions(-) rename {generate-pdf => node/generate-pdf}/.gitignore (100%) rename {generate-pdf => node/generate-pdf}/.prettierrc.json (100%) rename {generate-pdf => node/generate-pdf}/README.md (100%) rename {generate-pdf => node/generate-pdf}/package-lock.json (100%) rename {generate-pdf => node/generate-pdf}/package.json (100%) rename {generate-pdf => node/generate-pdf}/src/main.js (100%) diff --git a/generate-pdf/.gitignore b/node/generate-pdf/.gitignore similarity index 100% rename from generate-pdf/.gitignore rename to node/generate-pdf/.gitignore diff --git a/generate-pdf/.prettierrc.json b/node/generate-pdf/.prettierrc.json similarity index 100% rename from generate-pdf/.prettierrc.json rename to node/generate-pdf/.prettierrc.json diff --git a/generate-pdf/README.md b/node/generate-pdf/README.md similarity index 100% rename from generate-pdf/README.md rename to node/generate-pdf/README.md diff --git a/generate-pdf/package-lock.json b/node/generate-pdf/package-lock.json similarity index 100% rename from generate-pdf/package-lock.json rename to node/generate-pdf/package-lock.json diff --git a/generate-pdf/package.json b/node/generate-pdf/package.json similarity index 100% rename from generate-pdf/package.json rename to node/generate-pdf/package.json diff --git a/generate-pdf/src/main.js b/node/generate-pdf/src/main.js similarity index 100% rename from generate-pdf/src/main.js rename to node/generate-pdf/src/main.js From b1850743db3312de8bbd1538fd06f0e448da2af4 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Thu, 20 Jul 2023 12:06:33 +0100 Subject: [PATCH 5/7] chore: prettier script --- node/generate-pdf/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/node/generate-pdf/package.json b/node/generate-pdf/package.json index 5ee5cd7c..a63d8f96 100644 --- a/node/generate-pdf/package.json +++ b/node/generate-pdf/package.json @@ -5,7 +5,7 @@ "main": "src/main.js", "type": "module", "scripts": { - "format": "prettier --write src/**/*.js" + "format": "prettier --write ." }, "dependencies": { "@faker-js/faker": "^8.0.2", From 8ff9fc4362444101ea37f076ae541722c8869d05 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Mon, 24 Jul 2023 16:31:03 +0100 Subject: [PATCH 6/7] docs: update to template --- node/generate-pdf/README.md | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/node/generate-pdf/README.md b/node/generate-pdf/README.md index 2a18fd0c..2402c074 100644 --- a/node/generate-pdf/README.md +++ b/node/generate-pdf/README.md @@ -1,12 +1,25 @@ -# Generate PDF +# ⚡ Generate PDF Function -This function demonstrates using functions to generate a PDF document. The function generates random order information and converts it into a structured PDF invoice. +Generates a PDF document containg a fake invoice. -## Usage +## 🧰 Usage -This function doesn't expect any parameters and only supports one type of request: +### `GET` -1. **Generating a PDF Invoice** - - **Request Type:** GET - - **Response:** - - On success, the function will respond with a binary stream of the generated PDF document. The `Content-Type` of the response will be set as `application/pdf`. +Returns a PDF invoice + +**Response** + +Sample `200` Response: + +Returns a binary stream of the generated PDF document. The `Content-Type` of the response will be set as `application/pdf`. + +## ⚙️ Configuration + +| Setting | Value | +| ----------------- | ------------- | +| Runtime | Node (18.0) | +| Entrypoint | `src/main.js` | +| Build Commands | `npm install` | +| Permissions | `any` | +| Timeout (Seconds) | 15 | From 6defada539684e06346359bf4cb2dcbb3a497701 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Mon, 31 Jul 2023 07:42:12 +0000 Subject: [PATCH 7/7] PR review changes --- node/generate-pdf/README.md | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/node/generate-pdf/README.md b/node/generate-pdf/README.md index 2402c074..28ef8eb0 100644 --- a/node/generate-pdf/README.md +++ b/node/generate-pdf/README.md @@ -1,6 +1,6 @@ -# ⚡ Generate PDF Function +# 📄 Node.js Generate PDF Function -Generates a PDF document containg a fake invoice. +Document containing sample invoice in PDF format ## 🧰 Usage @@ -8,6 +8,8 @@ Generates a PDF document containg a fake invoice. Returns a PDF invoice +No parameters required. + **Response** Sample `200` Response: @@ -23,3 +25,7 @@ Returns a binary stream of the generated PDF document. The `Content-Type` of the | Build Commands | `npm install` | | Permissions | `any` | | Timeout (Seconds) | 15 | + +## 🔒 Environment Variables + +No environment variables required. \ No newline at end of file