1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- <html>
- <head>
- <script src="./pdf.js"></script>
- <script src="./common/jquery-2.1.0.js"></script>
- </head>
- <body>
- <input type="file" id="file">
- <div id="pop"></div>
- </body>
- <script>
- pdfjsLib.GlobalWorkerOptions.workerSrc = './pdf.worker.js';
- document.getElementById('file').onchange = function(event) {
- var file = event.target.files[0];
- var fileReader = new FileReader();
- fileReader.onload = function() {
- var typedarray = new Uint8Array(this.result);
- console.log(typedarray);
- const CMAP_URL = 'https://unpkg.com/pdfjs-dist@2.0.489/cmaps/';
- // pdfjsLib.GlobalWorkerOptions.workerSrc ='./pdf.worker.js';//自己的路径
- var loadingTask = pdfjsLib.getDocument({
- data: typedarray,
- cMapUrl: CMAP_URL,
- cMapPacked: true,
- });
- loadingTask.promise.then(function(pdf) {
- console.log(pdf)
- var $pop = $('#pop');
- var shownPageCount = pdf.numPages < 50 ? pdf.numPages : 50;//设置显示的编码
- var getPageAndRender = function (pageNumber) {
- pdf.getPage(pageNumber).then(function getPageHelloWorld(page) {
- var scale = 1.8;
- var viewport = page.getViewport(scale);
- var $canvas = $('<canvas></canvas>').attr({
- 'height': viewport.height,
- 'width': viewport.width,
- });
- $pop.append($canvas);
-
- page.render({
- canvasContext: $canvas[0].getContext('2d'),
- viewport: viewport
- });
- });
- if (pageNumber < shownPageCount) {
- pageNumber++;
- getPageAndRender(pageNumber);
- }
- };
- getPageAndRender(1);
- });
- };
- fileReader.readAsArrayBuffer(file);
- }
- </script>
- </html>
|