Skip to content

Instantly share code, notes, and snippets.

@mansarip
Last active April 23, 2024 04:01
Show Gist options
  • Save mansarip/eb11b66e7dc65cee988155275a17a119 to your computer and use it in GitHub Desktop.
Save mansarip/eb11b66e7dc65cee988155275a17a119 to your computer and use it in GitHub Desktop.
How to use PDFKit with Hono + Bun

How to use PDFKit with Hono + Bun

Full example

import { Hono } from "hono";
import PDFDocument from "pdfkit";

const app = new Hono();

app.get("/pdf", async (c) => {
  const doc = new PDFDocument();
  doc.info.Title = "Test Document";
  doc.text("Salam!");
  doc.end();
  c.header("Content-Disposition", 'inline; filename="my-custom-filename.pdf"');
  return c.body(doc);
});

export default app;

You can also use new Response() instead of using context (c) from Hono :

app.get("/pdf", async (c) => {
  const doc = new PDFDocument();
  doc.info.Title = "Test Document";
  doc.text("Salam!");
  doc.end();
  return new Response(doc, {
    headers: {
      "Content-Disposition": 'inline; filename="apakah.pdf"',
    },
  });
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment