-
Notifications
You must be signed in to change notification settings - Fork 1
/
pdf-renderer.js
107 lines (84 loc) · 2.21 KB
/
pdf-renderer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
'use strict'
let EventEmitter = require('events')
let Watchdog = require('ya-watchdog')
let co = require('co')
// PDFJS comes from global scope, no RequireJS support for the moment
const HQ_SCALE = 3.0
const LQ_SCALE = 0.75
class PDFRenderer extends EventEmitter {
constructor (pdfFile) {
super()
this._canvas = document.createElement('canvas')
this._ctx = this._canvas.getContext('2d')
this._idle = true
this._nextJob = null
this._lastJob = null
let self = this
co(function * () {
self._book = yield PDFJS.getDocument(pdfFile)
self._page = 0
// Output some stats
console.log(`Num pages = ${self._book.numPages}`)
self._watchdog = new Watchdog(150)
self._watchdog.on('timeout', function () {
self._render()
})
self._pushRenderJob(HQ_SCALE)
self._render()
self.emit('ready')
})
}
nextPage () {
this._page++
if (this._page >= this._book.numPages) this._page = this._book.numPages - 1 // Saturate
this._pushRenderJob(LQ_SCALE)
this._render()
}
prevPage () {
this._page--
if (this._page < 0) this._page = 0
this._pushRenderJob(LQ_SCALE)
this._render()
}
destroy () {
this._book.destroy()
}
_upToDate () {
return this._lastJob.page === this._page && this._lastJob.quality === HQ_SCALE
}
_pushRenderJob (quality) {
this._nextJob = {
page: this._page,
quality: quality
}
}
_render () {
if (!this._idle) return
if (this._nextJob === null) {
if (this._upToDate()) return
this._pushRenderJob(HQ_SCALE)
this._watchdog.kick()
return
}
let job = this._nextJob
this._idle = false
this._nextJob = null
let self = this
co(function * () {
let p = yield self._book.getPage(job.page + 1)
let viewport = p.getViewport(job.quality)
self._canvas.height = viewport.height
self._canvas.width = viewport.width
yield p.render({
canvasContext: self._ctx,
viewport: viewport
})
self.emit('data', self._canvas.toDataURL())
self._idle = true
self._lastJob = job
// If there is any new job
self._render()
})
}
}
module.exports = PDFRenderer