-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
22 lines (19 loc) · 711 Bytes
/
index.ts
File metadata and controls
22 lines (19 loc) · 711 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
const port = process.env.PORT || 3000
const server = Bun.serve({
port,
fetch(request) {
const pdfFile = "/tmp/weasyprint-output"
const location = new URL(request.url)
const params = (new URLSearchParams(location.search))
const filename = params.get("name") || "download.pdf"
const url = params.get("url")
if(!url) return new Response("Missing url query param.")
Bun.spawnSync(`weasyprint ${url} ${pdfFile}`.split(" "))
const response = Bun.file(pdfFile).stream()
return new Response(response, { headers: {
"Content-Type": "application/pdf",
"Content-Disposition": `attachment; filename=${filename}`
}})
},
})
console.info(`Listening on ${server.url}`)