pnp-pdf
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

pnp-pdf

Plug and play PDF files using pdf.js. One of the easiest way to render PDF files on browsers.

Installation | HTML example | React.js example

Install npm globally (ignore if already installed):

npm i -g npm

Install the pnp-pdf library:

npm i pnp-pdf

Setup the pnp-pdf library on a JavaScript project

Step 1: Add the HTML file from here https://github.com/abhisekdutta507/pnp-pdf-example.

<body tabindex="0">
  <div id="outerContainer">

    <!-- sidebarContainer -->

    <div id="mainContainer">
      <div class="toolbar"></div>

      <div id="viewerContainer" tabindex="0">
        <div id="viewer" class="pdfViewer"></div>
      </div>
    </div> <!-- mainContainer -->

    <!-- dialogContainer -->

    <!-- editorUndoBar -->

  </div> <!-- outerContainer -->
  <div id="printContainer"></div>
</body>

Step 2: Import the styles

@import 'pnp-pdf/plugin/styles.css';

Step 3: Open the file on browser

import { open } from 'pnp-pdf';

const file = "https://raw.githubusercontent.com/mozilla/pdf.js/ba2edeae/web/compressed.tracemonkey-pldi-09.pdf";

open(file);

Setup the pnp-pdf library on a React.js project

Step 1: Let's create the JSX component like react_pdf_viewer. And only take the JSX.

/* eslint-disable jsx-a11y/aria-role */
/* eslint-disable jsx-a11y/role-has-required-aria-props */
/* eslint-disable jsx-a11y/role-supports-aria-props */
/* eslint-disable jsx-a11y/anchor-is-valid */

import { useEffect } from "react";
import { open } from 'pnp-pdf';
import "pnp-pdf/plugin/styles.css";

/**
 * @param {{
 *    file: string;
 *    style: CSSProperties;
 * }} props 
 * @returns 
 */
export const PDFViewer = ({
  file,
  style,
}) => {
  useEffect(() => {    
    open(file);
  }, [file]);

  return (
    <div id="pdfViewerReactComponent" tabIndex={0} style={style}>
      {/* static JSX template from the react_pdf_viewer.jsx file */}
    </div>
  );
};

export default PDFViewer;

Step 2: Use the JSX component in your project

import { PDFViewer } from "./react_pdf_viewer";

const file = "https://raw.githubusercontent.com/mozilla/pdf.js/ba2edeae/web/compressed.tracemonkey-pldi-09.pdf";

export default function ReactComponent() {
  return (
    <div className="relative w-screen h-screen">
      <PDFViewer file={file} style={{}} />
    </div>
  );
}

Best Practices

  • Make sure the PDFViewer component has some fixed height. By default, width is 100%. If the wrapper does not have a fixed height the script will fail to display the pdf file.
  • It works best when displayed full screen.
  • In real world projects we can not always use it fullscreen. Then, style={{ minWidth: 760 }} will give the best view.

Credits

  1. pdfjs-dist

Source Code

See the package source for more details.

Package Sidebar

Install

npm i pnp-pdf

Weekly Downloads

12

Version

1.0.0

License

Apache-2.0

Unpacked Size

12.2 MB

Total Files

81

Last publish

Collaborators

  • abhisek507