当前位置:   article > 正文

VUE-PDF VUE的PDF预览组件_vue-pdf cdn地址

vue-pdf cdn地址

VUE-PDF VUE的PDF预览组件

 

vue.js pdf viewer

 

安装

npm install --save vue-pdf

 

示例

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

 

演示

vue-pdf demo on jsfiddle

TBD: fix the demo

 

浏览器支持

Same browser support as Vue.js 2

 

注意

since v2.x, the script is exported as esm.

 

API

 

Props

:src String / Object - default: ''

The url of the pdf file. src may also be a string|TypedArray|DocumentInitParameters|PDFDataRangeTransport for more details, see PDFJS.getDocument().

:page Number - default: 1

The page number to display.

:rotate Number - default: 0

The page rotation in degrees, only multiple of 90 are valid.

 

Events

@password (updatePassword, reason)

  • updatePassword: The function to call with the pdf password.
  • reason: the reason why this function is called 'NEED_PASSWORD' or 'INCORRECT_PASSWORD'

@progress Number

Document loading progress. Range [0, 1].

@loaded

Triggered when the document is loaded.

@page-loaded Number

Triggered when a page is loaded.

@num-pages Number

The total number of pages of the pdf.

@error Object

Triggered when an error occurred.

@link-clicked Number

Triggered when an internal link is clicked

 

公共方法

print(dpi, pageList) * experimental *

  • dpi: the print rezolution of the document (try 100).
  • pageList: the list (array) of pages to print.

 

公共静态方法

createLoadingTask(src)

  • src: see :src prop
    This function creates a PDFJS loading task that can be used and reused as :src property.
    The loading task is a promise that resolves with the PDFJS pdf document that exposes the numPages property (see example below).

beware: when the component is destroyed, the object returned by createLoadingTask() become invalid.

 

示例

Example - current page / page count

  1. <template>
  2. <div>
  3. {{currentPage}} / {{pageCount}}
  4. <pdf
  5. src="https://cdn.mozilla.net/pdfjs/tracemonkey.pdf"
  6. @num-pages="pageCount = $event"
  7. @page-loaded="currentPage = $event"
  8. ></pdf>
  9. </div>
  10. </template>
  11. <script>
  12. import pdf from 'vue-pdf'
  13. export default {
  14. components: {
  15. pdf
  16. },
  17. data() {
  18. return {
  19. currentPage: 0,
  20. pageCount: 0,
  21. }
  22. }
  23. }
  24. </script>

Example - display multiple pages of the same pdf document

  1. <template>
  2. <div>
  3. <pdf
  4. v-for="i in numPages"
  5. :key="i"
  6. :src="src"
  7. :page="i"
  8. style="display: inline-block; width: 25%"
  9. ></pdf>
  10. </div>
  11. </template>
  12. <script>
  13. import pdf from 'vue-pdf'
  14. var loadingTask = pdf.createLoadingTask('https://cdn.mozilla.net/pdfjs/tracemonkey.pdf');
  15. export default {
  16. components: {
  17. pdf
  18. },
  19. data() {
  20. return {
  21. src: loadingTask,
  22. numPages: undefined,
  23. }
  24. },
  25. mounted() {
  26. this.src.then(pdf => {
  27. this.numPages = pdf.numPages;
  28. });
  29. }
  30. }
  31. </script>

Example - print all pages

  1. <template>
  2. <button @click="$refs.myPdfComponent.print()">print</button>
  3. <pdf ref="myPdfComponent" src="https://cdn.mozilla.net/pdfjs/tracemonkey.pdf"></pdf>
  4. </template>

Example - print multiple pages

  1. <template>
  2. <button @click="$refs.myPdfComponent.print(100, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14])">print</button>
  3. <pdf ref="myPdfComponent" src="https://cdn.mozilla.net/pdfjs/tracemonkey.pdf"></pdf>
  4. </template>

Example - get text content

  1. <template>
  2. <div>
  3. <button
  4. @click="logContent"
  5. >
  6. log content
  7. </button>
  8. <pdf
  9. ref="myPdfComponent"
  10. src="https://cdn.mozilla.net/pdfjs/tracemonkey.pdf"
  11. ></pdf>
  12. </div>
  13. </template>
  14. <script>
  15. import pdf from 'vue-pdf'
  16. export default {
  17. components: {
  18. pdf
  19. },
  20. methods: {
  21. logContent() {
  22. this.$refs.myPdfComponent.pdf.forEachPage(function(page) {
  23. return page.getTextContent()
  24. .then(function(content) {
  25. var text = content.items.map(item => item.str);
  26. console.log(text);
  27. })
  28. });
  29. }
  30. }
  31. }
  32. </script>

Example - complete

  1. <template>
  2. <div>
  3. <input type="checkbox" v-model="show">
  4. <select v-model="src" style="width: 30em">
  5. <option v-for="item in pdfList" :value="item" v-text="item"></option>
  6. </select>
  7. <input v-model.number="page" type="number" style="width: 5em"> /{{numPages}}
  8. <button @click="rotate += 90">&#x27F3;</button>
  9. <button @click="rotate -= 90">&#x27F2;</button>
  10. <button @click="$refs.pdf.print()">print</button>
  11. <div style="width: 50%">
  12. <div v-if="loadedRatio > 0 && loadedRatio < 1" style="background-color: green; color: white; text-align: center" :style="{ width: loadedRatio * 100 + '%' }">{{ Math.floor(loadedRatio * 100) }}%</div>
  13. <pdf v-if="show" ref="pdf" style="border: 1px solid red" :src="src" :page="page" :rotate="rotate" @password="password" @progress="loadedRatio = $event" @error="error" @num-pages="numPages = $event" @link-clicked="page = $event"></pdf>
  14. </div>
  15. </div>
  16. </template>
  17. <script>
  18. import pdf from 'vue-pdf'
  19. export default {
  20. components: {
  21. pdf: pdf
  22. },
  23. data () {
  24. return {
  25. show: true,
  26. pdfList: [
  27. '',
  28. 'https://cdn.mozilla.net/pdfjs/tracemonkey.pdf',
  29. 'https://cdn.rawgit.com/mozilla/pdf.js/c6e8ca86/test/pdfs/freeculture.pdf',
  30. 'https://cdn.rawgit.com/mozilla/pdf.js/c6e8ca86/test/pdfs/annotation-link-text-popup.pdf',
  31. 'https://cdn.rawgit.com/mozilla/pdf.js/c6e8ca86/test/pdfs/calrgb.pdf',
  32. 'https://cdn.rawgit.com/sayanee/angularjs-pdf/68066e85/example/pdf/relativity.protected.pdf',
  33. 'data:application/pdf;base64,JVBERi0xLjUKJbXtrvsKMyAwIG9iago8PCAvTGVuZ3RoIDQgMCBSCiAgIC9GaWx0ZXIgL0ZsYXRlRGVjb2RlCj4+CnN0cmVhbQp4nE2NuwoCQQxF+/mK+wMbk5lkHl+wIFislmIhPhYEi10Lf9/MVgZCAufmZAkMppJ6+ZLUuFWsM3ZXxvzpFNaMYjEriqpCtbZSBOsDzw0zjqPHZYtTrEmz4eto7/0K54t7GfegOGCBbBdDH3+y2zsMsVERc9SoRkXORqKGJupS6/9OmM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vU2l6ZSA4CiAgIC9Sb290IDcgMCBSCiAgIC9JbmZvIDYgMCBSCj4+CnN0YXJ0eHJlZgo4MjQKJSVFT0YK',
  34. ],
  35. src:'',
  36. loadedRatio: 0,
  37. page: 1,
  38. numPages: 0,
  39. rotate: 0,
  40. }
  41. },
  42. methods: {
  43. password: function(updatePassword, reason) {
  44. updatePassword(prompt('password is "test"'));
  45. },
  46. error: function(err) {
  47. console.log(err);
  48. }
  49. }
  50. }
  51. </script>

 

关键词

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

闽ICP备14008679号