vue-pdf解决显示中文乱码和打印预览乱码

vue-pdf解决显示中文乱码和打印预览乱码

1.正常的vue-pdf使用
1.1 安装vue-pdf

cnpm install vue-pdf --save-dev


1.2 简单使用

  1.  
    <template>
  2.  
    <pdf src="./123.pdf"></pdf>
  3.  
    </template>
  4.  
     
  5.  
    <script>
  6.  
    import pdf from 'vue-pdf'
  7.  
     
  8.  
    export default {
  9.  
    components: {
  10.  
    pdf
  11.  
    }
  12.  
    }
  13.  
    </script>


1.3 分页、打印

  1.  
    <template>
  2.  
    <div id="showpdf">
  3.  
    <el-row>
  4.  
    <el-col :span="24" style="padding:10px;">
  5.  
    <div style="width:100%;text-align:center;padding-right:20px;">
  6.  
    <el-button type="primary" size="mini" @click="changePage(-1)">首页</el-button>
  7.  
    <el-button type="primary" size="mini" @click="changePage(0)">上一页</el-button>
  8.  
    <span style=" margin-left:20px;margin-right:20px;">
  9.  
    共 {{pageCount}} 页 ,当前第 {{currentPage}} 页
  10.  
    </span>
  11.  
    <el-button type="primary" size="mini" @click="changePage(1)">下一页</el-button>
  12.  
    <el-button type="primary" size="mini" @click="changePage(99999)">尾页</el-button>
  13.  
    <el-button v-if="isAuth('input_info_print') " style="float: right;margin-left:10px;" size="mini"
  14.  
    type="primary" icon="el-icon-printer" @click="$refs.pdf.print()">打印</el-button>
  15.  
    </div>
  16.  
    <div class="main" v-loading="loading">
  17.  
    <pdf :src="pdfUrl" :page="currentPage" @progress="loadedRatio = $event"
  18.  
    @num-pages="pageCount=$event" @page-loaded="currentPage=$event" @loaded="loadPdfHandler"
  19.  
    ref="pdf" class="pdf"></pdf>
  20.  
    </div>
  21.  
    </el-col>
  22.  
    </el-row>
  23.  
    </div>
  24.  
     
  25.  
    </template>
  26.  
     
  27.  
    <script>
  28.  
    import pdf from "vue-pdf";
  29.  
    export default {
  30.  
    data() {
  31.  
    return {
  32.  
    pdfUrl: "",
  33.  
    currentPage: 1,
  34.  
    pageCount: 0,
  35.  
    scale: 100, //放大系数
  36.  
    loadedRatio: 0,
  37.  
    loading: false,
  38.  
    };
  39.  
    },
  40.  
    components: {
  41.  
    pdf,
  42.  
    },
  43.  
    methods: {
  44.  
    init(id) {//初始化
  45.  
    this.pdfUrl = "";
  46.  
    this.showPdf(id);
  47.  
    },
  48.  
    loadPdfHandler(e) {//加载分页信息
  49.  
    this.currentPage = 1; // 加载的时候先加载第一页
  50.  
    this.pageCount = 1;
  51.  
    },
  52.  
    showPdf(id) {//显示pdf信息
  53.  
    this.loading = true;
  54.  
    this.pageCount = 1;
  55.  
    this.currentPage = 1;
  56.  
    var curl = `/api/v1/xx/xx/show/${id}`;
  57.  
    this.$http({
  58.  
    url: this.$http.adornUrl(curl),
  59.  
    method: "GET",
  60.  
    responseType: "blob",
  61.  
    }).then((res) => {
  62.  
    var url = window.URL.createObjectURL(new Blob([res.data])); //后端获取pdf文件流
  63.  
    this.pdfUrl = pdf.createLoadingTask({
  64.  
    url: url,
  65.  
    cMapUrl: "../../../../static/cmaps/",//加载字体包
  66.  
    cMapPacked: true,
  67.  
    });
  68.  
    this.loading = false;
  69.  
    });
  70.  
    },
  71.  
    toBig() {//放大缩小
  72.  
    this.scale += 5;
  73.  
    this.$refs.wrapper.$el.style.width = parseInt(this.scale) + "%";
  74.  
    },
  75.  
    toSmall() {
  76.  
    if (this.scale == 100) {
  77.  
    return;
  78.  
    }
  79.  
    this.scale += -5;
  80.  
    this.$refs.wrapper.$el.style.width = parseInt(this.scale) + "%";
  81.  
    },
  82.  
    changePage(val) {//翻页
  83.  
    if (val == 99999) {
  84.  
    this.currentPage = this.pageCount;
  85.  
    return;
  86.  
    }
  87.  
    if (val == -1) {
  88.  
    this.currentPage = 1;
  89.  
    return;
  90.  
    }
  91.  
    if (val === 0 && this.currentPage > 1) {
  92.  
    this.currentPage--;
  93.  
    }
  94.  
    if (val === 1 && this.currentPage < this.pageCount) {
  95.  
    this.currentPage++;
  96.  
    }
  97.  
    },
  98.  
    },
  99.  
    };
  100.  
    </script>


2.解决无法显示中文


正常情况,不会显示中文的问题。一般出问题的都是票据、合同之类的pdf。
我也是遇到这种pdf的时候,才发现问题。经过各种百度,发现大家使用

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

但是,但是,但是,只是在第一次显示的时候,中文可以显示出来。再次查看,就不能正常显示。
又一顿百度,太失望了,很多人为了这个问题,放弃使用vue-pdf了。当然使用pdf.js和pdfjs-dist也可以解决问题,但是,vue-pdf使用简单,可以自由控制分页、打印等功能,不甘心,就去github vue-pdf上找,结果还真有大神解决了。有兴趣的查看链接:vue-pdf

啰嗦一大堆,看解决方法:

复制cmap文件到/static,直接引用。

  1.  
    this.url= pdf.createLoadingTask({
  2.  
    url: 'http://localhost/11.pdf',
  3.  
    cMapUrl: '../../../../static/cmaps/',
  4.  
    cMapPacked: true
  5.  
    })


cmap所在位置:

node_modules\vue-pdf\node_modules\pdfjs-dist\cmaps


3. 打印预览中文乱码

更改node_modules\vue-pdf\src\pdfjsWrapper.js文件,
参照:
https://github.com/FranckFreiburger/vue-pdf/pull/130/commits/253f6186ff0676abf9277786087dda8d95dd8ea7
更改后即可。
最后附上效果:


打印预览:

 

 

如果解决了你的问题,手动来个赞,让我知道帮助到了你~
————————————————
版权声明:本文为CSDN博主「沙沉海底」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/w546097639/article/details/108472464

posted @   踏浪小鲨鱼  阅读(522)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示