3.1.1 • Published 1 year ago

react-advanced-pdf-upload v3.1.1

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

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 nameTypeDescriptionOptionalDefault value
componentsobjectThe additional frontend components with which AdvancedPdfUpload is populated. See here for an example of all its properties.No
finalizeButtonobjectAn object with the properties ref (required), setLoading (optional) and setDisabled to control the finalize button.No
previewResolutionintegerThe resolution for PDF page previews in PPI (pixels per inch).Yes100
previewAreaHeightintegerThe height of the entire page preview area in pixels.Yes240
previewAreaPaddingintegerThe inner padding of the preview area in pixels.Yes16
previewSpacingintegerThe distance between two page previews in pixels.Yes24
previewControlsHeightintegerThe height of the page preview controls (for page numbers and rotation buttons) in pixels.Yes40
loadPreviewsfunctionThe callback for loading page previews. Gets an object according to this schema as parameter. Should return the rendered pages according to this type.No
buildPdffunctionThe 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
showPreviewAreaWhenEmptybooleanWhether the area with the page previews shall be shown (i.e. just take up empty space) even when no pages are there yet.Yesfalse
defaultFilenamestringWhen 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.Yesupload.pdf

License

MIT

3.1.1

1 year ago

3.1.0

1 year ago

3.0.0

2 years ago

2.1.1

2 years ago

2.1.0

2 years ago

2.0.1

2 years ago

2.0.0

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago