webcapture Get a free key

React to PDF

React does not produce a PDF. It produces HTML. The question is what you do with that HTML next: rasterise it in the browser and wrap the pixels in a PDF, or send it to a print engine and get a document with real text, real page breaks and real print CSS.

The two ways people do this

The client-side route is the popular one: a library renders your component into an HTML-to-canvas pass, then wraps the canvas in a PDF. It works, and it is the wrong tool for anything a person will read on paper. Text becomes pixels: it cannot be selected, searched, copied or read by a screen reader, fonts that fail to load are baked in as fallbacks, and a long page arrives as one tall image sliced across pages.

The server-side route treats React as what it is: a way to produce markup. You render the component to an HTML string, send that string to a real Chromium print engine, and get a genuine PDF. Text stays text. @page, page breaks, margins and counters work, because the thing doing the pagination is a browser's print pipeline.

Three steps

  1. Render the component to a string on the server: renderToStaticMarkup for a plain document, renderToString when you need Suspense boundaries resolved.
  2. Make the styles reachable. A pasted document has no stylesheet unless you inline it or point the engine at a URL it can fetch.
  3. POST the markup with html, or POST the page's url if the component already has a route.
import { renderToStaticMarkup } from "react-dom/server";

const html = renderToStaticMarkup(<Invoice order={order} />);

const res = await fetch("https://webcapture.io/v1/convert", {
  method: "POST",
  headers: {
    Authorization: "Bearer " + process.env.WEBCAPTURE_KEY,
    "Content-Type": "application/json",
  },
  body: JSON.stringify({ html, format: "pdf", pdfPageSize: "A4", margin: "14mm" }),
});

That is a Next.js route handler, a Remix action or a plain Node script with no changes: the call is one POST either way.

The part that actually bites: styles

A component rendered on the server is markup with no stylesheet. Three common cases:

  • Tailwind or a global CSS file. The classes are in the HTML, so the compiled stylesheet must be too. Inline it in a <style> tag, or host it and set baseUrl so the renderer can fetch it.
  • CSS-in-JS. Server rendering usually emits the styles into a separate sheet. Extract it (for styled-components, ServerStyleSheet) and inline the result; component state that only exists in the browser is not in the string.
  • Charts and canvas. A chart that draws after mount is blank in a static render. Either render it to SVG server-side, or point the engine at a real URL and use waitFor with a selector that only appears once the chart has drawn.

Images need to be absolute. A relative src resolves against about:blank unless you send baseUrl, which is why a document with a logo often arrives without one. The HTML to image tool shows the same base-URL rule on the free surface.

When to render from a URL instead

If the document already has a route (an invoice page the customer can open), send url and skip the string entirely: the engine fetches the page, runs its CSS and fonts, waits for your waitFor selector, and prints. The HTML path is for documents that exist only in a request: a report assembled from database rows, a receipt built inside a webhook.

Either way the output is the same engine, and the free URL to PDF tool runs it without a key for public pages. For the print declarations that keep a component layout from collapsing on paper, the formatting guide is the long version.

Frequently asked questions

Is there a React library that makes PDFs directly?

Yes, and it is a different product: libraries in that family build a PDF document from their own component model (View, Text, Page) rather than from HTML and CSS. That is a good fit for generated documents and a poor one for reusing the component you already ship, because none of your CSS applies.

Why is my Tailwind class showing up unstyled in the PDF?

The class is in the markup but the compiled stylesheet is not. Server-rendered React ships HTML only. Inline the built CSS in a style tag, or host it and pass baseUrl so the renderer can fetch it before printing.

Does this work with Next.js server components?

The render has to happen where the API call happens, so the call belongs in a route handler, a server action or a job, not in a component that runs during a page render. The component itself can be a normal one: renderToStaticMarkup does not care where it came from.

Render your first page in seconds

Mint a key, POST a URL, and the finished document comes back in the same response; pass ?wait=false for a job ticket with poll and download URLs. The first 50 renders a month are free, and there is no credit card. Not ready for a key? The free URL to PDF tool renders public pages without one.