pdf.js/examples/components/simpleviewer.js

75 lines
2.2 KiB
JavaScript
Raw Normal View History

2014-10-01 02:22:38 +09:00
/* Copyright 2014 Mozilla Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
2018-12-06 21:55:15 +09:00
if (!pdfjsLib.getDocument || !pdfjsViewer.PDFViewer) {
2016-04-29 03:30:03 +09:00
alert('Please build the pdfjs-dist library using\n' +
' `gulp dist-install`');
2014-10-01 02:22:38 +09:00
}
// The workerSrc property shall be specified.
2014-10-01 02:22:38 +09:00
//
pdfjsLib.GlobalWorkerOptions.workerSrc =
'../../node_modules/pdfjs-dist/build/pdf.worker.js';
2014-10-01 02:22:38 +09:00
2014-10-01 05:42:28 +09:00
// Some PDFs need external cmaps.
2014-10-01 02:22:38 +09:00
//
var CMAP_URL = '../../node_modules/pdfjs-dist/cmaps/';
var CMAP_PACKED = true;
2014-10-01 02:22:38 +09:00
var DEFAULT_URL = '../../web/compressed.tracemonkey-pldi-09.pdf';
2016-04-14 04:39:29 +09:00
var SEARCH_FOR = ''; // try 'Mozilla';
2014-10-01 02:22:38 +09:00
2014-10-01 02:31:58 +09:00
var container = document.getElementById('viewerContainer');
// (Optionally) enable hyperlinks within PDF files.
var pdfLinkService = new pdfjsViewer.PDFLinkService();
// (Optionally) enable find controller.
var pdfFindController = new pdfjsViewer.PDFFindController({
linkService: pdfLinkService,
2014-10-01 02:31:58 +09:00
});
var pdfViewer = new pdfjsViewer.PDFViewer({
container: container,
linkService: pdfLinkService,
findController: pdfFindController,
2016-04-14 04:39:29 +09:00
});
pdfLinkService.setViewer(pdfViewer);
2016-04-14 04:39:29 +09:00
document.addEventListener('pagesinit', function () {
// We can use pdfViewer now, e.g. let's change default scale.
2014-10-01 02:31:58 +09:00
pdfViewer.currentScaleValue = 'page-width';
2016-04-14 04:39:29 +09:00
if (SEARCH_FOR) { // We can try search for things
pdfFindController.executeCommand('find', { query: SEARCH_FOR, });
2016-04-14 04:39:29 +09:00
}
2014-10-01 02:22:38 +09:00
});
// Loading document.
var loadingTask = pdfjsLib.getDocument({
url: DEFAULT_URL,
cMapUrl: CMAP_URL,
cMapPacked: CMAP_PACKED,
});
loadingTask.promise.then(function(pdfDocument) {
// Document loaded, specifying document for the viewer and
// the (optional) linkService.
2014-10-01 02:22:38 +09:00
pdfViewer.setDocument(pdfDocument);
pdfLinkService.setDocument(pdfDocument, null);
2014-10-01 02:22:38 +09:00
});