document-viewer-ts
Advanced tools
Comparing version 0.3.2-legacy to 0.4.0
@@ -1,3 +0,2 @@ | ||
export declare const renderPDF: (containerDiv: Element, documentUrl: string) => Promise<unknown>; | ||
export declare const renderDocument: (containerDiv: Element) => void; | ||
export declare const init: (workerSrc: string) => void; | ||
export declare const init: () => void; |
@@ -1,180 +0,13 @@ | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
import { GlobalWorkerOptions, getDocument, renderTextLayer } from 'pdfjs-dist/legacy/build/pdf'; | ||
const chevronLeft = `<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-chevron-left" viewBox="0 0 16 16"> | ||
<path fill-rule="evenodd" d="M11.354 1.646a.5.5 0 0 1 0 .708L5.707 8l5.647 5.646a.5.5 0 0 1-.708.708l-6-6a.5.5 0 0 1 0-.708l6-6a.5.5 0 0 1 .708 0z"/> | ||
</svg>`; | ||
const chevronRight = `<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-chevron-right" viewBox="0 0 16 16"> | ||
<path fill-rule="evenodd" d="M4.646 1.646a.5.5 0 0 1 .708 0l6 6a.5.5 0 0 1 0 .708l-6 6a.5.5 0 0 1-.708-.708L10.293 8 4.646 2.354a.5.5 0 0 1 0-.708z"/> | ||
</svg>`; | ||
const plusSvg = `<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="16" height="16"><g xmlns="http://www.w3.org/2000/svg" fill="#fff"> | ||
<polygon opacity=".85" enable-background="new" points="9 8 9 6 8 6 8 8 6 8 6 9 8 9 8 11 9 11 9 9 11 9 11 8"/> | ||
<path d="m16.742 15.501l-2.742-2.743h-0.643l-0.684-0.683c0.827-0.962 1.327-2.209 1.327-3.575 0-3.038-2.463-5.5-5.5-5.5s-5.5 2.462-5.5 5.5 2.463 5.5 5.5 5.5c1.367 0 2.613-0.501 3.576-1.327l0.684 0.683v0.644l2.742 2.742c0.342 0.343 0.896 0.344 1.24 0.001 0.344-0.345 0.344-0.898 0-1.242zm-8.242-3.301c-2.044 0-3.7-1.657-3.7-3.7s1.656-3.7 3.7-3.7c2.043 0 3.699 1.657 3.699 3.7s-1.656 3.7-3.699 3.7z"/> | ||
</g></svg>`; | ||
const minusSvg = `<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="16" height="16"><g xmlns="http://www.w3.org/2000/svg" fill="#fff"> | ||
<path d="m16.742 15.501l-2.742-2.743h-0.643l-0.684-0.684c0.827-0.961 1.327-2.208 1.327-3.574 0-3.037-2.463-5.5-5.5-5.5s-5.5 2.463-5.5 5.5 2.463 5.5 5.5 5.5c1.367 0 2.613-0.502 3.576-1.326l0.684 0.682v0.644l2.742 2.742c0.342 0.343 0.896 0.344 1.24 0.001 0.344-0.345 0.344-0.898 0-1.242zm-8.242-3.301c-2.044 0-3.7-1.657-3.7-3.7s1.656-3.7 3.7-3.7c2.043 0 3.699 1.657 3.699 3.7s-1.656 3.7-3.699 3.7z"/> | ||
<polygon opacity=".85" enable-background="new" points="9 8 8 8 6 8 6 9 8 9 9 9 11 9 11 8"/> | ||
</g></svg>`; | ||
const handleError = (containerDiv) => (err) => { | ||
renderErrorMessage(containerDiv)(`There was an error fetching your document. Please try again later. Error: ${err}`); | ||
}; | ||
const pdfScale = 10; | ||
const zoomedWidth = '100%'; | ||
const defaultWidth = '80%'; | ||
const scaleTextLayer = (textLayerDiv, textContent, pdfPage, canvas, viewport) => __awaiter(void 0, void 0, void 0, function* () { | ||
textLayerDiv.innerHTML = ''; | ||
const textLayerFragment = document.createDocumentFragment(); | ||
const scale = pdfScale * (canvas.offsetWidth / viewport.width); | ||
const vs = pdfPage.getViewport({ scale }); | ||
textLayerDiv.style.width = `${vs.width}px`; | ||
textLayerDiv.style.height = `${vs.height}px`; | ||
yield renderTextLayer({ | ||
textContent, | ||
container: textLayerFragment, | ||
viewport: vs | ||
}).promise; | ||
textLayerDiv.appendChild(textLayerFragment); | ||
}); | ||
const createLoadingIndicator = () => { | ||
const container = document.createElement('div'); | ||
container.className = 'indicator-container'; | ||
const indicator = document.createElement('div'); | ||
indicator.className = 'lds-ring'; | ||
indicator.innerHTML = '<div></div><div></div><div></div><div></div></div>'; | ||
container.appendChild(indicator); | ||
return container; | ||
const renderPDF = (containerDiv, documentUrl) => { | ||
const mozillaPDFViewer = document.createElement('iframe'); | ||
mozillaPDFViewer.width = '100%'; | ||
mozillaPDFViewer.height = '100%'; | ||
mozillaPDFViewer.setAttribute('frameborder', '0'); | ||
const params = new URLSearchParams({ file: documentUrl }); | ||
mozillaPDFViewer.setAttribute('src', `https://mozilla.github.io/pdf.js/web/viewer.html?${params.toString()}`); | ||
containerDiv.appendChild(mozillaPDFViewer); | ||
}; | ||
export const renderPDF = (containerDiv, documentUrl) => __awaiter(void 0, void 0, void 0, function* () { | ||
const loadingIndicator = createLoadingIndicator(); | ||
containerDiv.appendChild(loadingIndicator); | ||
const documentId = containerDiv.id; | ||
const canvasContainer = document.createElement('div'); | ||
canvasContainer.className = 'canvas-container'; | ||
const controlsDiv = document.createElement('div'); | ||
controlsDiv.className = 'viewer-controls'; | ||
const fullPageNumberDiv = document.createElement('div'); | ||
fullPageNumberDiv.className = 'page-number'; | ||
const pageNumberInput = document.createElement('input'); | ||
pageNumberInput.className = 'page-number-input'; | ||
pageNumberInput.type = 'number'; | ||
const pageDiv = document.createElement('div'); | ||
const outOfDiv = document.createElement('div'); | ||
const pageCountDiv = document.createElement('div'); | ||
const nextButton = document.createElement('button'); | ||
nextButton.className = 'next-button'; | ||
const prevButton = document.createElement('button'); | ||
prevButton.className = 'prev-button'; | ||
const zoomButton = document.createElement('button'); | ||
zoomButton.className = 'zoom-button'; | ||
const loadingTask = getDocument(documentUrl); | ||
try { | ||
const pdfDocument = yield loadingTask.promise; | ||
containerDiv.removeChild(loadingIndicator); | ||
const isValidPage = (page) => page <= pdfDocument.numPages && page > 0; | ||
// initial viewer setup | ||
pageNumberInput.value = '1'; | ||
pageDiv.textContent = 'Page '; | ||
outOfDiv.textContent = '/'; | ||
pageCountDiv.textContent = `${pdfDocument.numPages}`; | ||
fullPageNumberDiv.appendChild(pageDiv); | ||
fullPageNumberDiv.appendChild(pageNumberInput); | ||
fullPageNumberDiv.appendChild(outOfDiv); | ||
fullPageNumberDiv.appendChild(pageCountDiv); | ||
// page container setup | ||
const pageContainer = document.createElement('div'); | ||
pageContainer.className = 'page-container'; | ||
pageContainer.style.width = defaultWidth; | ||
canvasContainer.appendChild(pageContainer); | ||
const canvas = document.createElement('canvas'); | ||
canvas.className = 'pdf-viewer-canvas'; | ||
canvas.id = `${documentId}-canvas`; | ||
pageContainer.appendChild(canvas); | ||
// text layer setup | ||
const textLayerDiv = document.createElement('div'); | ||
textLayerDiv.className = 'text-layer'; | ||
pageContainer.appendChild(textLayerDiv); | ||
nextButton.innerHTML = chevronRight; | ||
prevButton.innerHTML = chevronLeft; | ||
zoomButton.innerHTML = plusSvg; | ||
containerDiv.appendChild(controlsDiv); | ||
controlsDiv.appendChild(prevButton); | ||
controlsDiv.appendChild(fullPageNumberDiv); | ||
controlsDiv.appendChild(nextButton); | ||
controlsDiv.appendChild(zoomButton); | ||
containerDiv.appendChild(canvasContainer); | ||
const displayPage = (page) => __awaiter(void 0, void 0, void 0, function* () { | ||
pageNumberInput.value = `${page}`; | ||
const pdfPage = yield pdfDocument.getPage(page); | ||
const viewport = pdfPage.getViewport({ scale: pdfScale }); | ||
canvas.width = viewport.width; | ||
canvas.height = viewport.height; | ||
canvas.style.width = '100%'; | ||
const ctx = canvas.getContext('2d'); | ||
yield pdfPage.render({ | ||
canvasContext: ctx || {}, | ||
viewport, | ||
}); | ||
const textContent = yield pdfPage.getTextContent(); | ||
scaleTextLayer(textLayerDiv, textContent, pdfPage, canvas, viewport); | ||
zoomButton.onclick = () => { | ||
if (pageContainer.style.width === defaultWidth) { | ||
pageContainer.style.width = zoomedWidth; | ||
zoomButton.innerHTML = minusSvg; | ||
} | ||
else { | ||
pageContainer.style.width = defaultWidth; | ||
zoomButton.innerHTML = plusSvg; | ||
} | ||
scaleTextLayer(textLayerDiv, textContent, pdfPage, canvas, viewport); | ||
}; | ||
window.addEventListener('resize', () => { | ||
scaleTextLayer(textLayerDiv, textContent, pdfPage, canvas, viewport); | ||
}); | ||
}); | ||
const skipPage = (direction) => () => { | ||
const pageNumber = parseInt(pageNumberInput.value || '1'); | ||
const nextPageNumber = pageNumber + direction; | ||
if (isValidPage(nextPageNumber)) { | ||
displayPage(nextPageNumber); | ||
} | ||
}; | ||
nextButton.onclick = skipPage(1); | ||
prevButton.onclick = skipPage(-1); | ||
pageNumberInput.onchange = () => { | ||
const pageNumber = Math.max(Math.min(parseInt(pageNumberInput.value), pdfDocument.numPages), 1); | ||
displayPage(pageNumber); | ||
}; | ||
pageNumberInput.addEventListener('click', (e) => e.stopPropagation()); | ||
containerDiv.addEventListener('keydown', (e) => { | ||
e.stopPropagation(); | ||
switch (e.key) { | ||
case ('ArrowLeft'): | ||
skipPage(-1)(); | ||
return; | ||
case ('ArrowRight'): | ||
skipPage(1)(); | ||
return; | ||
default: | ||
return; | ||
} | ||
}); | ||
containerDiv.addEventListener('click', () => { | ||
containerDiv.focus(); | ||
}); | ||
containerDiv.setAttribute('tabIndex', '1'); | ||
containerDiv.focus(); | ||
yield displayPage(1); | ||
} | ||
catch (err) { | ||
handleError(containerDiv)(err); | ||
return err; | ||
} | ||
}); | ||
const renderDocx = (containerDiv, documentUrl) => { | ||
@@ -252,4 +85,3 @@ const microsoftViewer = document.createElement('iframe'); | ||
}; | ||
export const init = (workerSrc) => { | ||
GlobalWorkerOptions.workerSrc = workerSrc; | ||
export const init = () => { | ||
loadDocuments(); | ||
@@ -256,0 +88,0 @@ window.addEventListener('load', loadDocuments); |
@@ -5,5 +5,4 @@ import React from 'react'; | ||
documentUrl: string; | ||
workerSrc: string; | ||
}; | ||
export declare const Viewer: React.FC<ViewerProps>; | ||
export {}; |
import React, { useEffect, useRef } from 'react'; | ||
import { GlobalWorkerOptions } from 'pdfjs-dist/legacy/build/pdf'; | ||
import { renderDocument } from './base'; | ||
export const Viewer = (props) => { | ||
GlobalWorkerOptions.workerSrc = props.workerSrc; | ||
const viewerContainer = useRef(null); | ||
@@ -7,0 +5,0 @@ useEffect(() => { viewerContainer.current && renderDocument(viewerContainer.current); }, []); |
@@ -1,3 +0,2 @@ | ||
export declare const renderPDF: (containerDiv: Element, documentUrl: string) => Promise<unknown>; | ||
export declare const renderDocument: (containerDiv: Element) => void; | ||
export declare const init: (workerSrc: string) => void; | ||
export declare const init: () => void; |
"use strict"; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.init = exports.renderDocument = exports.renderPDF = void 0; | ||
const pdf_1 = require("pdfjs-dist/legacy/build/pdf"); | ||
const chevronLeft = `<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-chevron-left" viewBox="0 0 16 16"> | ||
<path fill-rule="evenodd" d="M11.354 1.646a.5.5 0 0 1 0 .708L5.707 8l5.647 5.646a.5.5 0 0 1-.708.708l-6-6a.5.5 0 0 1 0-.708l6-6a.5.5 0 0 1 .708 0z"/> | ||
</svg>`; | ||
const chevronRight = `<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-chevron-right" viewBox="0 0 16 16"> | ||
<path fill-rule="evenodd" d="M4.646 1.646a.5.5 0 0 1 .708 0l6 6a.5.5 0 0 1 0 .708l-6 6a.5.5 0 0 1-.708-.708L10.293 8 4.646 2.354a.5.5 0 0 1 0-.708z"/> | ||
</svg>`; | ||
const plusSvg = `<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="16" height="16"><g xmlns="http://www.w3.org/2000/svg" fill="#fff"> | ||
<polygon opacity=".85" enable-background="new" points="9 8 9 6 8 6 8 8 6 8 6 9 8 9 8 11 9 11 9 9 11 9 11 8"/> | ||
<path d="m16.742 15.501l-2.742-2.743h-0.643l-0.684-0.683c0.827-0.962 1.327-2.209 1.327-3.575 0-3.038-2.463-5.5-5.5-5.5s-5.5 2.462-5.5 5.5 2.463 5.5 5.5 5.5c1.367 0 2.613-0.501 3.576-1.327l0.684 0.683v0.644l2.742 2.742c0.342 0.343 0.896 0.344 1.24 0.001 0.344-0.345 0.344-0.898 0-1.242zm-8.242-3.301c-2.044 0-3.7-1.657-3.7-3.7s1.656-3.7 3.7-3.7c2.043 0 3.699 1.657 3.699 3.7s-1.656 3.7-3.699 3.7z"/> | ||
</g></svg>`; | ||
const minusSvg = `<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="16" height="16"><g xmlns="http://www.w3.org/2000/svg" fill="#fff"> | ||
<path d="m16.742 15.501l-2.742-2.743h-0.643l-0.684-0.684c0.827-0.961 1.327-2.208 1.327-3.574 0-3.037-2.463-5.5-5.5-5.5s-5.5 2.463-5.5 5.5 2.463 5.5 5.5 5.5c1.367 0 2.613-0.502 3.576-1.326l0.684 0.682v0.644l2.742 2.742c0.342 0.343 0.896 0.344 1.24 0.001 0.344-0.345 0.344-0.898 0-1.242zm-8.242-3.301c-2.044 0-3.7-1.657-3.7-3.7s1.656-3.7 3.7-3.7c2.043 0 3.699 1.657 3.699 3.7s-1.656 3.7-3.699 3.7z"/> | ||
<polygon opacity=".85" enable-background="new" points="9 8 8 8 6 8 6 9 8 9 9 9 11 9 11 8"/> | ||
</g></svg>`; | ||
exports.init = exports.renderDocument = void 0; | ||
const handleError = (containerDiv) => (err) => { | ||
renderErrorMessage(containerDiv)(`There was an error fetching your document. Please try again later. Error: ${err}`); | ||
}; | ||
const pdfScale = 10; | ||
const zoomedWidth = '100%'; | ||
const defaultWidth = '80%'; | ||
const scaleTextLayer = (textLayerDiv, textContent, pdfPage, canvas, viewport) => __awaiter(void 0, void 0, void 0, function* () { | ||
textLayerDiv.innerHTML = ''; | ||
const textLayerFragment = document.createDocumentFragment(); | ||
const scale = pdfScale * (canvas.offsetWidth / viewport.width); | ||
const vs = pdfPage.getViewport({ scale }); | ||
textLayerDiv.style.width = `${vs.width}px`; | ||
textLayerDiv.style.height = `${vs.height}px`; | ||
yield (0, pdf_1.renderTextLayer)({ | ||
textContent, | ||
container: textLayerFragment, | ||
viewport: vs | ||
}).promise; | ||
textLayerDiv.appendChild(textLayerFragment); | ||
}); | ||
const createLoadingIndicator = () => { | ||
const container = document.createElement('div'); | ||
container.className = 'indicator-container'; | ||
const indicator = document.createElement('div'); | ||
indicator.className = 'lds-ring'; | ||
indicator.innerHTML = '<div></div><div></div><div></div><div></div></div>'; | ||
container.appendChild(indicator); | ||
return container; | ||
const renderPDF = (containerDiv, documentUrl) => { | ||
const mozillaPDFViewer = document.createElement('iframe'); | ||
mozillaPDFViewer.width = '100%'; | ||
mozillaPDFViewer.height = '100%'; | ||
mozillaPDFViewer.setAttribute('frameborder', '0'); | ||
const params = new URLSearchParams({ file: documentUrl }); | ||
mozillaPDFViewer.setAttribute('src', `https://mozilla.github.io/pdf.js/web/viewer.html?${params.toString()}`); | ||
containerDiv.appendChild(mozillaPDFViewer); | ||
}; | ||
const renderPDF = (containerDiv, documentUrl) => __awaiter(void 0, void 0, void 0, function* () { | ||
const loadingIndicator = createLoadingIndicator(); | ||
containerDiv.appendChild(loadingIndicator); | ||
const documentId = containerDiv.id; | ||
const canvasContainer = document.createElement('div'); | ||
canvasContainer.className = 'canvas-container'; | ||
const controlsDiv = document.createElement('div'); | ||
controlsDiv.className = 'viewer-controls'; | ||
const fullPageNumberDiv = document.createElement('div'); | ||
fullPageNumberDiv.className = 'page-number'; | ||
const pageNumberInput = document.createElement('input'); | ||
pageNumberInput.className = 'page-number-input'; | ||
pageNumberInput.type = 'number'; | ||
const pageDiv = document.createElement('div'); | ||
const outOfDiv = document.createElement('div'); | ||
const pageCountDiv = document.createElement('div'); | ||
const nextButton = document.createElement('button'); | ||
nextButton.className = 'next-button'; | ||
const prevButton = document.createElement('button'); | ||
prevButton.className = 'prev-button'; | ||
const zoomButton = document.createElement('button'); | ||
zoomButton.className = 'zoom-button'; | ||
const loadingTask = (0, pdf_1.getDocument)(documentUrl); | ||
try { | ||
const pdfDocument = yield loadingTask.promise; | ||
containerDiv.removeChild(loadingIndicator); | ||
const isValidPage = (page) => page <= pdfDocument.numPages && page > 0; | ||
// initial viewer setup | ||
pageNumberInput.value = '1'; | ||
pageDiv.textContent = 'Page '; | ||
outOfDiv.textContent = '/'; | ||
pageCountDiv.textContent = `${pdfDocument.numPages}`; | ||
fullPageNumberDiv.appendChild(pageDiv); | ||
fullPageNumberDiv.appendChild(pageNumberInput); | ||
fullPageNumberDiv.appendChild(outOfDiv); | ||
fullPageNumberDiv.appendChild(pageCountDiv); | ||
// page container setup | ||
const pageContainer = document.createElement('div'); | ||
pageContainer.className = 'page-container'; | ||
pageContainer.style.width = defaultWidth; | ||
canvasContainer.appendChild(pageContainer); | ||
const canvas = document.createElement('canvas'); | ||
canvas.className = 'pdf-viewer-canvas'; | ||
canvas.id = `${documentId}-canvas`; | ||
pageContainer.appendChild(canvas); | ||
// text layer setup | ||
const textLayerDiv = document.createElement('div'); | ||
textLayerDiv.className = 'text-layer'; | ||
pageContainer.appendChild(textLayerDiv); | ||
nextButton.innerHTML = chevronRight; | ||
prevButton.innerHTML = chevronLeft; | ||
zoomButton.innerHTML = plusSvg; | ||
containerDiv.appendChild(controlsDiv); | ||
controlsDiv.appendChild(prevButton); | ||
controlsDiv.appendChild(fullPageNumberDiv); | ||
controlsDiv.appendChild(nextButton); | ||
controlsDiv.appendChild(zoomButton); | ||
containerDiv.appendChild(canvasContainer); | ||
const displayPage = (page) => __awaiter(void 0, void 0, void 0, function* () { | ||
pageNumberInput.value = `${page}`; | ||
const pdfPage = yield pdfDocument.getPage(page); | ||
const viewport = pdfPage.getViewport({ scale: pdfScale }); | ||
canvas.width = viewport.width; | ||
canvas.height = viewport.height; | ||
canvas.style.width = '100%'; | ||
const ctx = canvas.getContext('2d'); | ||
yield pdfPage.render({ | ||
canvasContext: ctx || {}, | ||
viewport, | ||
}); | ||
const textContent = yield pdfPage.getTextContent(); | ||
scaleTextLayer(textLayerDiv, textContent, pdfPage, canvas, viewport); | ||
zoomButton.onclick = () => { | ||
if (pageContainer.style.width === defaultWidth) { | ||
pageContainer.style.width = zoomedWidth; | ||
zoomButton.innerHTML = minusSvg; | ||
} | ||
else { | ||
pageContainer.style.width = defaultWidth; | ||
zoomButton.innerHTML = plusSvg; | ||
} | ||
scaleTextLayer(textLayerDiv, textContent, pdfPage, canvas, viewport); | ||
}; | ||
window.addEventListener('resize', () => { | ||
scaleTextLayer(textLayerDiv, textContent, pdfPage, canvas, viewport); | ||
}); | ||
}); | ||
const skipPage = (direction) => () => { | ||
const pageNumber = parseInt(pageNumberInput.value || '1'); | ||
const nextPageNumber = pageNumber + direction; | ||
if (isValidPage(nextPageNumber)) { | ||
displayPage(nextPageNumber); | ||
} | ||
}; | ||
nextButton.onclick = skipPage(1); | ||
prevButton.onclick = skipPage(-1); | ||
pageNumberInput.onchange = () => { | ||
const pageNumber = Math.max(Math.min(parseInt(pageNumberInput.value), pdfDocument.numPages), 1); | ||
displayPage(pageNumber); | ||
}; | ||
pageNumberInput.addEventListener('click', (e) => e.stopPropagation()); | ||
containerDiv.addEventListener('keydown', (e) => { | ||
e.stopPropagation(); | ||
switch (e.key) { | ||
case ('ArrowLeft'): | ||
skipPage(-1)(); | ||
return; | ||
case ('ArrowRight'): | ||
skipPage(1)(); | ||
return; | ||
default: | ||
return; | ||
} | ||
}); | ||
containerDiv.addEventListener('click', () => { | ||
containerDiv.focus(); | ||
}); | ||
containerDiv.setAttribute('tabIndex', '1'); | ||
containerDiv.focus(); | ||
yield displayPage(1); | ||
} | ||
catch (err) { | ||
handleError(containerDiv)(err); | ||
return err; | ||
} | ||
}); | ||
exports.renderPDF = renderPDF; | ||
const renderDocx = (containerDiv, documentUrl) => { | ||
@@ -219,3 +51,3 @@ const microsoftViewer = document.createElement('iframe'); | ||
new File([], 'test.txt'); | ||
(0, exports.renderPDF)(containerDiv, documentUrl); | ||
renderPDF(containerDiv, documentUrl); | ||
} | ||
@@ -258,4 +90,3 @@ catch (err) { | ||
}; | ||
const init = (workerSrc) => { | ||
pdf_1.GlobalWorkerOptions.workerSrc = workerSrc; | ||
const init = () => { | ||
loadDocuments(); | ||
@@ -262,0 +93,0 @@ window.addEventListener('load', loadDocuments); |
@@ -5,5 +5,4 @@ import React from 'react'; | ||
documentUrl: string; | ||
workerSrc: string; | ||
}; | ||
export declare const Viewer: React.FC<ViewerProps>; | ||
export {}; |
@@ -5,6 +5,4 @@ "use strict"; | ||
const react_1 = require("react"); | ||
const pdf_1 = require("pdfjs-dist/legacy/build/pdf"); | ||
const base_1 = require("./base"); | ||
const Viewer = (props) => { | ||
pdf_1.GlobalWorkerOptions.workerSrc = props.workerSrc; | ||
const viewerContainer = (0, react_1.useRef)(null); | ||
@@ -11,0 +9,0 @@ (0, react_1.useEffect)(() => { viewerContainer.current && (0, base_1.renderDocument)(viewerContainer.current); }, []); |
{ | ||
"name": "document-viewer-ts", | ||
"version": "0.3.2-legacy", | ||
"version": "0.4.0", | ||
"description": "PDF and MS Doc viewer written in TypeScript for React and vanilla JavaScript", | ||
@@ -18,7 +18,5 @@ "main": "dist/lib/index.js", | ||
"clean": "rimraf ./dist ./build", | ||
"lint:no-fix": "eslint --config='.eslintrc.js' './src/*.{ts,tsx}' './test/*.{ts,tsx}'", | ||
"lint:no-fix": "eslint --config='.eslintrc.js' './src/*.{ts,tsx}'", | ||
"lint": "npm run lint:no-fix -- --fix", | ||
"prepublishOnly": "npm run clean && npm run build && npm run test -- --ci --silent && npm run lint:no-fix -- --max-warnings 0", | ||
"test": "jest", | ||
"test:watch": "jest --watch" | ||
"prepublishOnly": "npm run clean && npm run build && npm run lint:no-fix -- --max-warnings 0" | ||
}, | ||
@@ -55,4 +53,2 @@ "repository": { | ||
"devDependencies": { | ||
"@testing-library/react-hooks": "^7.0.2", | ||
"@types/jest": "~27.4.1", | ||
"@types/react": "^16.8.0", | ||
@@ -66,11 +62,6 @@ "@types/react-dom": "^16.8.0", | ||
"eslint": "~8.10.0", | ||
"jest": "~28.1.1", | ||
"jest-environment-jsdom": "~28.1.1", | ||
"pdfjs-dist": "~2.14.305", | ||
"process": "~0.11.10", | ||
"react": "^16.9.0", | ||
"react-dom": "^16.9.0", | ||
"react-test-renderer": "~16.14.0", | ||
"smoothscroll-polyfill": "~0.4.4", | ||
"ts-jest": "~28.0.5", | ||
"ts-node": "~10.4.0", | ||
@@ -77,0 +68,0 @@ "typescript": "~4.6.2", |
@@ -9,9 +9,5 @@ | ||
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 | ||
Requires peer dependency `pdfjs-dist` at version `2.14.0` or higher: | ||
``` | ||
npm install pdfjs-dist | ||
``` | ||
Install the package: | ||
@@ -23,31 +19,5 @@ | ||
### Example Usage | ||
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" | ||
} | ||
] | ||
} | ||
] | ||
} | ||
... | ||
``` | ||
#### With HTML and Vanilla JS | ||
Call the init script in the root of your JS application using the path to your worker file. | ||
Call the init script in the root of your JS application. | ||
@@ -58,3 +28,3 @@ `index.js` | ||
init("PATH/TO/WORKER/FILE"); | ||
init(); | ||
``` | ||
@@ -81,3 +51,3 @@ | ||
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. | ||
Import the `Viewer` component and call it with the proper `documentId` and `documentUrl` props. Also make sure to import `styles.css` in your app. | ||
@@ -92,5 +62,4 @@ `index.jsx` | ||
documentUrl="https://raw.githubusercontent.com/mozilla/pdf.js/ba2edeae/web/compressed.tracemonkey-pldi-09.pdf" | ||
workerSrc="PATH/TO/WORKER/FILE" | ||
/> | ||
``` | ||
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
16
127
26367
395
61