Skip to content

Vue Reader - an easy way to embed a ePub into your webapp

An vue-reader for vue powered by EpubJS

Basic usage

bash
npm install vue-reader --save

And in your vue-component...

Vue 3
Vue 2
vue
<template>
  <div style='height: 100vh'>
    <vue-reader url='/vue-reader/files/啼笑因缘.epub'/>
  </div>
</template>
<script>
import { VueReader } from 'vue-reader'

export default {
  components: { VueReader }
}
</script>

Different Builds

ModuleFilename
UMD(for browsers)vue-reader.global-full.js
CommonJSvue-reader.cjs.js
ES Module(for bundlers)vue-reader.es.js

VueReader Attributes

NameDescriptionTypeDefault
urlbook url or arrayBufferstring/ArrayBuffer
titlethe title of the bookstring
showTocwhether to show the tocbooleantrue
backgroundColorbackgroundColor of readerstring#fff

VueReader Events

NameDescriptionType
progressbook url request progressfunction(percentage)

VueReader Slots

NameDescription
titleYou have access to title by slot

VueReader props passed to inner EpubView

EpubView Attributes

NameDescriptionTypeDefault
urlthe path or arrayBuffer of the bookstring/ArrayBuffer
locationset / update location of the epubstring/number
tocChangedwhen the reader has parsed the book you will receive an array of the chaptersfunction(toc)
epubInitOptionspass custom properties to the epub init function, see epub.jsobject
epubOptionspass custom properties to the epub rendition, see epub.js's book.renderTo functionobject
getRenditionwhen epubjs has rendered the epub-file you can get access to the epubjs-rendition object herefunction(rendition)

EpubView Events

NameDescriptionType
update:locationa function that receives the current location while user is reading. This function is called everytime the page changes, and also when it first renders.function(location)
selectwhen select textfunction(cfirange,contents)
keyPresswhen press the keyfunction(keyboardEvent)

EpubView Slots

NameDescription
loadingViewepub view loadingView

EpubView Exposes

NameDescriptionType
nextPagedisplay next pagefunction
prevPagedisplay previous pagefunction
setLocationSet the pagefunction(href)

Recipes and tips

Save and retrieve progress from storage

Saving the current page on storage is pretty simple, but we need to keep in mind that locationChanged also gets called on the very first render of our app.

Vue 2
vue
<template>
  <div style='height: 100vh'>
    <vue-reader url="/files/啼笑因缘.epub" :location='location' @update:location='locationChange'> </vue-reader>
  </div>
</template>
<script>
import { VueReader } from 'vue-reader'
export default {
  components: { VueReader },
  data() {
    return {
      location: null,
      firstRenderDone: false
    }
  },
  methods: {
    locationChange(epubcifi) {
      if (!this.firstRenderDone) {
        this.location = localStorage.getItem('book-progress')
        return this.firstRenderDone = true
      }
      localStorage.setItem('book-progress', epubcifi)
      this.location = epubcifi
    }
  }
}
</script>

Display page number for current chapter

We store the epubjs rendition in a ref, and get the page numbers in the callback when location is changed. Note that in this example we also find them name of the current chapter from the toc. Also see limitation for pagination for the whole book.

Change font-size

Hooking into epubJS rendition object is the key for this also.

Add / adjust custom css for the epub-html

EpubJS render the epub-file inside a iframe so you will need to create a custom theme and apply it.
This is useful for when you want to set custom font families, custom background and text colors, and everything CSS related.

Hightlight selection in epub

This shows how to hook into epubJS annotations object and let the user highlight selection and store this in a list where user can go to a selection or delete it.

Handling missing mime-types on server

EpubJS will try to parse the epub-file you pass to it, but if the server send wrong mine-types or the file does not contain .epub you can use the epubInitOptions prop to force reading it right.

vue
<template>
  <div style="height: 100vh">
    <vue-reader url="/my-epub-service" :epubInitOptions="{ openAs: 'epub' }">
    </vue-reader>
  </div>
</template>
<script setup>
import { VueReader } from 'vue-reader'
</script>

Smooth Scroll

Sets css-property for epub-js manager to scroll-behavior: smooth

Display a scrolled epub-view

Pass options for this into epubJS in the prop epubOptions

Epubjs is rendering the epub-content inside and iframe which defaults to sandbox="allow-same-origin", to enable opening links or running javascript in an epub, you will need to pass some extra params in the epubOptions prop.

vue
<vue-reader
  url='/files/啼笑因缘.epub' 
  :epubOptions='{
    allowPopups: true, // Adds `allow-popups` to sandbox-attribute
    allowScriptedContent: true, // Adds `allow-scripts` to sandbox-attribute
  }'
/>

Speak the text

Get book information

Import file

Current progress

Search in the book

Disable context menu

Custom font