Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rotate problem #8307

Closed
wanwan0306 opened this issue Apr 18, 2017 · 3 comments
Closed

rotate problem #8307

wanwan0306 opened this issue Apr 18, 2017 · 3 comments
Labels

Comments

@wanwan0306
Copy link

Configuration:
angular:4.0.2
pdfjs-dist:1.8.213

Hello.
my all pdf file only have one page.
when I click one name, it will show the corresponding pdf file.
but sometimes the pdf show will roate 180,sometimes normal.
and they are calling the same function.

this is my code:

<div class="pdfViewer" [ngStyle]="{'height': pdfHeight+'px'}">
    <div class="page" [ngStyle]="{'height': pageConfig.height+'px','width':pageConfig.width+'px'}">
      <!-- pdf canvas -->
      <div class="canvasWrapper">
        <canvas #page width="{{pageConfig.width}}" height="{{pageConfig.height}}"></canvas>
      </div>
      <!-- pdf text -->
      <div #textLayer class="textLayer" [ngStyle]="{'height': pageConfig.height+'px','width':pageConfig.width+'px'}"></div>
    </div>
  </div>
@ViewChild("textLayer") textLayer:ElementRef;
@ViewChild("page") page:ElementRef;
//draw pdf
drawPdf(data:any) {
  //this.currentPdfShow = data;
  let pdfjsDist = require("pdfjs-dist");
  let PDFJS = pdfjsDist.PDFJS;
  let loadingTask = PDFJS.getDocument('src/app/theme/components/spPdfShowNew/pdf/yb/wenshu/'+data+'.pdf');
  let self = this;
  loadingTask.promise.then((pdf) => {
    //pdf always have one page  永远都只是一页
    pdf.getPage(1).then((page) => {
      let pdfScale = (self.pafWidth * 0.8 - 80) / page.getViewport(1).width; //根据传过来的宽度来获取放大的倍数
      let viewport = page.getViewport(pdfScale,0);
      let canvas = self.page.nativeElement;
      let context = canvas.getContext('2d');
      context.clearRect(0,0,viewport.width,viewport.height);
     
     let renderContext = {
        canvasContext: context,
        viewport: viewport
      };
      page.render(renderContext);
      
      self.pageConfig['width'] = viewport.width;
      self.pageConfig['height'] = viewport.height;
      self.pdfHeight = viewport.height + 20;

      page.getTextContent({
        normalizeWhitespace: true,
      }).then((textContent) => {

        let textDivs = [];
        let textLayerFrag = document.createDocumentFragment();
        let textLayerRenderTask = pdfjsDist.renderTextLayer({
          textContent: textContent,
          container: textLayerFrag,
          viewport: viewport,
          textDivs: textDivs,
          timeout: self.TEXT_LAYER_RENDER_DELAY,
          enhanceTextSelection: false,
        });

        textLayerRenderTask.promise.then(function () {
          if (textLayerFrag) {
            self.textLayer.nativeElement.innerHTML = '';
            self.textLayer.nativeElement.appendChild(textLayerFrag);
          }
        }.bind(this), function (reason) {});

      })
    });
  }, function (reason) {console.error(reason);});
}
@timvandermeij
Copy link
Contributor

As mentioned in https://github.com/mozilla/pdf.js/blob/master/.github/ISSUE_TEMPLATE.md, you need to at least provide a PDF file that causes the issue and a minimal working version of your app hosted somewhere (JSBin, for example) for us to be able to look into the issue. Closing as incomplete for now.

@yurydelendik
Copy link
Contributor

let viewport = page.getViewport(pdfScale,0);

tells that you want your page without rotation (see 0), remove this arg if you want page to be rotated how PDF document defines it.

@gwl002
Copy link

gwl002 commented Sep 12, 2018

same problem.sometimes happen,sometimes not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants