export const pdfExample = (async (req) => {
const { Hono } = await import("npm:hono");
const app = new Hono();
app.get("/", async (c) => {
return new Response(
`<html><body><h1>This PDF is generated with Val Town:</h1>
<iframe src='/pdf' width="100%" height="500"></iframe></body>
</html>`,
{
headers: {
"Content-Type": "text/html; charset=utf-8",
},
},
);
});
app.get("/pdf", async (c) => {
const { PDFDocument, StandardFonts, rgb } = await import("npm:pdf-lib");
const pdfDoc = await PDFDocument.create();
const timesRomanFont = await pdfDoc.embedFont(StandardFonts.TimesRoman);
const page = pdfDoc.addPage();
const { width, height } = page.getSize();
const fontSize = 30;
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();
return new Response(pdfBytes, {
headers: {
"Content-Type": "application/pdf",
},
});
});
return app.fetch(req);
});