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);
}