当前位置:   article > 正文

前端vue展示PDFbase64_vue将pdf(base64)在前端展示

vue将pdf(base64)在前端展示
  1. <template>
  2. <div class="h5pdf">
  3. <div class="ljsx" v-if="!isSX">
  4. <img class="ljsxtp" src="../../assets/h5sj/ljsx.png">
  5. <div class="ljsxwz" v-html="yctip">
  6. </div>
  7. </div>
  8. <div v-if="isSX">
  9. <div class="catips">
  10. <div class="catipswz">{{pdfToptips}}</div>
  11. </div>
  12. <canvas class="cvs" ref="img" :id="'the-canvas'+this.page" :key="this.page">></canvas>
  13. <img class="img" :src="imgSrc">
  14. <footer>
  15. <el-button class="syyBut"
  16. @click="subpage">上一页
  17. </el-button>
  18. <el-button class="ymxs">{{this.page+'/'+this.pdfList}}
  19. </el-button>
  20. <el-button class="xyyBut"
  21. @click="addpage">下一页
  22. </el-button>
  23. </footer>
  24. </div>
  25. </div>
  26. </template>
  27. <script>
  28. // eslint-disable-next-line import/no-duplicates
  29. import PDFJS from 'pdfjs-dist'
  30. // eslint-disable-next-line import/no-duplicates
  31. import pdfjsLib from 'pdfjs-dist'
  32. import html2canvas from 'html2canvas'
  33. // eslint-disable-next-line no-unused-vars
  34. import { zrrJfqdEwmPDF } from '../../api/jfzm/jfzm-api'
  35. export default {
  36. data () {
  37. return {
  38. pdfDoc: null,
  39. pdfList: 0,
  40. page: 1,
  41. is_img_show: false,
  42. imgSrc: null,
  43. isEnlarge: false,
  44. enlargeWidth: screen.availWidth,
  45. isSX: false,
  46. base64StrPDF: 'JVBERi0xLjMKMyAwIG9iago8PC9UeXBlIC9QYWdlCi9QYXJlbnQgMSAwIFIKL1Jlc291cmNlcyAyIDAgUgovTWVkaWFCb3ggWzAgMCA1OTUuMjggODQxLjg5XQovQ29udGVudHMgNCAwIFI+PgplbmRvYmoKNCAwIG9iago8PC9MZW5ndGggMjcxPj4Kc3RyZWFtCjAuNTcgdwowIEcKQlQKL0YxIDE2IFRmCjE4LjQgVEwKMCBnCjU2LjY5IDc4NS4yMCBUZAooT2BZfQAgA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owMDAwMDAxMzAyIDAwMDAwIG4gCjAwMDAwMDEzOTkgMDAwMDAgbiAKMDAwMDAwMTUwMCAwMDAwMCBuIAowMDAwMDAxNTkzIDAwMDAwIG4gCjAwMDAwMDE2ODUgMDAwMDAgbiAKMDAwMDAwMTc3OSAwMDAwMCBuIAowMDAwMDAyMTAzIDAwMDAwIG4gCjAwMDAwMDIyMjAgMDAwMDAgbiAKdHJhaWxlcgo8PAovU2l6ZSAyMQovUm9vdCAyMCAwIFIKL0luZm8gMTkgMCBSCj4+CnN0YXJ0eHJlZgoyMzI0CiUlRU9G',
  47. yctip: '',
  48. pdfToptips: '长按可保存图片,如有多张图片请分别长按保存。'
  49. }
  50. },
  51. mounted () {
  52. this.isSX = true
  53. this.initThePDFJSLIB()
  54. this.loadFile(this.base64StrPDF)
  55. },
  56. methods: {
  57. GetQueryString (name) {
  58. if (!name) return null
  59. // 查询参数:先通过search取值,如果取不到就通过hash来取
  60. var after = window.location.search
  61. after = after.substr(1) || window.location.hash.split('?')[1]
  62. // 地址栏URL没有查询参数,返回空
  63. if (!after) return null
  64. // 如果查询参数中没有"name",返回空
  65. if (after.indexOf(name) === -1) return null
  66. var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)')
  67. // 当地址栏参数存在中文时,需要解码,不然会乱码
  68. var r = decodeURI(after).match(reg)
  69. // 如果url中"name"没有值,返回空
  70. if (!r) return null
  71. return r[2]
  72. },
  73. addpage () {
  74. if (this.pdfList > this.page) {
  75. this.page++
  76. this._renderPage(this.page)
  77. this.ztp()
  78. }
  79. },
  80. subpage () {
  81. if (this.page > 1) {
  82. this.page--
  83. this._renderPage(this.page)
  84. this.ztp()
  85. }
  86. },
  87. ztp () {
  88. html2canvas(this.$refs.img).then(canvas => { // html 转 canvas
  89. // canvas转img
  90. this.imgSrc = this.$refs.img.toDataURL('image/png')
  91. if (this.imgSrc !== '') {
  92. this.is_img_show = true
  93. }
  94. })
  95. },
  96. // 初始化pdfjs
  97. initThePDFJSLIB () {
  98. pdfjsLib.GlobalWorkerOptions.workerSrc = 'pdfjs-dist/build/pdf.worker.js'
  99. },
  100. _renderPage (num) {
  101. this.pdfDoc.getPage(num).then(page => {
  102. let canvas = document.getElementById('the-canvas' + num)
  103. let ctx = canvas.getContext('2d')
  104. let dpr = window.devicePixelRatio || 1
  105. let bsr =
  106. ctx.webkitBackingStorePixelRatio ||
  107. ctx.mozBackingStorePixelRatio ||
  108. ctx.msBackingStorePixelRatio ||
  109. ctx.oBackingStorePixelRatio ||
  110. ctx.backingStorePixelRatio ||
  111. 1
  112. let ratio = dpr / bsr
  113. let viewport = page.getViewport(
  114. this.enlargeWidth / page.getViewport(1).width
  115. )
  116. canvas.width = viewport.width * ratio
  117. canvas.height = viewport.height * ratio
  118. canvas.style.width = viewport.width + 'px'
  119. canvas.style.height = viewport.height + 'px'
  120. ctx.setTransform(ratio, 0, 0, ratio, 0, 0)
  121. let renderContext = {
  122. canvasContext: ctx,
  123. viewport: viewport
  124. }
  125. page.render(renderContext)
  126. })
  127. },
  128. loadFile (base64Str) {
  129. PDFJS.getDocument({
  130. data: atob(base64Str),
  131. cMapUrl: 'https://cdn.jsdelivr.net/npm/pdfjs-dist@2.0.943/cmaps/',
  132. cMapPacked: true
  133. }).then(pdf => {
  134. this.pdfDoc = pdf
  135. this.pdfList = this.pdfDoc.numPages
  136. this.$nextTick(() => {
  137. this._renderPage(1)
  138. this.ztp()
  139. })
  140. })
  141. }
  142. }
  143. }
  144. </script>
  145. <style lang="scss">
  146. .h5pdf {
  147. position: absolute;
  148. width: 100%;
  149. height: 100%;
  150. background: #F9FAFB;
  151. .el-button + .el-button {
  152. margin-left: 0.4rem;
  153. }
  154. .el-button:hover{
  155. color: #FFFFFF;
  156. border-color: #1E1E1E;
  157. background-color: #1E1E1E;
  158. }
  159. .ymxs {
  160. background: #1E1E1E;
  161. color: #FFFFFF;
  162. border: none;
  163. font-size: 18px;
  164. }
  165. .syyBut {
  166. background: #1E1E1E;
  167. color: #FFFFFF;
  168. border: none;
  169. font-size: 18px;
  170. margin-top: 0.1rem;
  171. }
  172. .xyyBut {
  173. background: #1E1E1E;
  174. color: #FFFFFF;
  175. border: none;
  176. font-size: 18px;
  177. margin-top: 0.1rem;
  178. }
  179. .cvs {
  180. margin-top: 1rem;
  181. }
  182. .catips {
  183. position: fixed;
  184. top: 0;
  185. width: 100%;
  186. height: 0.35rem;
  187. background: #FEF7DA;
  188. font-size: 13px;
  189. color: #FFA922;
  190. }
  191. .catipswz {
  192. padding-right: 0.16rem;
  193. padding-top: 0.08rem;
  194. padding-bottom: 0.08rem;
  195. }
  196. .ljsx {
  197. margin: 2.32rem auto auto auto;
  198. }
  199. .ljsxwz {
  200. color: #999999;
  201. font-size: 18px;
  202. }
  203. .ljsxtp {
  204. width: 1.58rem;
  205. height: 1.505rem;
  206. }
  207. footer {
  208. position: fixed;
  209. bottom: 0;
  210. z-index: 999;
  211. width: 100%;
  212. height: 0.62rem;
  213. background: #1E1E1E;
  214. opacity: 0.7;
  215. }
  216. canvas {
  217. display: block;
  218. }
  219. .img {
  220. position: absolute;
  221. top: 0.36rem;
  222. left: 0;
  223. opacity: 0;
  224. width: 100%;
  225. height: 85%;
  226. }
  227. }
  228. </style>

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

闽ICP备14008679号