Pdfjs 打字稿。 PDF 加载但已损坏
Pdfjs typescript. PDF loading but corrupted
我的 vue.js 应用程序中有以下打字稿 class。
/** Taken from https://github.com/VadimDez/ng2-pdf-viewer/blob/master/src/app/pdf-viewer/pdf-viewer.component.ts */
import { Component, Vue } from 'vue-property-decorator';
import { PDFDocumentProxy, PDFViewerParams, PDFSource, PDFPromise } from 'pdfjs-dist';
let PDFJS: any;
let PDFJSViewer: any;
PDFJS = require('pdfjs-dist/build/pdf');
PDFJSViewer = require('pdfjs-dist/web/pdf_viewer');
@Component
export default class FileViewer extends Vue {
public pdfLinkService: any;
public pdfViewer: any;
public pdfFindController: any;
private _renderText: boolean = true;
private _externalLinkTarget: string = 'blank';
private _pdf: PDFDocumentProxy;
private src: string | Uint8Array | PDFSource = "http://mozilla.github.io/pdf.js/web/compressed.tracemonkey-pldi-09.pdf";
private cMapsUrl: string = 'https://unpkg.com/pdfjs-dist@2.0.489/build/pdf.js';
mounted(){
this.setupViewer();
}
static getLinkTarget(type: string) {
switch (type) {
case 'blank':
return (<any>PDFJS).LinkTarget.BLANK;
case 'none':
return (<any>PDFJS).LinkTarget.NONE;
case 'self':
return (<any>PDFJS).LinkTarget.SELF;
case 'parent':
return (<any>PDFJS).LinkTarget.PARENT;
case 'top':
return (<any>PDFJS).LinkTarget.TOP;
}
return null;
}
static setExternalLinkTarget(type: string) {
const linkTarget = FileViewer.getLinkTarget(type);
if (linkTarget !== null) {
(<any>PDFJS).externalLinkTarget = linkTarget;
}
}
private getDocumentParams() {
const srcType = typeof(this.src);
// if (!this._cMapsUrl) {
// return this.src;
// }
const params: any = {
cMapUrl: this.cMapsUrl,
cMapPacked: true
};
if (srcType === 'string') {
params.url = this.src;
} else if (srcType === 'object') {
if ((this.src as any).byteLength !== undefined) {
params.data = this.src;
} else {
Object.assign(params, this.src);
}
}
return params;
}
loadPDF(){
const loadingTask: any = (PDFJS as any).getDocument(this.getDocumentParams());
(<PDFPromise<PDFDocumentProxy>>loadingTask.promise)
.then((pdf: PDFDocumentProxy) => {
if (this._pdf) {
this._pdf.destroy();
}
this._pdf = pdf;
this.pdfViewer.setDocument(pdf);
}, (error: any) => {
console.log(error);
});
}
destroy(){
this._pdf.destroy();
}
public setupViewer() {
(PDFJS as any).disableTextLayer = !this._renderText;
FileViewer.setExternalLinkTarget(this._externalLinkTarget);
this.pdfLinkService = new PDFJSViewer.PDFLinkService();
let container = document.getElementById('viewerContainer');
const pdfOptions: PDFViewerParams | any = {
container: container,
removePageBorders: false
// linkService: this.pdfLinkService
}
this.pdfViewer = new PDFJSViewer.PDFViewer(pdfOptions);
this.pdfLinkService.setViewer(this.pdfViewer);
this.pdfFindController = new PDFJSViewer.PDFFindController({ pdfViewer: this.pdfViewer });
this.pdfViewer.setFindController(this.pdfFindController);
this.loadPDF();
}
doSomething(){
this.pdfFindController.executeCommand('find', { query: 'trace', highlightAll: true });
}
}
有了这个,我得到了 pdf 加载但不是 100% 正确。如下图所示。
第 1 页
如您所见,它看起来非常好。但是问题从第2页开始。
第 2 页
第 3 页
从左侧可以看出,有些文本不知何故重叠。我不知道这可能是什么原因。
有什么想法吗?
这是因为没有加载 pdf viewer css 文件。
我的 vue.js 应用程序中有以下打字稿 class。
/** Taken from https://github.com/VadimDez/ng2-pdf-viewer/blob/master/src/app/pdf-viewer/pdf-viewer.component.ts */
import { Component, Vue } from 'vue-property-decorator';
import { PDFDocumentProxy, PDFViewerParams, PDFSource, PDFPromise } from 'pdfjs-dist';
let PDFJS: any;
let PDFJSViewer: any;
PDFJS = require('pdfjs-dist/build/pdf');
PDFJSViewer = require('pdfjs-dist/web/pdf_viewer');
@Component
export default class FileViewer extends Vue {
public pdfLinkService: any;
public pdfViewer: any;
public pdfFindController: any;
private _renderText: boolean = true;
private _externalLinkTarget: string = 'blank';
private _pdf: PDFDocumentProxy;
private src: string | Uint8Array | PDFSource = "http://mozilla.github.io/pdf.js/web/compressed.tracemonkey-pldi-09.pdf";
private cMapsUrl: string = 'https://unpkg.com/pdfjs-dist@2.0.489/build/pdf.js';
mounted(){
this.setupViewer();
}
static getLinkTarget(type: string) {
switch (type) {
case 'blank':
return (<any>PDFJS).LinkTarget.BLANK;
case 'none':
return (<any>PDFJS).LinkTarget.NONE;
case 'self':
return (<any>PDFJS).LinkTarget.SELF;
case 'parent':
return (<any>PDFJS).LinkTarget.PARENT;
case 'top':
return (<any>PDFJS).LinkTarget.TOP;
}
return null;
}
static setExternalLinkTarget(type: string) {
const linkTarget = FileViewer.getLinkTarget(type);
if (linkTarget !== null) {
(<any>PDFJS).externalLinkTarget = linkTarget;
}
}
private getDocumentParams() {
const srcType = typeof(this.src);
// if (!this._cMapsUrl) {
// return this.src;
// }
const params: any = {
cMapUrl: this.cMapsUrl,
cMapPacked: true
};
if (srcType === 'string') {
params.url = this.src;
} else if (srcType === 'object') {
if ((this.src as any).byteLength !== undefined) {
params.data = this.src;
} else {
Object.assign(params, this.src);
}
}
return params;
}
loadPDF(){
const loadingTask: any = (PDFJS as any).getDocument(this.getDocumentParams());
(<PDFPromise<PDFDocumentProxy>>loadingTask.promise)
.then((pdf: PDFDocumentProxy) => {
if (this._pdf) {
this._pdf.destroy();
}
this._pdf = pdf;
this.pdfViewer.setDocument(pdf);
}, (error: any) => {
console.log(error);
});
}
destroy(){
this._pdf.destroy();
}
public setupViewer() {
(PDFJS as any).disableTextLayer = !this._renderText;
FileViewer.setExternalLinkTarget(this._externalLinkTarget);
this.pdfLinkService = new PDFJSViewer.PDFLinkService();
let container = document.getElementById('viewerContainer');
const pdfOptions: PDFViewerParams | any = {
container: container,
removePageBorders: false
// linkService: this.pdfLinkService
}
this.pdfViewer = new PDFJSViewer.PDFViewer(pdfOptions);
this.pdfLinkService.setViewer(this.pdfViewer);
this.pdfFindController = new PDFJSViewer.PDFFindController({ pdfViewer: this.pdfViewer });
this.pdfViewer.setFindController(this.pdfFindController);
this.loadPDF();
}
doSomething(){
this.pdfFindController.executeCommand('find', { query: 'trace', highlightAll: true });
}
}
有了这个,我得到了 pdf 加载但不是 100% 正确。如下图所示。
第 1 页
如您所见,它看起来非常好。但是问题从第2页开始。
第 2 页
第 3 页
从左侧可以看出,有些文本不知何故重叠。我不知道这可能是什么原因。
有什么想法吗?
这是因为没有加载 pdf viewer css 文件。