Last active
July 14, 2024 08:07
-
-
Save daliborgogic/1bfa57ce0e0f3cdcd75bf8998442f775 to your computer and use it in GitHub Desktop.
Pdfmake on Cloudflare Workers. Total Upload: 2324.17 KiB / gzip: 983.62 KiB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"type": "module", | |
"scripts": { | |
"dev": "wrangler dev --port 3000 worker.mjs", | |
"deploy": "wrangler deploy" | |
}, | |
"devDependencies": { | |
"pdfmake": "^0.3.0-beta.8", | |
"wrangler": "^3.63.1" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pdfmake from 'pdfmake' | |
import pdfFonts from 'pdfmake/build/vfs_fonts' | |
pdfmake.vfs = pdfFonts | |
export default { | |
fetch: async request => { | |
const docDefinition = await request.json() | |
const pdf = pdfmake.createPdf(docDefinition) | |
const base64 = await pdf.getBase64() | |
const binStr = atob(base64) | |
const len = binStr.length | |
const arr = new Uint8Array(len) | |
for (let i = 0; i < len; i++) { | |
arr[i] = binStr.charCodeAt(i) | |
} | |
const blob = new Blob([arr], { type: 'application/pdf' }) | |
return new Response(blob) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name = 'pdfmake' | |
main = 'worker.mjs' | |
compatibility_date = "2024-07-01" | |
minify = true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
pdfmake
Virtual File System (VFS)