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

@rawwee/react-pdf-html

v1.2.1

Published

Package for rendering @react-pdf/renderer components as HTML components

Downloads

358

Readme

react-pdf-html

react-pdf-html is a package that provides components that can render @react-pdf/renderer components as HTML components.

This provides an alternative for rendering PDF on the fly to check the final PDF and it enables having one source of truth for your PDF templates/components.

Installation

You can install react-pdf-html using npm/yarn/bun:

npm install @rawwee/react-pdf-html

Usage

To use react-pdf-html, you need to import the components you want to use from the package and render them in your React application as you would when using @react-pdf/renderer. Here's an example:

Example when displaying it in application

import React from "react";
// this replaces @react-pdf/renderer components
import { Document, Image, Page, View } from "@rawwee/react-pdf-html";

const MyDocument = () => (
  <Document>
    <Page>
      <Text>Hello, World!</Text>
    </Page>
  </Document>
);

// You can then render the component as an HTML component
const App = () => <MyDocument />;

export default App;

In this example, we're rendering a simple PDF document with a single page that contains the text "Hello, World!". We're using the PDFViewer component from react-pdf-html to render the PDF as an HTML component.

As you can see, you don't need the @react-pdf/renderer's PDFViewer component to render the PDF (or Document component from wojtekmaj/react-pdf).

Example when rendering the PDF

The package provides a useful hook called usePDFComponentsAreHTML() which enables (or disables) the rendering of the components as HTML components. This is useful when you want to render the PDF conditionally (e.g. when you want to download the PDF).

export const PDFDownload = ({ PdfInstance, show, closeModal }: Props) => {
  const { isHTML, setHtml } = usePDFComponentsAreHTML();
  const [download, setDownload] = useState(false);

  useEffect(() => {
    if (show) {
      // triggers the rendering of the PDF template component
      // which is defined as:
      // const TemplateNotHtml = useCallback(() => CVTemplate(pdfData), [isHTML, pdfData]);
      setHtml(false);
    }
  }, [show]);

  return (
    <Modal
      show={show}
      position="center"
      closeModal={() => {
        setHtml(true);
        setDownload(false);
      }}
    >
      <div>
        <h1 className="text-2xl font-bold">Download CV</h1>
        <div>
          {PdfInstance &&
            (download ? (
              <PDFDownloadLink
                className="w-full bg-blue-500 p-2 text-center font-bold text-white shadow-[0_0_20px_-5px] hover:shadow-blue-800 focus:shadow-blue-800"
                document={!isHTML ? <PdfInstance /> : <></>}
                fileName={`${cvName}.pdf`}
              >
                {({ blob, url, loading, error }) => {
                  if (loading) return "Loading document...";
                  return "Download now!";
                }}
              </PDFDownloadLink>
            ) : (
              <button
                className="w-full bg-blue-500 p-2 text-center font-bold text-white shadow-[0_0_20px_-5px] hover:shadow-blue-800 focus:shadow-blue-800"
                onClick={() => setDownload(true)}
              >
                Download
              </button>
            ))}
        </div>
      </div>
    </Modal>
  );
};

As you can see, we're using the usePDFComponentsAreHTML() hook to set the isHTML value to false when the modal is shown. This triggers the rendering of the PDF template component. When the modal is closed, we set the isHTML value to true which triggers the rendering of the PDF as an HTML component.

Used by

You can check the usage for this package in the following projects:

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.