vue2中使用vue-pdf

用法很简单 参考文档(https://www.npmjs.com/package/vue-pdf)

打印时会出现乱码,解决办法参考链接(https://www.cnblogs.com/xiaoyaoxingchen/p/10894035.html)

<template>
  <div>
   
    <!-- vue-pdf只兼容vue2,且路径如果不是网络路径,本地路径得文件需要放在public文件夹下且需要用绝对路径 -->
    <div class="content">
      <!-- /默认从public文件下查找 -->
       <pdf src='http://119.23.142.49:3031/三分钟学前端网络篇.pdf' ref="pdf" @num-pages="total=$event" :page="currentPage"></pdf> 
    </div>

  <el-pagination
  background
  :page-size="1"
  :current-page="currentPage"
  @current-change="currentPage=$event"
  layout="prev, pager, next"
  :total="total">
</el-pagination>
<button @click="printPdf">打印</button>
       <router-view/>
  </div>
</template>

<script>
import pdf from 'vue-pdf'
export default {
  components: {pdf },
  data(){
    return {
      total:0,
      currentPage:1,
    }
  },
  methods:{
    printPdf(){
      this.$refs.pdf.print(100,[1,2,3])
    }
  }
}
</script>

<style>
.content{
  width: 700px;
  height: 1000px;
}
</style>
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
vue-pdf-embed是一个Vue组件,用于在Vue应用程序嵌入和显示PDF文件。它提供了一个简单的方式来展示PDF文件,并且支持一些常用的功能,比如缩放、翻页等。 要使用vue-pdf-embed,首先需要在你的Vue项目安装该组件。可以通过npm或者yarn来进行安装,具体的安装命令如下: ``` npm install vue-pdf-embed ``` 或者 ``` yarn add vue-pdf-embed ``` 安装完成后,在你的Vue组件引入vue-pdf-embed,并在模板使用它。以下是一个简单的示例: ```vue <template> <div> <vue-pdf-embed :src="pdfUrl" :page="currentPage" :scale="scale"></vue-pdf-embed> <button @click="prevPage">上一页</button> <button @click="nextPage">下一页</button> <input type="range" v-model="scale" min="0.5" max="2" step="0.1"> </div> </template> <script> import VuePdfEmbed from 'vue-pdf-embed'; export default { components: { VuePdfEmbed, }, data() { return { pdfUrl: 'path/to/your/pdf/file.pdf', currentPage: 1, scale: 1, }; }, methods: { prevPage() { if (this.currentPage > 1) { this.currentPage--; } }, nextPage() { // 在实际应用,你可能需要根据PDF文件的总页数来判断是否到达最后一页 this.currentPage++; }, }, }; </script> ``` 在上面的示例,我们首先引入了vue-pdf-embed组件,并在模板使用了它。通过设置`:src`属性来指定要显示的PDF文件的URL,`:page`属性来指定当前显示的页码,`:scale`属性来指定缩放比例。 同时,我们还添加了两个按钮和一个滑动条,用于切换页码和调整缩放比例。你可以根据实际需求进行修改和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值