当前位置:   article > 正文

vue.js2:pdf文件转图片,打包压缩下载_vue2 pdf转图片

vue2 pdf转图片

目录

所需工具

工具下载

代码实现

效果


所需工具

1、pdfjs-dist(负责API解析,可将pdf文件渲染成canvas实现预览)

2、pdfjsWorker(负责核心解析)

3、JSZip(将图片打包成生成.zip文件)

4、FileSaver(保存下载zip文件)

工具下载

  1. npm install --save pdfjs-dist
  2. npm install jszip --save
  3. npm install file-saver --save

代码实现

  1. <template>
  2. <div style="width: 100%;margin: auto;display: flex;flex-direction: column;align-items: center;">
  3. <div style="width:800px;text-shadow: 2px 2px 5px #777;
  4. line-height: 28px;font-size: 28px;color: rgb(90, 90, 90);
  5. margin-top: 20px;">
  6. pdf转图片
  7. </div>
  8. <div style="width: 800px;margin-top: 20px;">
  9. <div>
  10. 选择pdf:
  11. <input type="file" ref="hiddenfile" accept="application/pdf" @change="handlePdf" class="hiddenInput" />
  12. </div>
  13. <div style="width:800px;font-size:12px;margin-top:10px;">单击图片可下载一页</div>
  14. <div style="width:800px;font-size:12px;margin-top:10px;">
  15. <button type="info" plain style="width:400px;" @click="downJpgAll">全部图片打zip包下载</button>
  16. </div>
  17. <div v-for="(item, i) in imgFiles" :key="i" style="width:800px;position: relative;margin-top:10px;">
  18. <canvas :id="`pdf_canvas_${item}`" style="border: 1px solid #eeeeee;float:left;"
  19. @click="downJpg(item)"></canvas>
  20. </div>
  21. </div>
  22. </div>
  23. </template>
  24. <script>
  25. import * as pdfjs from 'pdfjs-dist'
  26. import * as pdfjsWorker from 'pdfjs-dist/build/pdf.worker.entry'
  27. import JSZip from 'jszip'
  28. import FileSaver from "file-saver"
  29. pdfjs.GlobalWorkerOptions.workerSrc = pdfjsWorker
  30. export default {
  31. data() {
  32. return {
  33. imgFiles: [],
  34. pdfName: '',
  35. }
  36. },
  37. methods: {
  38. // 处理pdf
  39. handlePdf(evt) {
  40. let that = this;
  41. // console.log(evt);
  42. let file = evt.target.files[0];
  43. console.log(file);
  44. console.log(file.name);
  45. this.pdfName = file.name.substring(0, file.name.lastIndexOf("."));
  46. console.log(this.pdfName);
  47. var reader = new FileReader();
  48. reader.readAsDataURL(file); //将文件读取为 DataURL
  49. reader.onload = function () { //文件读取成功完成时触发
  50. const loadingTask = pdfjs.getDocument(reader.result)
  51. loadingTask.promise.then((pdf) => {
  52. var pageNum = pdf.numPages;
  53. console.log("页数:" + pageNum);
  54. //准备图片
  55. for (let i = 1; i <= pageNum; i++) {
  56. let one = i;
  57. that.imgFiles.push(one);
  58. }
  59. //处理
  60. for (let i = 1; i <= pageNum; i++) {
  61. //var canvas = document.createElement("canvas");
  62. //canvas.id = "pageNum" + i;
  63. pdf.getPage(i).then((page) => {
  64. const canvas = document.getElementById('pdf_canvas_' + i);
  65. const ctx = canvas.getContext('2d')
  66. const viewport = page.getViewport({ scale: 4 })
  67. console.log(viewport);
  68. // 画布大小,默认值是width:300px,height:150px
  69. canvas.height = viewport.height;
  70. canvas.width = viewport.width;
  71. // 画布的dom大小, 设置移动端,宽度设置铺满整个屏幕
  72. //const clientWidth = document.body.clientWidth;
  73. const destWidth = 398;
  74. canvas.style.width = destWidth + 'px';
  75. // 根据pdf每页的宽高比例设置canvas的高度
  76. canvas.style.height = destWidth * (viewport.height / viewport.width) + 'px';
  77. page.render({
  78. canvasContext: ctx,
  79. viewport,
  80. })
  81. })
  82. //加载完成
  83. }// end for
  84. })
  85. }
  86. },
  87. // 单图下载图片
  88. downJpg(i) {
  89. var canvas = document.getElementById("pdf_canvas_" + i);
  90. var oA = document.createElement("a");
  91. oA.download = this.pdfName + "_" + i + '.jpg';// 设置下载的文件名,默认是'下载'
  92. oA.href = canvas.toDataURL("image/jpeg");
  93. //oA.className = "qrcode"
  94. document.body.appendChild(oA);
  95. oA.click();
  96. oA.remove(); // 下载之后把创建的元素删除
  97. },
  98. // 全部下载图片
  99. downJpgAll() {
  100. let blogTitle = this.pdfName;
  101. let that = this;
  102. let zip = new JSZip();
  103. let images = zip.folder('images');
  104. // 要下载图片的url;
  105. this.imgFiles.forEach((item, index, arr) => {
  106. console.log('图片', item);
  107. var canvas = document.getElementById("pdf_canvas_" + item);
  108. images.file(that.pdfName + "-" + (index + 1) + ".png", that.splitBase64(canvas.toDataURL("image/png", 1.0)), {
  109. base64: true
  110. });
  111. });
  112. zip.generateAsync({ type: 'blob' }).then(function (content) {
  113. // see FileSaver.js
  114. FileSaver.saveAs(content, blogTitle + '.zip');
  115. });
  116. },
  117. // 去掉base64编码前的 data:image/png;base64,
  118. splitBase64(dataurl) {
  119. var arr = dataurl.split(',')
  120. return arr[1]
  121. }
  122. }
  123. }
  124. </script>
  125. <style>
  126. * {
  127. margin: 0;
  128. padding: 0;
  129. }
  130. </style>

效果展示

录制_2022_09_05_16_39_43_647

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

闽ICP备14008679号