Skip to Content
Takumi

Runtimes & frameworks

Import takumi-pdf on Node.js, Bun, Cloudflare Workers, Next.js, or in the browser, and return a PDF from your framework's server route.

Use the default import on Node.js, Bun, and Cloudflare Workers. It loads the WebAssembly binary itself. For manual initialization, import takumi-pdf/no-init.

A server route returns the bytes with the application/pdf content type. In Next.js, import from takumi-pdf/next instead:

Return a PDF
import { render } from "takumi-pdf";

export async function GET() {
  const pdf = await render("<h1>Invoice 1042</h1>");

  return new Response(pdf, { headers: { "content-type": "application/pdf" } });
}

The shape of the route depends on the framework, and a Worker answers from fetch instead. Each framework guide shows its own: Next.js, SvelteKit, TanStack Start, Astro, and Cloudflare Workers.

Next.js

Import takumi-pdf/next in a Next.js route handler. It works on the Node and Edge runtimes without a serverExternalPackages entry.

app/api/pdf/route.ts
import { render } from "takumi-pdf/next";

In the browser

In a browser bundle, initialize the renderer with takumi-pdf/no-init and takumi-pdf/wasm-url:

Initialize in the browser
import init, { render } from "takumi-pdf/no-init";
import wasmUrl from "takumi-pdf/wasm-url";

await init({ module_or_path: wasmUrl });

Call init() once per page load and await it before the first render. takumi-pdf/no-init exports the same API as the default entry, so render() also takes a React element such as <Invoice />. To save the bytes as a file, wrap them in a Blob:

Download the PDF
import init, { render } from "takumi-pdf/no-init";
import wasmUrl from "takumi-pdf/wasm-url";

const ready = init({ module_or_path: wasmUrl });

export async function downloadInvoice() {
  await ready;

  const pdf = await render("<h1>Invoice 1042</h1>");
  const link = document.createElement("a");

  link.href = URL.createObjectURL(new Blob([pdf], { type: "application/pdf" }));
  link.download = "invoice.pdf";
  link.click();
  setTimeout(() => URL.revokeObjectURL(link.href));
}

takumi-pdf/wasm-url resolves the binary with new URL(specifier, import.meta.url), and Vite, webpack, and Turbopack rewrite that call to the asset they emit. The module has no top-level await, so it also works in a worker bundled as an IIFE.

Keep the default entry in server code. A Vite SSR build leaves the URL pointing at the server chunk, where the asset is not emitted.

On Turbopack, remove any turbopack.rules entry that maps *.wasm to type: "wasm". That rule makes Turbopack instantiate the binary and look for wasm-bindgen glue the package does not ship.

Last updated on