Home

Awesome

📄 vue-pdf-embed

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

Awesome List npm Version npm Downloads GitHub Stars License

Features

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

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

// optional styles
import 'vue-pdf-embed/dist/styles/annotationLayer.css'
import 'vue-pdf-embed/dist/styles/textLayer.css'

// either URL, Base64, binary, or document proxy
const pdfSource = '<PDF_URL>'
</script>

<template>
  <VuePdfEmbed annotation-layer text-layer :source="pdfSource" />
</template>

Props

NameTypeAccepted valuesDescription
annotationLayerbooleantrue or falsewhether the annotation layer should be enabled
heightnumbernatural 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
pagenumber1 to the last page numbernumber of the page to display (displaying all pages if not specified)
rotationnumber0, 90, 180, 270 (multiples of 90)desired page rotation angle in degrees
scalenumberrational numbersdesired page viewport scale
sourcestring <br> objectdocument URL or Base64 or typed array or document proxysource of the document to display
textLayerbooleantrue or falsewhether the text layer should be enabled
widthnumbernatural 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
rendered–finished 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

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

Common Issues and Caveats

Server-Side Rendering

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.

Web Worker Loading

The web worker used to handle PDF documents is loaded by default. However, this may not be acceptable due to bundler restrictions or CSP (Content Security Policy). In such cases it is recommended to use the essential build (index.essential.mjs) and set up the worker manually using the exposed GlobalWorkerOptions.

import { GlobalWorkerOptions } from 'vue-pdf-embed/dist/index.essential.mjs'
import PdfWorker from 'pdfjs-dist/build/pdf.worker.mjs?url'

GlobalWorkerOptions.workerSrc = PdfWorker

Document Loading

Typically, document loading is internally handled within the component. However, for optimization purposes, the document can be loaded in the useVuePdfEmbed composable function and then passed as the source prop of the component (e.g. when sharing the source between multiple instances of the component).

<script setup>
import VuePdfEmbed, { useVuePdfEmbed } from 'vue-pdf-embed'

const { doc } = useVuePdfEmbed({ source: '<PDF_URL>' })
</script>

<template>
  <VuePdfEmbed :source="doc" />
</template>

Resources

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:

<VuePdfEmbed
  :source="{
    cMapUrl: 'https://unpkg.com/pdfjs-dist/cmaps/',
    url: '<PDF_URL>',
  }"
/>

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

<VuePdfEmbed
  image-resources-path="https://unpkg.com/pdfjs-dist/web/images/"
  source="<PDF_URL>"
/>

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)

License

MIT License. Please see LICENSE file for more information.