当前位置:   article > 正文

vue-pdf插件实现PDF预览功能_vue pdfpreview

vue pdfpreview

在做的Vue项目中,遇到了要上传文件,其中pdf要实现预览,网上查到vue-pdf插件可实现该功能。实现步骤如下:

1.安装插件

npm install --save vue-pdf

2.简单的demo 完整代码

  1. <template>
  2. <div>
  3. <a @click="handlePreview()" alt="查看文件" class=""><a-icon type="eye" /></a>
  4. <a-modal width='1000px' :visible="previewPdfVisible" @cancel="handlePdfCancel" @ok="handlePdfCancel">
  5. <pdf
  6. ref="pdf"
  7. :src="url"
  8. >
  9. </pdf>
  10. </a-modal>
  11. </div>
  12. </template>
  13. <script>
  14. export default {
  15. components:{
  16. pdf
  17. },
  18. data(){
  19. return {
  20. url:"",
  21. }
  22. },
  23. methods:{
  24. //查看文件
  25. handlePreview(){
  26. let fileUrl='http://10.4.4.83:9999/sys/common/static/temp/1645755049088.pdf';
  27. this.previewPdfVisible=true;
  28. this.url=fileUrl;
  29. },
  30. }
  31. }
  32. </script>
  33. <style>
  34. </style>

以上只能展示一页

3.多页展示

  1. <template>
  2. <div>
  3. <a @click="handlePreview()" alt="查看文件" class=""><a-icon type="eye" /></a>
  4. <a-modal width='1000px' :visible="previewPdfVisible" @cancel="handlePdfCancel" @ok="handlePdfCancel">
  5. <pdf
  6. ref="pdf"
  7. v-for="i in pdfNumPages"
  8. :key="i"
  9. :page="i"
  10. :src="previewPdf">
  11. >
  12. </pdf>
  13. </a-modal>
  14. </div>
  15. </template>
  16. <script>
  17. import pdf from 'vue-pdf';
  18. export default {
  19. components:{
  20. pdf
  21. },
  22. data(){
  23. return {
  24. previewPdfVisible:false,
  25. previewPdf:"",
  26. pdfNumPages: null, // pdf 总页数
  27. }
  28. },
  29. methods:{
  30. handlePdfCancel(){
  31. this.previewPdfVisible = false;
  32. },
  33. //查看文件
  34. handlePreview(){
  35. let fileUrl='http://10.4.4.83:9999/sys/common/static/temp/结算中心_1645755049088.pdf';
  36. this.previewPdfVisible=true;
  37. this.previewPdf=fileUrl;
  38. //计算pdf页码总数
  39. let loadingTask = pdf.createLoadingTask(this.previewPdf)
  40. loadingTask.promise.then(pdf => {
  41. this.pdfNumPages = pdf.numPages
  42. }).catch(err => {
  43. console.error('pdf 加载失败', err);
  44. })
  45. },
  46. }
  47. }
  48. </script>
  49. <style>
  50. </style>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/880288
推荐阅读
相关标签