Readme
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);
}
👆 This is a val. Vals are TypeScript snippets of code, written in the browser and run on our servers. Create scheduled functions, email yourself, and persist small pieces of data — all from the browser.