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.

Page's originalHeight is zero for some pdf pages

See original GitHub issue

First of all, really appreciate everyone who contributes to this project šŸ™‡ .

Our team recently encountered an issue with getting the pagesā€™ width and height. To be more specific, we have a custom callback function thatā€™s passed as the onRenderSuccess prop to the <Page /> component. In that callback, we access the parameter that passed to the callback to get the dimensional information of the page such as height, width, etc. Something like this:

handleRenderSuccess = pageData => {
  const height = pageData.height;
  const width = pageData.width;
  // Do something else
}

render() {
  <Page
    onRenderSuccess={handleRenderSuccess}
    // Other props
  />
}

However we found out for very little pdfs, the pageData.height gives us 0 and so is pageData.originalHeight. Even if the page renders correctly without any error in the console. After some digging, we found the parameter passes to onRenderSuccess prop is generated by makePageCallback() in utils: https://github.com/wojtekmaj/react-pdf/blob/2df1e241e8ceee8d0aa9ed3f185ce860671f2daf/src/Page/PageCanvas.jsx#L59 Inside makePageCallback(), it gets the pageā€™s original width by this.view[2] and original height by this.view[3], then times the scale to get the width and height respectively. https://github.com/wojtekmaj/react-pdf/blob/2c0e179e6b485b3aa8ee5c343080f47b02aabbb4/src/shared/utils.js#L120-L126 The view is an array with four entries representing the ā€œmediaBoxā€ of the current page (https://github.com/mozilla/pdf.js/blob/e389ed6201df7955147dafda3a6813fff8ca5934/src/core/document.js#L173-L191). Conventionally, the first two numbers are the lower left x and y coordinates, and the last two numbers are the upper right x and y coordinates.

However, according to this pdf spec by Adobe (section 7.9.5), itā€™s also acceptable to specify the upper left x and y, and lower right x and y. It also recommends applications to take this into consideration.

We verify thatā€™s the reason for some pagesā€™ originalHeight to be zero by checking the view property. It gives us something like [0, 824.400024, 578.159973, 0]. Which we believe the first two numbers are the upper left x and y coordinates, and the last two numbers are lower left x and y coordinates.

I wonder has anybody else also encountered this issue? My suggestion for the fix will probably be updating makePageCallback() to:

 export const makePageCallback = (page, scale) => {
   const originalWidth = Math.max(this.view[0], this.view[2]);
   const originalHeight = Math.max(this.view[1], this.view[3]);
   Object.defineProperty(page, 'width', { get() { return originalWidth * scale; }, configurable: true }); 
   Object.defineProperty(page, 'height', { get() { return originalHeight * scale; }, configurable: true }); 
   Object.defineProperty(page, 'originalWidth', { get() { return originalWidth; }, configurable: true }); 
   Object.defineProperty(page, 'originalHeight', { get() { return originalHeight; }, configurable: true }); 
   return page; 
 }; 

Does this sound like the right approach? Iā€™m by no means an expert of pdf. Any feedback or suggestion are definitely welcome. Thank you for your time! Due to some privacy issue, Iā€™m not able to provide an example pdf. Apologize in advance.

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
wojtekmajcommented, Nov 23, 2020

This looks like a sensible approach. I wonder if we could generate some PDFs to create unit tests for these cases. This would give me much more confidence implementing this fix.

If anyone encountered a similar issue and can share the PDF having such issue, itā€™d be much appreciated.

0reactions
github-actions[bot]commented, Jan 31, 2022

This issue was closed because it has been stalled for 14 days with no activity.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Open pdf file at page 0 ? - Adobe Support Community
Sometimes when I and my client open PDF file with Acrobat 2017, it starts from Page 0 (zero), and shows nothing, then we...
Read more >
Can a PDF file have 0 pages defined or otherwise result in 0 ...
Sure, a PDF file is a container format that can contain pretty much anything, including (only) metadata with 0 pages.
Read more >
Resizing or Scaling PDF Pages (Paper Size and Content ...
Here is sample code to resize PDF Pages using Qoppa's library ... dimension is 0. if (originalWidth == 0 || originalHeight == 0)...
Read more >
Blank pages displayed when viewing large PDF - IBM
Troubleshooting scenario 5: PDF displays blank pages when you view a large document. Symptom: When you view PDF documents, pages that should contain...
Read more >
Expansive Soil Stabilization with Lime, Cement, and Silica Fume
Introduction. Plate-shaped clay particles can be assembled in many ways. The ability of some clay particles to attract and hold water molecules on...
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