首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Pdfjs打字本PDF加载但损坏

Pdfjs打字本PDF加载但损坏
EN

Stack Overflow用户
提问于 2018-10-05 08:21:02
回答 1查看 1.5K关注 0票数 0

在我的vue.js应用程序中,我有下面的类型记录类。

代码语言:javascript
复制
/** 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

页面3

从左边可以看出,在某种程度上有一些文字重叠。我不知道这是什么原因。

有什么想法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-10-16 08:19:49

这是由于没有加载pdf查看器css文件。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52661314

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档