-
Notifications
You must be signed in to change notification settings - Fork 10k
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
Set page size via @page + size #5857
Conversation
// To make sure that the page fits on printed paper, points are converted to | ||
// pixels, the width is rounded UP, while the height is rounded DOWN. | ||
|
||
var cssPixelsPerPoint = 96 / 72; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this is a constant, please change the name to CSS_PIXELS_PER_POINT
.
Also, since the value is identical to CSS_UNITS
, could we just use that one instead?
Please do not merge this PR yet. I have received a test page of size (240x440 pt = 320.00 x 586.67px) from someone else by mail, which did render on two pages instead of one in Chrome's (41.0.2272.77) print preview after I set I'll try to improve my understanding of how the page sizes are determined/rounded/scaled in relation to the paper size. |
I have completely rewritten my patch, and verified that it works as expected (and also with the PDF that someone mailed me in private). @Snuffleupagus Could you review my code? See initial post for test steps. |
I'm sorry, but since I've never touched the printing code I don't feel comfortable doing the final review. |
@@ -1311,12 +1340,30 @@ var PDFViewerApplication = { | |||
//#endif | |||
}, | |||
|
|||
// Whether all pages of the PDF have the same width and height. | |||
hasEqualPageSizes: function pdfViewHasEqualPageSizes() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd suggest changing this to a getter instead, since you're not passing any parameters to the function and it only returns a boolean.
In Blink-based browsers, there is a mismatch between document size and paper size. Even if exactly the same values and unit are used, it is possible that the printed results takes more pages than expected. To solve the issue, the page size is set via @page size, and the canvas and ancestor nodes are assigned a width+height of 100% (=relative to the page). This change resolves bugs such as blank pages and split pages.
@Snuffleupagus Done. @brendandahl Could you review this PR? The code change is relatively small, you just need to make sure that I did not introduce regressions. Build PDF.js and run the following tests Test 1
Test 2
Test 3
|
Ping for review? See my previous comment for steps to test. |
} | ||
|
||
// Insert a @page + size rule to make sure that the page size is correctly | ||
// set. Note that we assume that all pages have the same size, because |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please explain more about why different page sizes aren't supported yet? Is it because we need to set different @page
on a per page basis and that may cause performance issues?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Variable page sizes are not supported by PDF.js because browsers do not offer the means to implement this (named pages). This is explained on the next few lines, and the relevant browser bugs have been referenced.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Gotcha. I understand that variable pages won't work but what about PDFs with pages that have landscape and portrait dimensions that other than a simple 90deg rotation will have the same dimensions? Anecdotally, I feel that these types of PDFs are common.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could create a separate feature request for that, and link to some PDFs for testing. pages cannot be targeted separately, but your scenario could be addressed by rotating the pages before printing.
If I'm not mistaken, pages with alternating landscape/portrait modes are not printed correctly, right? If so, then the acceptance of this patch won't be a regression.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah it isn't a regression, would you prefer a PR to your Rob--W:print-page-size
or to Mozilla?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest a new PR that refers to this discussion for context.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have PR the patch on #6103
Hey, could this PR be merged? The Notable PDF Chrome extension has already included integrated this patch in their product and a user told me that their extensions prints just fine (contrary to the unpatched PDF.js PDF Viewer). |
@yurydelendik Could you review this? |
The huge latency in reviewing is very disappointing... What can I do to speed up this process? I've since received multiple messages and feedback from users, who confirmed that this patch is working as intended. Therefore I'll be publishing an update of the Chrome extension of the master branch with this patch applied on top of it in a few minutes. |
I'm really sorry for the review delay. I have been very busy and only since yesterday I have had more time to do reviews (which resulted in four PRs landing already). I had hoped that @yurydelendik or @brendandahl would have time to review this PR in the meantime, but they have also been very busy on other Mozilla projects (for instance Shumway). I'm going to review and test this today and merge if everything is indeed working correctly. I agree with you that this needs to be fixed and that is has taken to long for the review to happen. Again, my apologies for that. |
@timvandermeij Thanks for your quick reply. Would it be okay if I review and merge PRs from others, and my own ones if they have been verified independently by others? I already have commit access to the PDF.js repository. |
@Rob--W You should discuss that with @yurydelendik and @brendandahl on IRC since they are the main developers. They are on IRC (#pdfjs on irc.mozilla.org) practically every day. |
Set page size via @page + size
I agree with the other people: this patch looks really good, solves the issues in Chrome and has no effect for the other browsers. Thank you very much! |
In Blink-based browsers, there is a mismatch between document size and paper size. Even if exactly the same values and unit are used, it is possible that the printed results takes more pages than expected.
To solve the issue, the page size is set via
@page
size, and the canvas and ancestor nodes are assigned a width+height of 100% (=relative to the page). This change resolves bugs such as blank pages and split pages.To test the result, open a PDF with A4 size, such as the ones in #5718 and open a print preview. No blanks pages should appear between the pages (in Firefox, a blank page shows up at the end, but this was already present before my patch).
Fixes #5718 (no blank pages, and the correct paper size is automatically selected if needed; only in Chrome, Firefox does not support
@page size yet
- https://bugzilla.mozilla.org/show_bug.cgi?id=851937).