Skip to Content
drksci
System

PDF rendering

How the résumé and any page become a PDF, and the download route that is kept here as reference because the Worker cannot run it.

A PDF is the page itself, printed by a real browser. The compositor in src/lib/pdf/compose.ts does the work. It was ported from the earlier site's serverRender.ts, on Playwright.

How a part becomes a page

The browser opens the page at a fixed desktop width, so the type scale reaches its desktop size whatever the reader's window was. Everything but one part is hidden, the part is measured, and it is printed as a single page cut to that height, full bleed. The parts are then joined into one file with pdf-lib.

Waiting for fonts and images is bounded. A request that hangs costs a little accuracy in the measurement, not the whole export. Network idle is preferred and never required, because a dev server's socket keeps a connection open for good.

Commands

bun run gen:pdf --web --default            # the résumé, the website's own download
bun run gen:article-pdf https://drksci.com/<slug> --out piece.pdf

Download route

The earlier site served the file from /api/resume-pdf. That route is not live here. It needs a headless Chromium inside the request, and the Worker cannot start one. Running it takes a Node host, or Cloudflare Browser Rendering behind a binding. The code is kept here so it can be dropped in as src/app/api/resume-pdf/route.ts when one of those exists.

Two rules carry over from the original. The target is always this deployment's own /blake, taken from the request and never from a query parameter, so the route cannot be turned into a way to fetch an arbitrary address. A variant is the owner's preview, so rendering one needs the owner's session.

import { chromium } from "playwright-core";
import { signedInAs } from "@/auth/server";
import { hideChrome, renderArticlePdf } from "@/lib/pdf/compose";

export const runtime = "nodejs";
export const maxDuration = 60;
export const dynamic = "force-dynamic";

const FILENAME = "resume.pdf";

export async function GET(req: Request) {
  const url = new URL(req.url);
  const variant = url.searchParams.get("variant");
  if (variant && !/^[a-z0-9]+(-[a-z0-9]+)*$/.test(variant))
    return Response.json({ error: "Bad variant" }, { status: 400 });
  if (variant && !(await signedInAs("blake")))
    return Response.json({ error: "Not found" }, { status: 404 });

  const target = `${url.origin}/blake${variant ? `/${variant}` : ""}`;
  const browser = await chromium.launch({
    executablePath: process.env.CHROME_PATH || undefined,
    args: ["--no-sandbox", "--disable-setuid-sandbox"],
  });
  try {
    const context = await browser.newContext();
    if (variant) {
      // the owner's session carries over, so the preview opens for the render
      const cookie = req.headers.get("cookie") ?? "";
      await context.setExtraHTTPHeaders({ cookie });
    }
    const page = await context.newPage();
    const bytes = await renderArticlePdf(page, target);
    return new Response(new Uint8Array(bytes), {
      headers: {
        "Content-Type": "application/pdf",
        "Content-Disposition": `attachment; filename="${FILENAME}"`,
        "Cache-Control": "no-store",
      },
    });
  } catch (err) {
    console.error("resume-pdf: render failed", err);
    return Response.json({ error: "PDF generation failed" }, { status: 500 });
  } finally {
    await browser.close();
  }
}

For the full résumé with its letter and transcript as separate fitted pages, call the same helpers the --web mode of scripts/gen-pdf.ts uses: showOnly for each part, renderVisibleAsOnePage, then mergePdfs.

The earlier route also signed each outbound link to a share id (trackOutbound, ported) and kept a rendered file in Postgres keyed by a hash of the document. Neither is carried here: there is no /a/ redirect route and no database.

On this page