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

Styling issue when trying to putting pdf viewer to right of html content #422

Closed
jgrant95 opened this issue Dec 11, 2018 · 7 comments
Closed
Milestone

Comments

@jgrant95
Copy link

jgrant95 commented Dec 11, 2018

Bug Report or Feature Request (mark with an x)
- [ ] Regression (a behavior that used to work and stopped working in a new release)
- [x] Bug report -> please search issues before submitting
- [ ] Feature request
- [ ] Documentation issue or request

So I'm trying to implement the pdf viewer into an existing angular project. However, for the time being I'm going to simply have to use an iframe to fit my needs. As currently in the pdf viewer there seems to be a styling issue - whether this is a simple fix I don't know as my styling knowledge isn't great.

The issue itself basically does not seem to visually load the pdf and is not visible whatsoever and has a spinner stuck on each page of the pdf.

I have reproduced this issue over in stackblitz. So if anyone gets a chance to take a look at this issue, I would much appreciate it.

https://stackblitz.com/edit/ng2-pdf-viewer-qdfyem

Thank you 😄

@ColinT
Copy link
Contributor

ColinT commented Dec 11, 2018

This appears to be an issue with mozilla/pdf.js.

I submitted my findings here: #10348.

Your iframe solution is more stable, but if you really don't want to use an iframe you can use a different workaround. Change your first col-6 into a new class with width: 49%;.

Example:

<!-- app.component.html -->
<h2>PDF Viewer</h2>

<div>
  <!-- Left content -->
  <div class="percent-49">
    <div class="content-example">
      Content Goes Here
    </div>

    <div class="button-example">Continue</div>
    <div class="button-example">Cancel</div>
  </div>

  <!-- Right content -->

  <pdf-viewer class="col-6" [src]="src" [original-size]="false" [autoresize]="true"></pdf-viewer>  
</div>
// app.component.scss
// ...
.percent-49 {
  width: 49%;
  float: left;
}
// ...

This works because then the left offset is less than the width of the PDFViewer, which passes their check for which pages to render.

@jgrant95
Copy link
Author

@ColinT Brilliant, amazing work! Thanks for your help and will keep an eye on the issue you've raised on Mozilla/pdf.js. Will be sticking with the iframe in the meantime.

Thank you again 😃

@ColinT
Copy link
Contributor

ColinT commented Dec 18, 2018

I found a stable way to make this work without an iframe, using position: relative; on a parent container:

<!-- app.component.html -->
<h2>PDF Viewer</h2>

<div>
  <!-- Left content -->
  <div class="col-6">
    <div class="content-example">
      Content Goes Here
    </div>

    <div class="button-example">Continue</div>
    <div class="button-example">Cancel</div>
  </div>

  <!-- Right content -->

  <div class="col-6" style="position:relative;">
    <pdf-viewer [src]="src" [original-size]="false" [autoresize]="true" style="display: block"></pdf-viewer>
  </div>
</div>

This works because pdfjs-dist uses element.offsetLeft to calculate whether pages are viewable. element.offsetLeft calculates its offset from the closest relatively positioned parent element (MDN reference).

Without the parent container, pdf-viewer will assume the closest parent is app-component and will calculate the viewable pages incorrectly, since it is offset 50% from the left. So we have to create a relatively positioned parent to correct the calculation.

Here is a stackblitz: https://stackblitz.com/edit/ng2-pdf-viewer-xqbb7v?file=app/app.component.html
You can see that removing the style="position:relative;" creates the bug you encountered.

@nithincn89
Copy link

@ColinT Thanks a lot. Your solution and explanation is really great..

@VadimDez VadimDez modified the milestones: 5.4.0, 6.0.0 Oct 10, 2019
@VadimDez
Copy link
Owner

@nkimadusanka
Copy link

We use the latest version of typescript 2. This library 6.0.0 onwards only support typescript 3. @ColinT solution is only fixed if you stuck under version 5.

@ja74687
Copy link

ja74687 commented Jun 16, 2021

Hey bug returned in version 7.0.1 when i back to 6.4.1 everything i ok :)

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

No branches or pull requests

6 participants