pdf.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <template>
  2. <div class="cpdf" id="cpdf">
  3. <Loading :loading="loading" />
  4. <canvas v-for="item in numPages" :key="item" :id="'pdf'+item" />
  5. </div>
  6. </template>
  7. <script>
  8. import * as dd from 'dingtalk-jsapi';
  9. import Loading from '@/components/Loading';
  10. import pdfjsLib from 'pdfjs-dist';
  11. export default {
  12. components: {
  13. // pdfjsLib,
  14. Loading
  15. },
  16. data() {
  17. return {
  18. numPages: 0,
  19. pdfSrc: '', // pdf文件地址
  20. loading: true
  21. }
  22. },
  23. created() {
  24. if (this.$route.query.path) {
  25. this.loadPdfHandler(this.$route.query.path);
  26. }
  27. if (this.$route.query.title) {
  28. this.ChangePageTitle(this.$route.query.title)
  29. }
  30. if (this.$route.query.id) {
  31. this.getDetail();
  32. } else {
  33. this.loading = false;
  34. // let path = 'http://storage.xuetangx.com/public_assets/xuetangx/PDF/PlayerAPI_v1.0.6.pdf'//你的pdf地址
  35. // this.loadPdfHandler(path);
  36. }
  37. // if (dd.env.platform != 'notInDingTalk') {
  38. // dd.biz.util.share({
  39. // type: 2,//分享类型,0:全部组件 默认;1:只能分享到钉钉;2:不能分享,只有刷新按钮
  40. // url: '',
  41. // title: '',
  42. // content: '',
  43. // image: null,
  44. // onSuccess: function () {
  45. // //onSuccess将在调起分享组件成功之后回调
  46. // /**/
  47. // },
  48. // onFail: function (err) { }
  49. // })
  50. // }
  51. },
  52. methods: {
  53. // pdf加载时
  54. loadPdfHandler(path,watermark) {
  55. try {
  56. let loadingTask = pdfjsLib.getDocument({
  57. url: path,
  58. // cMapUrl: "https://unpkg.com/browse/pdfjs-dist@2.2.228/cmaps/",
  59. // cMapUrl:"../../assets/cmaps/",
  60. // cMapPacked: true
  61. });
  62. let _this = this;
  63. loadingTask.promise.then((pdf) => {
  64. this.numPages = pdf.numPages;
  65. for (let i = 1; i <= pdf.numPages; i++) {
  66. pdf.getPage(i).then(function (page) {
  67. let scale = 5; //缩放比例
  68. let viewport = page.getViewport(scale);
  69. let canvas = document.getElementById('pdf' + i);
  70. let context = canvas.getContext('2d');
  71. canvas.height = viewport.height;
  72. canvas.width = viewport.width;
  73. canvas.style.width = '100%';
  74. context.font = "24px 宋体"
  75. context.fillStyle = "#FFC82C"
  76. // 设置右对齐
  77. context.textAlign = 'right'
  78. alert(watermark);
  79. alert(canvas.width-20);
  80. alert(canvas.height-20);
  81. // 在指定位置绘制文字,这里指定距离右下角20坐标的地方
  82. context.fillText(watermark, canvas.width - 20, canvas.height - 20)
  83. let renderContext = {
  84. canvasContext: context,
  85. viewport: viewport
  86. };
  87. page.render(renderContext);
  88. });
  89. }
  90. _this.loading = false;
  91. }).catch(() => {
  92. _this.loading = false;
  93. });
  94. } catch (e) {
  95. // 错误处理代码片段
  96. this.loading = false;
  97. }
  98. },
  99. getDetail() {
  100. let self = this;
  101. this.$api.user.getDetail(this.$route.query.id, localStorage.userId || '').then((response) => {
  102. const res = response.data.data;
  103. let path = res.data.path || ''
  104. self.loadPdfHandler(path,res.data.watermark);
  105. self.ChangePageTitle(res.data.fileName);
  106. }).catch(() => {
  107. self.loading = false;
  108. });
  109. },
  110. ChangePageTitle(title) {
  111. if (dd.env.platform != 'notInDingTalk') {
  112. dd.ready(function () {
  113. dd.biz.navigation.setTitle({
  114. title: title, //控制标题文本,空字符串表示显示默认文本
  115. onSuccess: function () {},
  116. onFail: function () {}
  117. });
  118. });
  119. } else {
  120. document.title = title
  121. }
  122. },
  123. }
  124. }
  125. </script>
  126. <style lang="less" scoped>
  127. .cpdf {
  128. width: 100%;
  129. height: 100%;
  130. margin: 0 auto;
  131. overflow-x: hidden;
  132. overflow-y: auto;
  133. .center {
  134. height: 100%;
  135. width: 100%;
  136. overflow: hidden;
  137. }
  138. .contor {
  139. display: flex;
  140. justify-content: center;
  141. margin: 20px;
  142. padding: 10px;
  143. .bottom {
  144. font-size: 15px;
  145. padding-top: 5px;
  146. }
  147. }
  148. }
  149. </style>