question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

PDFs are getting rendered vertically mirrored

See original GitHub issue

Configuration:

  • Web browser and its version: Google Chrome Version 71.0.3578.98 (Official Build) (64-bit)
  • Operating system and its version: Tried on Ubuntu 18.04 and Windows 10
  • PDF.js version: 2.0.943 (current from npm)
  • Is a browser extension: no

Steps to reproduce the problem:

I’ve copied the code from https://mozilla.github.io/pdf.js/examples/#hello-world-with-document-load-error-handling and changed 2 things:

  1. added authorization headers (works fine when in jsfiddle)
  2. viewport.height and .width both are NaN, so I’ve added a fallback to viewBox-properties
var loadingTask = pdfjsLib.getDocument({
        url: url,
        httpHeaders: {
            'Authorization': server.headers.get('Authorization')
        }
    });
    loadingTask.promise.then(function (pdf) {
        console.log('PDF loaded');

        // Fetch the first page
        var pageNumber = 1;
        pdf.getPage(pageNumber).then(function (page) {
            console.log('Page loaded');

            var scale = 1.5;
            var viewport = page.getViewport({
                scale: scale
            });

            // Prepare canvas using PDF page dimensions
            var canvas = document.getElementById('the-canvas');
            var context = canvas.getContext('2d');
            canvas.height = viewport.height ||viewport.viewBox[3]; /* viewport.height is NaN */
            canvas.width = viewport.width || viewport.viewBox[2];  /* viewport.width is also NaN */

            // Render PDF page into canvas context
            var renderContext = {
                canvasContext: context,
                viewport: viewport
            };
            var renderTask = page.render(renderContext);
            renderTask.promise.then(function () {
                console.log('Page rendered');
            });
        });
    }, function (reason) {
        // PDF loading error
        console.error(reason);
    });

What is the expected behavior?

Correct Rendering of the PDF

What went wrong? (add screenshot)

screenshot from 2019-01-02 16-10-16

As you can see, the PDF is vertically mirrored. This is not a problem with this specific PDF, as it is rendered correctly here and the issue happens with any other PDFs I’ve tested.

I think it has something to do with viewport.heightand.widthbeingNaN` and/or the fact that the example uses a newer (2.1.something) version of PDF.js, but I’d like to use the npm package.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

13reactions
timvandermeijcommented, Jan 2, 2019

I think it may be because of:

var viewport = page.getViewport({
  scale: scale
});

Could you try with var viewport = page.getViewport(scale); to see if that works? If so, we need to make a new release because this recently changed and we updated the examples already.

6reactions
nleanbacommented, Jan 4, 2019

var viewport = page.getViewport(scale); fixed the problem, thank you.

Read more comments on GitHub >

github_iconTop Results From Across the Web

When I print my PDF document, it is showing as a mirror ...
Answer: In Adobe, select the 'Print' command and then select the 'Advanced' button within the print status window. Lastly, select the option 'Print...
Read more >
PDF.JS PDF is not rendering properly, appears mirrored and ...
The PDF is upside down and mirrored. I've looked around and a common cause of this is re-using the canvas for multiple renders,...
Read more >
Advanced PDF print settings - Acrobat Pro - Adobe Support
Mirrors the imageable area across the horizontal and vertical axes so that it is wrong reading. Type is readable when the photosensitive layer ......
Read more >
3 Free and Best Ways to Mirror PDF Easily - UPDF
1. Mirror the page without rasterizing the document ... The easiest method is to print from the PDF machine with Postscript mirroring. To...
Read more >
Downloading or delivering dashboards in rendered formats
By default, a PDF file mirrors what you see when you view the dashboard in your browser at a width of 1680 pixels....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found