Document Viewer
PDF and docx viewer for Vanilla JavaScript and React applications
Note: this viewer has been stripped to essentially a wrapper around a few separate third-party applications, for the original version that used a custom integration of the pdf.js library use version 0.3.2-legacy or below.
Installation
Install the package:
npm install document-viewer-ts
Example Usage
With HTML and Vanilla JS
Call the init script in the root of your JS application.
index.js
import { init } from 'document-viewer'
init();
Wherever you want to include a document viewer in the HTML, include a <div />
with class="viewer-container"
and id
being some unique key (on the page) and data-document-url
being the url of the document you want to display. Also make sure you're importing styles.css
from the package in your HTML head.
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script src="./index.js"></script>
<link rel="stylesheet" href="../node_modules/document-viewer/dist/styles/styles.css"></link>
</head>
<body>
<div class="viewer-container" id="doc-1" data-document-url="https://raw.githubusercontent.com/mozilla/pdf.js/ba2edeae/web/compressed.tracemonkey-pldi-09.pdf"></div>
</body>
</html>
With React
Import the Viewer
component and call it with the proper documentId
and documentUrl
props. Also make sure to import styles.css
in your app.
index.jsx
import React from 'react'
import { Viewer } from 'document-viewer'
export default () =>
<Viewer
documentId="doc-1"
documentUrl="https://raw.githubusercontent.com/mozilla/pdf.js/ba2edeae/web/compressed.tracemonkey-pldi-09.pdf"
/>