Security News
Bun 1.2 Released with 90% Node.js Compatibility and Built-in S3 Object Support
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
document-viewer-ts
Advanced tools
PDF and MS Doc viewer written in TypeScript for React and vanilla JavaScript
Requires peer dependency pdfjs-dist
:
npm install pdfjs-dist
Install the package:
npm install document-viewer-ts
If using webpack, copy PATH/TO/NODE_MODULES/pdfjs-dist/build/pdf.worker.min.js
to a local directory:
webpack.config.ts
import CopyPlugin from "copy-webpack-plugin";
...
const config = {
...
plugins: [
...
new CopyPlugin({
patterns: [
{
from: "PATH/TO/NODE_MODULES/document-viewer/dist/worker/pdf.worker.min.js",
to: "PATH/TO/WORKER/FILE"
}
]
}
]
}
...
Call the init script in the root of your JS application using the path to your worker file.
index.js
import { init } from 'document-viewer'
init("PATH/TO/WORKER/FILE");
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>
Import the Viewer
component and call it with the proper documentId
, documentUrl
, and workerSrc
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"
workerSrc="PATH/TO/WORKER/FILE"
/>
FAQs
PDF and MS Doc viewer written in TypeScript for React and vanilla JavaScript
The npm package document-viewer-ts receives a total of 23 weekly downloads. As such, document-viewer-ts popularity was classified as not popular.
We found that document-viewer-ts demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
Security News
Biden's executive order pushes for AI-driven cybersecurity, software supply chain transparency, and stronger protections for federal and open source systems.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.