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-advanced-pdf-upload

v3.1.1

Published

This library provides an easy-to-use React component for interactively creating a single PDF file out of several uploaded ones, with the possibility to concatenate, reorder, remove and rotate pages.

Downloads

19

Readme

react-advanced-pdf-upload

GitHub license npm

This library provides an easy-to-use React component for interactively creating a single PDF file out of several uploaded ones, with the possibility to concatenate, reorder, remove and rotate pages. It has been designed to be as customizable as possible. Additionally, it can work with PDFs with different page dimensions without any issues.

How it works

Most importantly, this library simply provides a React component for the frontend, but no code to actually work with PDFs. All actual functionality for rendering and building PDFs belongs to the https://github.com/sigalor/react-advanced-pdf-upload-backend component, i.e. runs on a backend server. This is mostly to keep this library's dependencies at a minimum.

However, it is certainly possible to do everything in the frontend, as the implementation of the two callbacks dealing with PDFs (loadPreviews and buildPdf, see below) is completely up to the user of this library.

The example in the following section assumes that the backend server from https://github.com/sigalor/react-advanced-pdf-upload-backend is running at http://localhost:3001.

Getting started

First, run the following to install this library. Make sure that this library's peer dependencies are installed.

npm install react-advanced-pdf-upload

After this, the most minimal usage of the AdvancedPdfUpload component looks like this:

import React, { useRef, useState } from 'react';
import AdvancedPdfUpload from 'react-advanced-pdf-upload';

export default () => {
  const finalizeButtonRef = useRef(null);
  const [finalizeButtonLoading, setFinalizeButtonLoading] = useState(false);
  const [finalizeButtonDisabled, setFinalizeButtonDisabled] = useState(false);

  return (
    <>
      <AdvancedPdfUpload
        components={{
          dropzonePlaceholder: <p>Drag and drop PDF files here or click to select files.</p>,
          loading: <p>Loading...</p>,
          pageNumber: ({ n }) => <i>{n}</i>,
        }}
        finalizeButton={{
          ref: finalizeButtonRef,
          setLoading: setFinalizeButtonLoading,
          setDisabled: setFinalizeButtonDisabled,
        }}
        loadPreviews={async data => {
          const res = await fetch('http://localhost:3001/render-pdf', {
            method: 'POST',
            headers: {
              Accept: 'application/json',
              'Content-Type': 'application/json',
            },
            body: JSON.stringify(data),
          }).catch(e => console.error(e));

          if (res && res.status >= 200 && res.status < 299) {
            return await res.json();
          } else {
            console.error(res);
          }
        }}
        buildPdf={async data => {
          const res = await fetch('http://localhost:3001/build-pdf', {
            method: 'POST',
            headers: {
              Accept: 'application/pdf',
              'Content-Type': 'application/json',
            },
            body: JSON.stringify(data),
          }).catch(e => console.error(e));

          if (res && res.status >= 200 && res.status < 299) {
            // do something with the finalized PDF file, e.g. let the user download it
            // the proposed new filename can be found in `data.name`
            return 'reset';
          } else {
            console.error(res);
            return 'resetLoading';
          }
        }}
      />
      <button
        ref={finalizeButtonRef}
        disabled={finalizeButtonLoading || finalizeButtonDisabled}
        style={{ marginTop: '0.5rem' }}
      >
        {finalizeButtonLoading ? 'Loading...' : 'Finalize'}
      </button>
    </>
  );
};

This code renders something like the following:

Basic demo 1

You can easily upload PDF files in the upper dropzone (provided by react-dropzone), then they appear at the bottom:

Basic demo 2

In the bottom area, you can easily drag-and-drop the pages around and remove or rotate pages. Once you upload more PDF files, they are appended to the back of the list of pages. When you click "Finalize", the final PDF is generated using the buildPdf callback.

Documentation

The AdvancedPdfUpload component expects the following parameters, which have been designed for maximum customizability:

| Parameter name | Type | Description | Optional | Default value | | ------------------------ | -------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------- | ------------- | | components | object | The additional frontend components with which AdvancedPdfUpload is populated. See here for an example of all its properties. | No | | | finalizeButton | object | An object with the properties ref (required), setLoading (optional) and setDisabled to control the finalize button. | No | | | previewResolution | integer | The resolution for PDF page previews in PPI (pixels per inch). | Yes | 100 | | previewAreaHeight | integer | The height of the entire page preview area in pixels. | Yes | 240 | | previewAreaPadding | integer | The inner padding of the preview area in pixels. | Yes | 16 | | previewSpacing | integer | The distance between two page previews in pixels. | Yes | 24 | | previewControlsHeight | integer | The height of the page preview controls (for page numbers and rotation buttons) in pixels. | Yes | 40 | | loadPreviews | function | The callback for loading page previews. Gets an object according to this schema as parameter. Should return the rendered pages according to this type. | No | | | buildPdf | function | The callback that is called once the user clicks the finalize button with an object according to this schema. Should return "resetLoading" when after this function is finished, only the loading state should be reset, or "reset" to reset the entire state. If nothing or something else is returned, nothing is done, i.e. also no further setters are called. | No | | | showPreviewAreaWhenEmpty | boolean | Whether the area with the page previews shall be shown (i.e. just take up empty space) even when no pages are there yet. | Yes | false | | defaultFilename | string | When exactly one file was upload, its name is used for the name property in the object passed to buildPdf. Otherwise this default filename is used. | Yes | upload.pdf |

License

MIT