npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-pdfmake-reconciler

v0.0.4

Published

Writing pdfmake with React JSX using React Reconciler.

Downloads

11

Readme

React pdfmake Reconciler

This package lets you create PDFs using pdfmake and React.

npm version

Installation

This package is available on NPM.

npm i react-pdfmake-reconciler

Features

  • Write complex PDF in JSX. Render JSX into pdfmake content structure.
  • Utilize React features like:
    • Context. Note that outside React contexts do not penetrate into PDF renderer.
    • Components
    • Hooks
  • Working React update loop, (although it is unlikely to trigger user events inside PDF.), e.g.
    • async setState calls
    • useEffect call
  • TypeScript typing for pdfmake Components (<pdf-*> components)
  • React Developer Tools support
  • Styled API

React Developer Tools Demo

Running demo

pnpm i
pnpm dev

Usage

See /demo and tests for more extensive examples.

Simple examples

/// <reference types="react-pdfmake-reconciler/react-jsx" />

import { PdfRenderer } from "react-pdfmake-reconciler/PdfRenderer";

const { unmount } = PdfRenderer.render(
  <pdf-text bold>Hello World!</pdf-text>,
  (document) => console.log(document),
);

/*
Console:
{
  content: {
    $__reactPdfMakeType: 'pdf-text',
    text: 'Hello World!',
    bold: true
  }
}
*/

// Call unmount to detach node tree.
unmount();
import { PdfRenderer } from "react-pdfmake-reconciler/PdfRenderer";

const document = PdfRenderer.renderOnce(<pdf-text bold>Hello World!</pdf-text>);

PDF elements

Newly defined intrinsic elements by this package have the pdf- prefix. Roughly speaking, each type of pdfmake content object corresponds to one element type, where the property specifying the Content is mapped to the children prop. For example:

const pdfMakeContent = {
  text: "GitHub",
  link: "https://www.github.com",
};

// is mapped to

const pdfNode = <pdf-text link="https://www.github.com">GitHub</pdf-text>;

There are also virtual element types. For more information, read JSDocs in types for more information.

Document, Header, and Footer

You can easily define extra document definition props straight inside your JSX using <PdfDocument>. It is optional to put the body of the document inside this component.

Implemented using React Portals, you can define static/dynamic header and footer using <PdfHeader> and <PdfFooter>.

These components can appear anywhere within your JSX structure, although you may follow this convention for a better looking structure:

import { PdfDocument, PdfHeader, PdfFooter } from "react-pdfmake-reconciler";

const pdfNode = (
  <PdfDocument orientation="landscape">
    {/* Example static header */}
    <PdfHeader>This is a header</PdfHeader>
    {/* Example dynmaic footer */}
    <PdfFooter>
      {(pageNumber, pageCount) => (
        <pdf-text>
          Page {pageNumber} / {pageCount}
        </pdf-text>
      )}
    </PdfFooter>
    {bodyGoesHere}
  </PdfDocument>
);

PdfPreview

<PdfPreview> provides an easy way to render your React pdfmake Reconciler JSX in the browser. You can also debug your PDF JSX using the React Developer Tools browser extension.

import { FC, StrictMode } from "react";
import { PdfPreview } from "react-pdfmake-reconciler";

const App: FC = () => (
  <div>
    <PdfPreview>
      {/* Optional */}
      <StrictMode>
        {/* Only use components that resolves to pdf-* components from here on out. DOM elements won't work. */}
        <pdf-text>Hello World!</pdf-text>
      </StrictMode>
    </PdfPreview>
  </div>
);