-
Notifications
You must be signed in to change notification settings - Fork 31
/
index.tsx
executable file
·321 lines (309 loc) · 10.4 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
import * as React from "react";
import * as CSSModules from "react-css-modules";
import * as styles from "./index.less";
import * as pdfjsLib from "pdfjs-dist";
const pdfjsViewer = require("../../../node_modules/pdfjs-dist/web/pdf_viewer.js");
// The workerSrc property shall be specified.
pdfjsLib.GlobalWorkerOptions.workerSrc = "https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.0.550/pdf.worker.js";
// default scale
const DEFAULT_MIN_SCALE = 0.25;
const DEFAULT_MAX_SCALE = 10.0;
let USE_ONLY_CSS_ZOOM = true;
let TEXT_LAYER_MODE = 0; // DISABLE
let MAX_IMAGE_SIZE = 1024 * 1024;
let CMAP_PACKED = true;
let DEFAULT_URL = "/test.pdf";
let DEFAULT_SCALE_DELTA = 1.1;
let MIN_SCALE = DEFAULT_MIN_SCALE;
let MAX_SCALE = DEFAULT_MAX_SCALE;
let DEFAULT_SCALE_VALUE: string|number = "auto"; // in order to be responsive
interface IProps {
url: string|object;
page?: number|string;
scale?: number|string;
onDocumentComplete?: any;
minScale?: number;
maxScale?: number;
isShowHeader?:boolean;
isShowFooter?:boolean;
}
interface IStates {
currentPageNumber: any;
currentScaleValue: any;
totalPage: number|string;
title: string;
}
@CSSModules(styles)
export class MobilePDFReader extends React.Component<IProps,IStates> {
state: IStates = {
currentPageNumber: 1,
currentScaleValue: "auto",
totalPage: null,
title: ""
};
pdfLoadingTask: any;
pdfViewer: any;
pdfDocument: any;
pdfHistory: any;
pdfLinkService: any;
container: any;
l10n: any;
error: any;
documentInfo: any;
metadata: any;
public constructor (props: IProps) {
super(props);
this.pdfLoadingTask = null;
this.pdfDocument = null;
this.pdfViewer = {
currentScaleValue: null
},
this.pdfHistory = null;
this.pdfLinkService = null;
this.container = React.createRef();
}
get pagesCount () {
return this.pdfDocument.numPages;
}
get page () {
if (this.pdfViewer != null) {
return this.pdfViewer.currentPageNumber;
} else {
return 1;
}
}
get loadingBar () {
let bar = new pdfjsViewer.ProgressBar("#loadingBar", {});
return pdfjsLib.shadow(this, "loadingBar", bar);
}
private open (params) {
let url = params.url
let self = this
this.setTitleUsingUrl(url)
// Loading document.
let loadingTask = pdfjsLib.getDocument({
url: url,
withCredentials: true,
maxImageSize: MAX_IMAGE_SIZE,
cMapPacked: CMAP_PACKED
})
this.pdfLoadingTask = loadingTask
loadingTask.onProgress = function (progressData) {
self.progress(progressData.loaded / progressData.total)
}
return loadingTask.promise.then(function (pdfDocument) {
// Document loaded, specifying document for the viewer.
self.pdfDocument = pdfDocument;
self.pdfViewer.setDocument(pdfDocument)
self.pdfLinkService.setDocument(pdfDocument)
self.pdfHistory.initialize(pdfDocument.fingerprint)
self.loadingBar.hide()
self.setTitleUsingMetadata(pdfDocument)
}, function (exception) {
let message = exception && exception.message
let l10n = self.l10n
let loadingErrorMessage
if (exception instanceof pdfjsLib.InvalidPDFException) {
// change error message also for other builds
loadingErrorMessage = l10n.get('invalid_file_error', null,
'Invalid or corrupted PDF file.')
} else if (exception instanceof pdfjsLib.MissingPDFException) {
// special message for missing PDFs
loadingErrorMessage = l10n.get('missing_file_error', null,
'Missing PDF file.')
} else if (exception instanceof pdfjsLib.UnexpectedResponseException) {
loadingErrorMessage = l10n.get('unexpected_response_error', null,
'Unexpected server response.')
} else {
loadingErrorMessage = l10n.get('loading_error', null,
'An error occurred while loading the PDF.')
}
loadingErrorMessage.then(function (msg) {
})
self.loadingBar.hide()
})
}
private setTitleUsingUrl (url) {
let title = pdfjsLib.getFilenameFromUrl(url) || url;
try {
title = decodeURIComponent(title);
} catch (e) {
// decodeURIComponent may throw URIError,
// fall back to using the unprocessed url in that case
}
this.setTitle(title);
}
private setTitleUsingMetadata (pdfDocument) {
let self = this ;
return pdfDocument.getMetadata().then(function (data) {
let info = data.info; let metadata = data.metadata;
self.documentInfo = info ;
self.metadata = metadata;
// Provides some basic debug information
console.log("PDF " + pdfDocument.fingerprint + " [" +
info.PDFFormatVersion + " " + (info.Producer || "-").trim() +
" / " + (info.Creator || "-").trim() + "]" +
" (PDF.js: " + (pdfjsLib.version || "-") + ")");
let pdfTitle;
if (metadata && metadata.has("dc:title")) {
let title = metadata.get("dc:title");
// Ghostscript sometimes returns 'Untitled', so prevent setting the
// title to 'Untitled.
if (title !== "Untitled") {
pdfTitle = title;
}
}
if (!pdfTitle && info && info["Title"]) {
pdfTitle = info["Title"];
}
if (pdfTitle) {
self.setTitle(pdfTitle + " - " + document.title);
}
return {title: pdfTitle, documentInfo: info};
});
}
private setTitle (title) {
this.setState({title});
}
private progress (level) {
let percent = Math.round(level * 100);
// Updating the bar if value increases.
if (percent > this.loadingBar.percent || isNaN(percent)) {
this.loadingBar.percent = percent;
}
}
private initUI () {
let linkService = new pdfjsViewer.PDFLinkService();
const self = this;
const { scale, page, onDocumentComplete } = self.props;
this.pdfLinkService = linkService;
this.l10n = pdfjsViewer.NullL10n;
let container = this.container.current;
let pdfViewer = new pdfjsViewer.PDFViewer({
container: container,
linkService: linkService,
l10n: this.l10n,
useOnlyCssZoom: USE_ONLY_CSS_ZOOM,
textLayerMode: TEXT_LAYER_MODE
});
this.pdfViewer = pdfViewer;
linkService.setViewer(pdfViewer);
this.pdfHistory = new pdfjsViewer.PDFHistory({
linkService: linkService
});
linkService.setHistory(this.pdfHistory);
container.addEventListener("pagesinit", function () {
// We can use pdfViewer now, e.g. let's change default scale.
// deal with the init page in the props
if (scale) {
DEFAULT_SCALE_VALUE = scale;
}
if (page) {
pdfViewer.currentPageNumber = page;
}
pdfViewer.currentScaleValue = DEFAULT_SCALE_VALUE;
self.setState({totalPage: self.pdfDocument.numPages});
});
container.addEventListener("pagechange", function (evt) {
let page = evt.pageNumber;
self.setState({ currentPageNumber: page });
});
}
private zoomIn = (ticks) => {
let newScale = this.pdfViewer.currentScale;
do {
newScale = (newScale * DEFAULT_SCALE_DELTA).toFixed(2);
newScale = Math.ceil(newScale * 10) / 10;
newScale = Math.min(MAX_SCALE, newScale);
} while (--ticks && newScale < MAX_SCALE);
this.pdfViewer.currentScaleValue = newScale;
}
private zoomOut = (ticks) => {
let newScale = this.pdfViewer.currentScale;
do {
newScale = (newScale / DEFAULT_SCALE_DELTA).toFixed(2);
newScale = Math.floor(newScale * 10) / 10;
newScale = Math.max(MIN_SCALE, newScale);
} while (--ticks && newScale > MIN_SCALE);
this.pdfViewer.currentScaleValue = newScale;
}
private pageAdd = () => {
if (this.pdfViewer.currentPageNumber > this.pdfDocument.numPages) {
return;
}
this.pdfViewer.currentPageNumber++;
}
private pageDelete = () => {
if (this.pdfViewer.currentPageNumber < 1) {
return;
}
this.pdfViewer.currentPageNumber--;
}
public componentDidMount () {
const { url, minScale, maxScale} = this.props ;
// deal with the props if include minScale or maxScale
if (minScale) {
MIN_SCALE = minScale ;
}
if (maxScale) {
MAX_SCALE = maxScale ;
}
this.initUI();
this.open({
url
});
}
public render(){
const { title } = this.state;
const { isShowHeader,isShowFooter } = this.props;
let showHeader = true;
let showFooter = true;
if(isShowHeader!==undefined){
showHeader = isShowHeader;
}
if(isShowFooter!==undefined){
showFooter = isShowFooter;
}
return <div className='mobile__pdf__container'>
{
showHeader&&<header className="mobile__pdf__container__header">
{title}
</header>
}
<div id="viewerContainer" ref={this.container}>
<div id="viewer" className="pdfViewer" ></div>
</div>
<div id="loadingBar">
<div className="progress"></div>
<div className="glimmer"></div>
</div>
<div id="errorWrapper" hidden={true}>
<div id="errorMessageLeft">
<span id="errorMessage"></span>
<button id="errorShowMore">
More Information
</button>
<button id="errorShowLess">
Less Information
</button>
</div>
<div id="errorMessageRight">
<button id="errorClose">
Close
</button>
</div>
<div className="clearBoth"></div>
<textarea id="errorMoreInfo" hidden={true} readOnly={true}></textarea>
</div>
{
showFooter&&<footer>
<button className="toolbarButton pageUp" title="Previous Page" id="previous" onClick={this.pageDelete}></button>
<button className="toolbarButton pageDown" title="Next Page" id="next" onClick={this.pageAdd}></button>
<input type="number" id="pageNumber" className="toolbarField pageNumber" value={this.state.currentPageNumber} size={4} min={1} />
<button className="toolbarButton zoomOut" title="Zoom Out" id="zoomOut" onClick={this.zoomOut}></button>
<button className="toolbarButton zoomIn" title="Zoom In" id="zoomIn" onClick={this.zoomIn}></button>
</footer>
}
</div>
}
}