@rawwee/react-pdf-html
v2.1.0
Published
Package for rendering @react-pdf/renderer components as HTML components
Readme
react-pdf-html
react-pdf-html gives you drop-in replacements for the @react-pdf/renderer components that render as plain HTML in the browser. Write your PDF template once, show it on the page as HTML for a fast live preview, and hand the same template to react-pdf when you need the real PDF.
Installation
pnpm add @rawwee/react-pdf-html @react-pdf/rendererIt works with @react-pdf/renderer 3 and 4. The test suite runs against react-pdf 4.9 with React 19, and against 3.4 with React 18.
Usage
Import the components from this package instead of @react-pdf/renderer:
import { Document, Page, Text, View } from "@rawwee/react-pdf-html";
export const MyDocument = () => (
<Document>
<Page>
<View>
<Text>Hello, World!</Text>
</View>
</Page>
</Document>
);Rendered anywhere in your app, <MyDocument /> shows as HTML.
Making the PDF
Wrap the document in RenderAsPDF wherever you hand it to react-pdf. Everything inside renders as react-pdf components, while the HTML preview elsewhere on the page keeps working:
import { PDFDownloadLink, pdf } from "@react-pdf/renderer";
import { RenderAsPDF } from "@rawwee/react-pdf-html";
const pdfDocument = (
<RenderAsPDF>
<MyDocument />
</RenderAsPDF>
);
// A download link
<PDFDownloadLink document={pdfDocument} fileName="document.pdf">
Download
</PDFDownloadLink>;
// Or a Blob
const blob = await pdf(pdfDocument).toBlob();To branch inside your own components, read the mode with usePDFComponentsAreHTML(). Its isHTML is false inside RenderAsPDF.
usePDFComponentsAreHTML().setHtml(false) still switches every component to PDF mode at once, for code written before RenderAsPDF existed.
Components
Every react-pdf component: Document, Page, View, Text, Link, Image, Note and Canvas, and the SVG components Svg, G, Path, Rect, Line, Polyline, Polygon, Circle, Ellipse, Tspan, Defs, ClipPath, LinearGradient, RadialGradient and Stop.
These need react-pdf 4: ImageBackground, Marker and the form fields FieldSet, TextInput, Checkbox, Select and List.
In HTML:
Pagehas itssizeandorientation, A4 by default.renderprops and Pagelayoutget the page's number and the page count.Canvasruns yourpaintfunction on an HTML canvas.TextinsideSvgrenders as SVG text.- Form fields become native inputs.
Noterenders nothing.
Differences from the PDF
- HTML has no page breaks. A page grows past its height instead of moving content to the next page, so each
Pagecounts as one page. fixedand other pagination props have no effect in HTML.Canvasgradients draw nothing in either output, because react-pdf's painter does not return them.- In react-pdf 4, a unitless
lineHeighton an element without its ownfontSizeis multiplied by 18, not by the inherited font size. SetfontSizenext to it so the preview and the PDF match.
Development
pnpm install
pnpm test
pnpm typecheck
pnpm buildUsed by
Contributing
Contributions are welcome! If you find a bug or want to add a new feature, please open an issue or submit a pull request on GitHub.
License
react-pdf-html is licensed under the MIT License.
