安装pdfjs-dist

npm install pdf-dist --save //我安装的版本是^2.0.943

引入pdfjs-dist

import * as pdfjsLib from ‘pdfjs-dist/legacy/build/pdf.js’
window.pdfjsWorker=require(‘pdfjs-dist/build/pdf.worker’) // 重点!!!!

代码

html:

      <div ref="pdfContainer">
        <div  v-loading="loading"></div>
      </div>

script

  data() {
    return {
     	loading:true
    	}
   },
    mounted() {
    	this.previewPDF("http://xxxx/test.pdf")
  	},
 	methods: {
 	 async previewPDF(pdfUrl) {
      const container = this.$refs.pdfContainer;
      const loadingTask = pdfjsLib.getDocument(pdfUrl);
      const pdf = await loadingTask.promise;
      const numPages = pdf.numPages;
      for (let i = 1; i <= numPages; i++) {
        const page = await pdf.getPage(i);
        const canvas = document.createElement('canvas');
        container.appendChild(canvas);
        const context = canvas.getContext('2d');
        const viewport = page.getViewport({ scale: 1 });
        canvas.width = viewport.width;
        canvas.height = viewport.height;
        const renderContext = {
          canvasContext: context,
          viewport: viewport
        };
        await page.render(renderContext).promise;
        this.loading = false
      }
    },
  }
GitHub 加速计划 / vu / vue
207.53 K
33.66 K
下载
vuejs/vue: 是一个用于构建用户界面的 JavaScript 框架,具有简洁的语法和丰富的组件库,可以用于开发单页面应用程序和多页面应用程序。
最近提交(Master分支:1 个月前 )
73486cb5 * chore: fix link broken Signed-off-by: snoppy <michaleli@foxmail.com> * Update packages/template-compiler/README.md [skip ci] --------- Signed-off-by: snoppy <michaleli@foxmail.com> Co-authored-by: Eduardo San Martin Morote <posva@users.noreply.github.com> 3 个月前
e428d891 Updated Browser Compatibility reference. The previous currently returns HTTP 404. 4 个月前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐