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-to-pdf-ts

v1.0.10

Published

Convert a React component to PDF using Puppeteer

Downloads

21

Readme

react-to-pdf-ts

This package provides a convenient and efficient way to convert React components into PDF files, leveraging the capabilities of Puppeteer in a TypeScript environment. It is designed with a focus on ease of use, flexibility, and robustness, making it an ideal solution for projects requiring PDF generation from React components.

Features

  • React to PDF Conversion: Seamlessly convert React components into high-quality PDF files.
  • Buffer Support: No need to write to disk; you can also get the PDF as a buffer.
  • Puppeteer Integration: Utilizes Puppeteer for reliable and accurate rendering.
  • TypeScript Support: Full TypeScript support ensures type safety and developer-friendly usage.
  • Customizable Options: Offers a range of customization options for Puppeteer, giving you control over the PDF generation process.

Prerequisites

Before using react-to-pdf-ts, ensure the following is set up in your Node.js TypeScript project:

  • Node.js and npm (or yarn) installed.

  • A project set up with TypeScript.

  • React and React DOM: Install these as they are peer dependencies of this package.

    npm install react react-dom
    

Installation

Install the package using npm:

npm install react-to-pdf-ts

Usage

Before you begin, make sure your TypeScript configuration (tsconfig.json) supports JSX. Set "jsx": "react" in your compiler options.

Additionally, when working with JSX in TypeScript, use the .tsx extension for your files. TypeScript treats .tsx files differently due to the JSX syntax.

Here's a quick example to get you started with react-to-pdf-ts in a TypeScript project:

  1. Import the package:
import {convertToPdf} from 'react-to-pdf-ts';
import * as React from 'react';
  1. Create a React Component:

You can either define a new React component or import an existing one. Here's an example of a simple component:

const MyComponent: React.FC = () => <div>Hello PDF World!</div>;
  1. Convert to PDF:

Call convertToPdf with your component and specify the output path for the PDF file:

convertToPdf(<MyComponent / >, {outputPath: './output.pdf'})
    .then((buffer: Buffer) => console.log('PDF buffer:', buffer))
    .catch(error => console.error('Error creating PDF:', error));

Ensure that your TypeScript project is properly set up to compile JSX.

API

convertToPdf(component: React.ReactElement, options: ConvertToPdfOptions): Promise<Buffer>

  • component: React component you wish to convert into a PDF.
  • options: Configuration options for PDF generation.
    • outputPath: (optional) File path to save the generated PDF.
    • puppeteerOptions (optional): Custom options for Puppeteer.

License

This project is licensed under the MIT License - see the LICENSE file for full details.