| 6 | import * as _ from "lodash"; |
| 7 | |
| 8 | export class PdfJsDocument implements PdfDocument { |
| 9 | private pdf: PDFDocumentProxy; |
| 10 | |
| 11 | constructor(pdf: PDFDocumentProxy) { |
| 12 | this.pdf = pdf; |
| 13 | } |
| 14 | |
| 15 | public numPages(): number { |
| 16 | return this.pdf.numPages; |
| 17 | } |
| 18 | |
| 19 | public getByteLength(): Promise<number> { |
| 20 | return this.getData().then((buffer) => { |
| 21 | return Promise.resolve(buffer.length); |
| 22 | }); |
| 23 | } |
| 24 | |
| 25 | public getData(): Promise<Uint8Array> { |
| 26 | return new Promise<Uint8Array>((resolve) => { |
| 27 | this.pdf.getData().then((buffer) => { |
| 28 | resolve(buffer); |
| 29 | }); |
| 30 | }); |
| 31 | } |
| 32 | |
| 33 | public getPageListAsDataUrls(pageIndexes: number[]): Promise<string[]> { |
| 34 | let dataUrls: string[] = new Array(pageIndexes.length); |
| 35 | return new Promise((resolve) => { |
| 36 | for (let i = 0; i < pageIndexes.length; i++) { |
| 37 | this.getPageAsDataUrl(pageIndexes[i]).then((dataUrl) => { |
| 38 | dataUrls[i] = dataUrl; |
| 39 | if (ArrayUtils.isArrayComplete(dataUrls)) { |
| 40 | resolve(dataUrls); |
| 41 | } |
| 42 | }); |
| 43 | } |
| 44 | }); |
| 45 | } |
| 46 | |
| 47 | public getPageAsDataUrl(pageIndex: number): Promise<string> { |
| 48 | return new Promise<string>((resolve) => { |
| 49 | // In pdf.js, indexes start at 1 |
| 50 | this.pdf.getPage(pageIndex + 1).then((page) => { |
| 51 | // Issue #369: When the scale is set to 1, we end up with poor quality images |
| 52 | // on high resolution machines. The best explanation I found for this was in |
| 53 | // this article: http://stackoverflow.com/questions/35400722/pdf-image-quality-is-bad-using-pdf-js |
| 54 | // Note that we played around with setting the scale from 3-5, which looked better on high |
| 55 | // resolution monitors - but did not look good at all at lower resolutions. scale=2 seems |
| 56 | // to be the happy medium. |
| 57 | let viewport = page.getViewport(2 /* scale */); |
| 58 | let canvas = document.createElement("canvas") as HTMLCanvasElement; |
| 59 | let context = canvas.getContext("2d"); |
| 60 | canvas.height = viewport.height; |
| 61 | canvas.width = viewport.width; |
| 62 | |
| 63 | let renderContext = { |
| 64 | canvasContext: context, |
| 65 | viewport: viewport |
nothing calls this directly
no outgoing calls
no test coverage detected