textlayer_pdfjs.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /* Copyright 2014 Mozilla Foundation
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. 'use strict';
  16. if (!pdfjsLib.getDocument || !pdfjsViewer.PDFPageView) {
  17. alert('Please build the pdfjs-dist library using\n' +
  18. ' `gulp dist-install`');
  19. }
  20. var DEFAULT_URL = {{og_filename}};
  21. var PAGE_TO_VIEW = 1;
  22. var SCALE = 1.0;
  23. var container = document.getElementById('pageContainer');
  24. // Loading document.
  25. var loadingTask = pdfjsLib.getDocument({
  26. url: DEFAULT_URL,
  27. cMapUrl: CMAP_URL,
  28. cMapPacked: CMAP_PACKED,
  29. });
  30. loadingTask.promise.then(function(pdfDocument) {
  31. // Document loaded, retrieving the page.
  32. return pdfDocument.getPage(PAGE_TO_VIEW).then(function (pdfPage) {
  33. // Creating the page view with default parameters.
  34. var pdfPageView = new pdfjsViewer.PDFPageView({
  35. container: container,
  36. id: PAGE_TO_VIEW,
  37. scale: SCALE,
  38. defaultViewport: pdfPage.getViewport({ scale: SCALE, }),
  39. // We can enable text/annotations layers, if needed
  40. textLayerFactory: new pdfjsViewer.DefaultTextLayerFactory(),
  41. annotationLayerFactory: new pdfjsViewer.DefaultAnnotationLayerFactory(),
  42. });
  43. // Associates the actual page with the view, and drawing it
  44. pdfPageView.setPdfPage(pdfPage);
  45. return pdfPageView.draw();
  46. });
  47. });