经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » JS/JS库/框架 » Vue.js » 查看文章
vue pdf二次封装解决无法显示中文问题方法详解
来源:jb51  时间:2023/1/18 8:41:56  对本文有异议

前言

vue-pdf 可以实现PDF文件在线预览并且支持分页。安装方式:npm install --save vue-pdf

完整代码

  1. <template>
  2. <el-dialog :visible="visible" title="查看PDF" width="1100px" top="2vh" append-to-body @close="handleClose">
  3. <pdf-viewer
  4. v-if="url"
  5. style="height: 750px;overflow: auto;"
  6. :src="url"
  7. :page="pageData.currentPage"
  8. @num-pages="pageData.total = $event"
  9. @page-loaded="pageData.currentPage = $event"
  10. @loaded="loadPdfHandler()">
  11. </pdf-viewer>
  12. <div class="ui-pdf-page" v-if="pageData.total > 1">
  13. <span @click="changePdfPage(0)" :class="pageData.currentPage == 1 ? '' : 'ui-link'">上一页</span>
  14. <span>{{pageData.currentPage + '/' + pageData.total}}</span>
  15. <span @click="changePdfPage(1)" :class="pageData.currentPage == pageData.total ? '' : 'ui-link'">下一页</span>
  16. </div>
  17. </el-dialog>
  18. </template>
  19. <script>
  20. import pdfViewer from 'vue-pdf'
  21. import CMapReaderFactory from 'vue-pdf/src/CMapReaderFactory.js' //解决中文显示
  22. export default{
  23. components: {
  24. pdfViewer
  25. },
  26. name: 'ComPdfView',
  27. props: {
  28. src: {
  29. type: String,
  30. default: '',
  31. },
  32. visible: {
  33. type: Boolean,
  34. default: false
  35. },
  36. },
  37. data(){
  38. return {
  39. url: '',
  40. pageData: {
  41. currentPage: 0,
  42. total: 0,
  43. }
  44. }
  45. },
  46. watch:{
  47. visible(val) {
  48. if (val) {
  49. this.url = pdfViewer.createLoadingTask({ url: this.src, CMapReaderFactory });
  50. }
  51. }
  52. },
  53. methods: {
  54. handleClose() {
  55. this.pageData.currentPage = 1;
  56. this.$emit('update:visible', false);
  57. },
  58. changePdfPage (val) {
  59. if (val === 0 && this.pageData.currentPage > 1) {
  60. this.pageData.currentPage--
  61. }
  62. if (val === 1 && this.pageData.currentPage < this.pageData.total) {
  63. this.pageData.currentPage++
  64. }
  65. },
  66. loadPdfHandler() {
  67. this.pageData.currentPage = 1;
  68. }
  69. }
  70. }
  71. </script>
  72. <style scoped lang="less">
  73. .ui-pdf-page span {
  74. font-size: 12px;
  75. padding: 0 20px;
  76. color: #626879;
  77. }
  78. .ui-pdf-page span.ui-link {
  79. color: #3c8cff;
  80. cursor: pointer;
  81. }
  82. </style>

解决无法显示中文问题

关键代码

  1. import pdfViewer from 'vue-pdf'
  2. import CMapReaderFactory from 'vue-pdf/src/CMapReaderFactory.js'
  3. this.url = pdfViewer.createLoadingTask({ url: this.src, CMapReaderFactory });

以上就是vue pdf二次封装解决无法显示中文问题方法详解的详细内容,更多关于vue pdf封装中文显示的资料请关注w3xue其它相关文章!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号