2.0.0 • Published 5 months ago

@mispencer/vue-pdf-embed v2.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

📄 vue-pdf-embed

PDF embed component for Vue 3 and Vue 2 (see Compatibility)

Awesome npm npm GitHub Repo stars npm

Features

  • Controlled rendering of PDF documents in Vue apps
  • Handling password-protected documents
  • Includes text layer (searchable and selectable documents)
  • Includes annotation layer (annotations and links)
  • No peer dependencies or additional configuration required
  • Can be used directly in the browser (see Examples)

Compatibility

This package is only compatible with Vue 3. For Vue 2 support, install vue-pdf-embed@1 and refer to the v1 docs.

Installation

Depending on the environment, the package can be installed in one of the following ways:

npm install vue-pdf-embed
yarn add vue-pdf-embed
<script src="https://unpkg.com/vue-pdf-embed"></script>

Usage

<template>
  <div>
    <h1>File</h1>
    <vue-pdf-embed :source="source1" />

    <h1>Base64</h1>
    <vue-pdf-embed :source="source2" />
  </div>
</template>

<script>
import VuePdfEmbed from 'vue-pdf-embed'

export default {
  components: {
    VuePdfEmbed,
  },
  data() {
    return {
      source1: '<PDF_URL>',
      source2: 'data:application/pdf;base64,<BASE64_ENCODED_PDF>',
    }
  },
}
</script>

Props

NameTypeAccepted valuesDescription
annotationLayerbooleantrue or falsewhether the annotation layer should be enabled
heightnumber stringnatural numbersdesired page height in pixels (ignored if the width property is specified)
imageResourcesPathstringURL or path with trailing slashpath for icons used in the annotation layer
workerSrcstringURLpath for PDF worker script
pagenumber string1 to the last page numbernumber of the page to display (displaying all pages if not specified)
rotationnumber string0, 90, 180, 270 (multiples of 90)desired page rotation angle in degrees
scalenumberrational numbersdesired ratio of canvas size to document size
sourcestring objectdocument URL or Base64 or typed array pre-filled with datasource of the document to display
textLayerbooleantrue or falsewhether the text layer should be enabled
widthnumber stringnatural numbersdesired page width in pixels

Events

NameValueDescription
internal-link-clickeddestination page numberinternal link was clicked
loadedPDF document proxyfinished loading the document
loading-failederror objectfailed to load document
password-requestedobject with callback function and isWrongPassword flagpassword is needed to display the document
progressobject with number of loaded pages along with total number of pagestracking document loading progress
renderedfinished rendering the document
rendering-failederror objectfailed to render document

Slots

NamePropsDescription
after-pagepage (page number)content to be added after each page
before-pagepage (page number)content to be added before each page

Public Methods

NameArgumentsDescription
downloadfilename (string)download document
printprint resolution (number), filename (string), all pages flag (boolean)print document via browser interface
rendermanually (re)render document

Note: Public methods can be accessed via a template ref.

Common Issues

This is a client-side library, so it is important to keep this in mind when working with SSR (server-side rendering) frameworks such as Nuxt. Depending on the framework used, you may need to properly configure the library import or use a wrapper.

The path to predefined CMaps should be specified to ensure correct rendering of documents containing non-Latin characters, as well as in case of CMap-related errors:

<vue-pdf-embed
  :source="{
    cMapUrl: 'https://unpkg.com/pdfjs-dist/cmaps/',
    url: pdfSource,
  }"
/>

The image resource path must be specified for annotations to display correctly:

<vue-pdf-embed
  image-resources-path="https://unpkg.com/pdfjs-dist/web/images/"
  :source="pdfSource"
/>

Note: The examples above use a CDN to load resources, however these resources can also be included in the build by installing the pdfjs-dist package as a dependency and further configuring the bundler.

Examples

Basic Usage Demo (JSFiddle)

Advanced Usage Demo (JSFiddle)

Advanced Usage Demo (StackBlitz)

License

MIT License. Please see LICENSE file for more information.

2.0.0

5 months ago