SvelteKit
Render Svelte components and their CSS as Open Graph images or PDFs in SvelteKit server routes.
Render a Svelte component to HTML on the server. Then pass that HTML and its CSS to ImageResponse.
Install Takumi
npm i takumi-jsMake a Svelte component
css="injected" so the component's <style> block lands in the server-rendered HTML.<script lang="ts">
let { name = "Goo goo gaga" } = $props();
</script>
<svelte:options css="injected" />
<div
id="og-image"
class="w-full h-full flex items-center justify-center p-12 flex-col gap-4"
>
<div class="text-center whitespace-pre-wrap leading-normal text-7xl font-semibold text-black">
Welcome {name} to
<span class="font-bold">Svelte</span> world
</div>
</div>
<style>
#og-image {
background-image: linear-gradient(to bottom right, var(--color-orange-50), var(--color-red-200));
}
</style>Render the component on a server route
Render the component with render from svelte/server, then return an ImageResponse. If you use Tailwind, import app.css with ?inline and pass it as css.
The example imports through #lib/*. Map it to ./src/lib/* in the imports field of package.json.
import { render } from "svelte/server";
import style from "../app.css?inline";
import ImageResponse from "takumi-js/response";
import OgImage from "#lib/components/OgImage.svelte";
import type { RequestEvent } from "./$types";
export async function GET({ url }: RequestEvent) {
const { body, head } = await render(OgImage, {
props: {
name: url.searchParams.get("name") ?? "Goo goo gaga",
},
});
return new ImageResponse(`${head}${body}`, {
width: 1200,
height: 630,
css: style,
fonts: ["https://takumi.kane.tw/fonts/Geist.woff2"],
});
}head carries the injected <style>, and body carries the markup. fonts takes a font URL, which Takumi fetches on demand.
Render a PDF
The same component HTML renders to a PDF with takumi-pdf. The component needs css="injected" here too, or its styles stay out of head. Install the package, then return its bytes from a server route.
npm i takumi-pdf<script lang="ts">
let { id }: { id: number } = $props();
</script>
<svelte:options css="injected" />
<h1>Invoice {id}</h1>
<style>
h1 { color: #e11d48; }
</style>import { render as renderSvelte } from "svelte/server";
import { render } from "takumi-pdf";
import Invoice from "#lib/components/Invoice.svelte";
export async function GET() {
const { head, body } = await renderSvelte(Invoice, {
props: { id: 1042 },
});
const pdf = await render(`${head}${body}`, { size: "a4" });
return new Response(pdf, {
headers: { "content-type": "application/pdf" },
});
}Content longer than a page flows onto the next one. See Pagination to control the breaks and Headers & footers to repeat page numbers.
Deploy to Cloudflare
@sveltejs/adapter-cloudflare 8.0.3 resolves server code for Cloudflare Workers but leaves out the plugin that loads .wasm imports. Both vite dev and vite build then stop with this error:
Failed to load url ./takumi_wasm_bg.js (resolved id: ./takumi_wasm_bg.js) in .../@takumi-rs/wasm/pkg/takumi_wasm_bg.wasm. Does the file exist?Pin the adapter to 8.0.2 until a later release loads .wasm imports again. Issue #2645 tracks the fix.
npm i -D @sveltejs/adapter-cloudflare@8.0.2Next steps
See ImageResponse for every option, Styles for the CSS Takumi supports, and Fonts for loading them.
Last updated on