• nuxt PDF world 预览


    nuxt vue-pdf 使用过程中遇到的踩坑过程。

    推荐一个非常详细的vue-pdf 博客
    Vue PDF文件预览vue-pdf 详解

    Vue PDF文件预览

    安装

    npm install vue-pdf -D
    

    写插件js,plugins目录下新建pdf.js文件

    import Vue from 'vue'
    import pdf from 'vue-pdf'
    Vue.component('PDFComponent', pdf)
    export default function ({ app, redirect }, inject) {
      inject('pdf', pdf);
    }
    

    .nuxt.config.js中添加引入该插件

     plugins: [{ src: '~/plugins/pdf.js', ssr: false }]
    

    在页面或者组件里面可以直接使用PDF

    var pdfUrl = this.$pdf.createLoadingTask('xxx.pdf');
    

    html

    <PDFComponent :url="url"></PDFComponent>
    

    此处不能直接在页面或者组件里面直接引入 PDF ,如果直接引入PDF组件,会报错的。一定要通过插件的方式才能使用 PDF

    通过上面的方式引入文件在预览PDF的时候还是会报错的。

    使用vue-pdf插件导致 Cannot read properties of undefined (reading 'catch')解决办法

    /node_modules/vue-pdf/src/pdfjsWrapper.js
    通过以下路径找到 pdfjsWrapper.js文件并把全部代码修改为下面的代码就解决啦!

    import { PDFLinkService } from 'pdfjs-dist/lib/web/pdf_link_service';
     
    var pendingOperation = Promise.resolve();
     
    export default function(PDFJS) {
     
    	function isPDFDocumentLoadingTask(obj) {
     
    		return typeof(obj) === 'object' && obj !== null && obj.__PDFDocumentLoadingTask === true;
    	}
     
    	function createLoadingTask(src, options) {
    		let CMAP_URL = 'https://unpkg.com/pdfjs-dist@2.0.943/cmaps/'
    		var source;
    		if ( typeof(src) === 'string' )
    			source = { url: src };
    		else if ( src instanceof Uint8Array )
    			source = { data: src };
    		else if ( typeof(src) === 'object' && src !== null )
    			source = Object.assign({}, src);
    		else
    			throw new TypeError('invalid src type');
     
    		// source.verbosity = PDFJS.VerbosityLevel.INFOS;
    		// source.pdfBug = true;
    		// source.stopAtErrors = true;
    		source.cMapUrl=CMAP_URL,
    		source.cMapPacked=true
    		var loadingTask = PDFJS.getDocument(source);
    		loadingTask.__PDFDocumentLoadingTask = true; // since PDFDocumentLoadingTask is not public
     
    		if ( options && options.onPassword )
    			loadingTask.onPassword = options.onPassword;
     
    		if ( options && options.onProgress )
    			loadingTask.onProgress = options.onProgress;
     
    		return loadingTask;
    	}
     
     
    	function PDFJSWrapper(canvasElt, annotationLayerElt, emitEvent) {
     
    		var pdfDoc = null;
    		var pdfPage = null;
    		var pdfRender = null;
    		var canceling = false;
     
    		canvasElt.getContext('2d').save();
     
    		function clearCanvas() {
     
    			canvasElt.getContext('2d').clearRect(0, 0, canvasElt.width, canvasElt.height);
    		}
     
    		function clearAnnotations() {
     
    			while ( annotationLayerElt.firstChild )
    				annotationLayerElt.removeChild(annotationLayerElt.firstChild);
    		}
     
    		this.destroy = function() {
     
    			if ( pdfDoc === null )
    				return;
     
    			// Aborts all network requests and destroys worker.
    			pendingOperation = pdfDoc.destroy();
    			pdfDoc = null;
    		}
     
    		this.getResolutionScale = function() {
     
    			return canvasElt.offsetWidth / canvasElt.width;
    		}
     
    		this.printPage = function(dpi, pageNumberOnly) {
     
    			if ( pdfPage === null )
    				return;
     
    			// 1in == 72pt
    			// 1in == 96px
    			var PRINT_RESOLUTION = dpi === undefined ? 150 : dpi;
    			var PRINT_UNITS = PRINT_RESOLUTION / 72.0;
    			var CSS_UNITS = 96.0 / 72.0;
     
    			var iframeElt = document.createElement('iframe');
     
    			// function removePrintContainer() {
     
    			// 	iframeElt.parentNode.removeChild(iframeElt);
    			// }
    			function removePrintContainer() {
    				printContainerElement.parentNode.removeChild(printContainerElement);
    		  }
     
    			new Promise(function(resolve, reject) {
     
    				iframeElt.frameBorder = '0';
    				iframeElt.scrolling = 'no';
    				iframeElt.width = '0px;'
    				iframeElt.height = '0px;'
    				iframeElt.style.cssText = 'position: absolute; top: 0; left: 0';
     
    				iframeElt.onload = function() {
     
    					resolve(this.contentWindow);
    				}
     
    				window.document.body.appendChild(iframeElt);
    			})
    			.then(function(win) {
     
    				win.document.title = '';
     
    				return pdfDoc.getPage(1)
    				.then(function(page) {
     
    					var viewport = page.getViewport({ scale: 1 });
    					win.document.head.appendChild(win.document.createElement('style')).textContent =
    						'@supports ((size:A4) and (size:1pt 1pt)) {' +
    							'@page { margin: 1pt; size: ' + ((viewport.width * PRINT_UNITS) / CSS_UNITS) + 'pt ' + ((viewport.height * PRINT_UNITS) / CSS_UNITS) + 'pt; }' +
    						'}' +
     
    						'@media print {' +
    							'body { margin: 0 }' +
    							'canvas { page-break-before: avoid; page-break-after: always; page-break-inside: avoid }' +
    						'}'+
     
    						'@media screen {' +
    							'body { margin: 0 }' +
    						'}'+
     
    						''
    					return win;
    				})
    			})
    			.then(function(win) {
     
    				var allPages = [];
     
    				for ( var pageNumber = 1; pageNumber <= pdfDoc.numPages; ++pageNumber ) {
     
    					if ( pageNumberOnly !== undefined && pageNumberOnly.indexOf(pageNumber) === -1 )
    						continue;
     
    					allPages.push(
    						pdfDoc.getPage(pageNumber)
    						.then(function(page) {
     
    							var viewport = page.getViewport({ scale: 1 });
     
    							var printCanvasElt = win.document.body.appendChild(win.document.createElement('canvas'));
    							printCanvasElt.width = (viewport.width * PRINT_UNITS);
    							printCanvasElt.height = (viewport.height * PRINT_UNITS);
    							return page.render({
    								canvasContext: printCanvasElt.getContext('2d'),
    								transform: [ // Additional transform, applied just before viewport transform.
    									PRINT_UNITS, 0, 0,
    									PRINT_UNITS, 0, 0
    								],
    								viewport: viewport,
    								intent: 'print'
    							}).promise;
    						})
    					);
    				}
     
    				Promise.all(allPages)
    				.then(function() {
     
    					win.focus(); // Required for IE
    					if (win.document.queryCommandSupported('print')) {
    						win.document.execCommand('print', false, null);
    						} else {
    						win.print();
    					  }
    					removePrintContainer();
    				})
    				.catch(function(err) {
     
    					removePrintContainer();
    					emitEvent('error', err);
    				})
    			})
    		}
     
    		this.renderPage = function(rotate) {
    			if ( pdfRender !== null ) {
     
    				if ( canceling )
    					return;
    				canceling = true;
    				pdfRender.cancel();
    				return;
    			}
     
    			if ( pdfPage === null )
    				return;
    			// rotate = (pdfPage.rotate === undefined ? 0 : pdfPage.rotate) + (rotate === undefined ? 0 : rotate);
    			rotate = rotate === undefined ? 0 : rotate
    			var scale = canvasElt.offsetWidth / pdfPage.getViewport({ scale: 1 }).width * (window.devicePixelRatio || 1);
    			var viewport = pdfPage.getViewport({ scale:scale, rotation:rotate });
     
    			emitEvent('page-size', viewport.width, viewport.height);
     
    			canvasElt.width = viewport.width;
    			canvasElt.height = viewport.height;
    			pdfRender = pdfPage.render({
    				canvasContext: canvasElt.getContext('2d'),
    				viewport: viewport
    			});
     
    			annotationLayerElt.style.visibility = 'hidden';
    			clearAnnotations();
     
    			var viewer = {
    				scrollPageIntoView: function(params) {
    					emitEvent('link-clicked', params.pageNumber)
    				},
    			};
     
    			var linkService = new PDFLinkService();
    			linkService.setDocument(pdfDoc);
    			linkService.setViewer(viewer);
     
    			pendingOperation = pendingOperation.then(function() {
     
    				var getAnnotationsOperation =
    				pdfPage.getAnnotations({ intent: 'display' })
    				.then(function(annotations) {
     
    					PDFJS.AnnotationLayer.render({
    						viewport: viewport.clone({ dontFlip: true }),
    						div: annotationLayerElt,
    						annotations: annotations,
    						page: pdfPage,
    						linkService: linkService,
    						renderInteractiveForms: false
    					});
    				});
     
    				var pdfRenderOperation =
    				pdfRender.promise
    				.then(function() {
     
    					annotationLayerElt.style.visibility = '';
    					canceling = false;
    					pdfRender = null;
    				})
    				.catch(function(err) {
     
    					pdfRender = null;
    					if ( err instanceof PDFJS.RenderingCancelledException ) {
     
    						canceling = false;
    						this.renderPage(rotate);
    						return;
    					}
    					emitEvent('error', err);
    				}.bind(this))
     
    				return Promise.all([getAnnotationsOperation, pdfRenderOperation]);
    			}.bind(this));
    		}
     
     
    		this.forEachPage = function(pageCallback) {
     
    			var numPages = pdfDoc.numPages;
     
    			(function next(pageNum) {
     
    				pdfDoc.getPage(pageNum)
    				.then(pageCallback)
    				.then(function() {
     
    					if ( ++pageNum <= numPages )
    						next(pageNum);
    				})
    			})(1);
    		}
     
     
    		this.loadPage = function(pageNumber, rotate) {
    			pdfPage = null;
    			if ( pdfDoc === null )
    				return;
    			pendingOperation = pendingOperation.then(function() {
    				return pdfDoc.getPage(pageNumber);
    			})
    			.then(function(page) {
    				pdfPage = page;
    				this.renderPage(rotate);
    				emitEvent('page-loaded', page.pageNumber);
    			}.bind(this))
    			.catch(function(err) {
     
    				clearCanvas();
    				clearAnnotations();
    				emitEvent('error', err);
    			});
    		}
     
    		this.loadDocument = function(src) {
     
    			pdfDoc = null;
    			pdfPage = null;
     
    			emitEvent('num-pages', undefined);
     
    			if ( !src ) {
     
    				canvasElt.removeAttribute('width');
    				canvasElt.removeAttribute('height');
    				clearAnnotations();
    				return;
    			}
     
    			// wait for pending operation ends
    			pendingOperation = pendingOperation.then(function() {
     
    				var loadingTask;
    				if ( isPDFDocumentLoadingTask(src) ) {
     
    					if ( src.destroyed ) {
     
    						emitEvent('error', new Error('loadingTask has been destroyed'));
    						return
    					}
     
    					loadingTask = src;
    				} else {
     
    					loadingTask = createLoadingTask(src, {
    						onPassword: function(updatePassword, reason) {
     
    							var reasonStr;
    							switch (reason) {
    								case PDFJS.PasswordResponses.NEED_PASSWORD:
    									reasonStr = 'NEED_PASSWORD';
    									break;
    								case PDFJS.PasswordResponses.INCORRECT_PASSWORD:
    									reasonStr = 'INCORRECT_PASSWORD';
    									break;
    							}
    							emitEvent('password', updatePassword, reasonStr);
    						},
    						onProgress: function(status) {
     
    							var ratio = status.loaded / status.total;
    							emitEvent('progress', Math.min(ratio, 1));
    						}
    					});
    				}
     
    				return loadingTask.promise;
    			})
    			.then(function(pdf) {
     
    				pdfDoc = pdf;
    				emitEvent('num-pages', pdf.numPages);
    				emitEvent('loaded');
    			})
    			.catch(function(err) {
     
    				clearCanvas();
    				clearAnnotations();
    				emitEvent('error', err);
    			})
    		}
     
    		annotationLayerElt.style.transformOrigin = '0 0';
    	}
     
    	return {
    		createLoadingTask: createLoadingTask,
    		PDFJSWrapper: PDFJSWrapper,
    	}
    }
    

    通过修改源码后。PDF预览功能就可以正常使用了。

    预览方式有两种。用<iframe></iframe> 来加载要预览的PDF文件。
    另外一种是通过PDF 组件的方式来实现。具体看自己的需要。

  • 相关阅读:
    MongoDB对集合分片
    将本地项目导入到PyCharm中
    安装docker-compose报错
    MySQL定时删除按日分表或者按月分表的表
    使用Navicat连接oracle
    MySQL Error_code: 1756
    Python logging模块
    PyCharm添加Liunx开发环境进行本地开发
    Java向上保留两位小数
    JAVA计算字符串UTF-8字节数
  • 原文地址:https://www.cnblogs.com/boyGdm/p/16408252.html
Copyright © 2020-2023  润新知