简体   繁体   English

PDFJS:PDF 文本渲染错误

[英]PDFJS: error on Text rendering for the PDF

Recently the PDF rendering get a messed up text layer where text gets duplicated with the grey colored overlay.最近,PDF 渲染得到一个混乱的文本层,其中文本被灰色覆盖层复制。 No idea about how to fix it as when i remove textLayerFactory: new pdfjsViewer.DefaultTextLayerFactory() it works fine.不知道如何修复它,因为当我删除textLayerFactory: new pdfjsViewer.DefaultTextLayerFactory()它工作正常。 but need this as if not is render as images which takes a lot of time for large documents但需要这个好像不是渲染为图像,这需要大量时间来处理大型文档

Im using pdfjsViewer.PDFPageView我使用 pdfjsViewer.PDFPageView

my code as follows我的代码如下

 getPdf() {

    var pdfDocument;

    if ( this._state !== 'inDOM' ) return false;

    pdfjsLib.disableRange = true;
    pdfjsLib.disableStream = true;

    let self = this;
    pdfDocument = pdfjsLib.getDocument(this.src);
    pdfDocument.promise.then(function(pdf) {
      self.set( 'pdfDocument', pdf );
      self.set( 'maxNumPages',  pdf.numPages );
      self.set( 'prevBtnDisabled', true );
      self.set( 'documentRendered', true );

      self.setViewportWidth();
      self.renderPdf();
    });

    return pdfDocument;
  },

  renderPdf() {

    var pdf = this.pdfDocument,
        maxNumPages,
        pagePromise;

    if ( !pdf ) return false;

    maxNumPages  = this.maxNumPages;

    pagePromise = this.getAndRenderPage( pdf, 1 );

    Array.apply( null, new Array( maxNumPages - 1 ) ).forEach( ( value, index ) => {

      pagePromise = pagePromise.then( () => this.getAndRenderPage( pdf, index + 2 ) );
    } );
  },

  getAndRenderPage( pdf, index ) {

    return pdf.getPage( index ).then( page => this.renderPage( page, index ) );
  },


  renderPage( pdfPage, pageNum ) {

    var parentWidth       = this.$().parent().width(),
        pageViewportScale = ( parentWidth >= this.get( 'breakpoints.mobile' ) ) ? 1.5 : 1.3,
        viewport          = pdfPage.getViewport( { scale: parentWidth / pdfPage.getViewport( { scale: pageViewportScale } ).width } ),
        container         = this.$().find( '.pdf_viewer--container' )[ 0 ],
        pdfPageView;

    pdfPageView = new pdfjsViewer.PDFPageView( {
      container: container,
      id: pageNum,
      scale: viewport.scale,
      defaultViewport: viewport,
     textLayerFactory: new pdfjsViewer.DefaultTextLayerFactory()

    } );
    var pages = this.get('pages');
    // Associates the actual page with the view, and drawing it
     pages.push( pdfPageView );
    this.set( 'pages', pages );
    this.set( 'scale', viewport.scale );z

    pdfPageView.setPdfPage( pdfPage );

    return pdfPageView.draw();
  },

i have seen same kind of questioned asked and its for angular Im importing his image as for the reference in here to give a more explanation about the issue我已经看到了同样的问题,它的角度我导入了他的图像作为参考在这里给出更多关于这个问题的解释

在此处输入图片说明

Reported Issue PDFJS: Text layer rendering twice报告的问题PDFJS:文本层渲染两次

in the new PDFjs, the CSS file needs to be added seperately from the node_modules folder.在新的 PDFjs 中,CSS 文件需要从 node_modules 文件夹中单独添加。 therefore i added this as因此我将其添加为

  app.import( 'node_modules/pdfjs-dist/web/pdf_viewer.css' );

and you can add this as a html import as well.您也可以将其添加为 html 导入。

<link rel="stylesheet" href="../../node_modules/pdfjs-dist/web/pdf_viewer.css">

more information about the example from PDFjs https://github.com/mozilla/pdf.js/blob/master/examples/components/pageviewer.html有关 PDFjs 示例的更多信息https://github.com/mozilla/pdf.js/blob/master/examples/components/pageviewer.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM