1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import { Hono } from "npm:hono";
import { PDFDocument, rgb, StandardFonts } from "npm:pdf-lib";
const pdfDoc = await PDFDocument.create();
const timesRomanFont = await pdfDoc.embedFont(StandardFonts.TimesRoman);
const fontSize = 30;
const page = pdfDoc.addPage();
const { width, height } = page.getSize();
page.drawText("Creating PDFs in JavaScript is awesome!", {
x: 50,
y: height - 4 * fontSize,
size: fontSize,
font: timesRomanFont,
color: rgb(0, 0.53, 0.71),
});
const pdfBytes = await pdfDoc.save();
const blob = new Blob([pdfBytes], { type: "application/pdf" });
export function servePDF(req: Request) {
const app = new Hono();
app.get("/", (c) => {
return new Response(blob);
});
app.get("/download", (c) => {
c.header("Content-Type", "application/pdf");
c.header("Content-Disposition", "attachment; filename=cool.pdf");
return c.stream(async (stream) => {
await stream.write(pdfBytes);
});
});
return app.fetch(req);
}
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
November 2, 2023