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

Set page size via @page + size #5857

Merged
merged 1 commit into from
May 14, 2015
Merged

Conversation

Rob--W
Copy link
Member

@Rob--W Rob--W commented Mar 18, 2015

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).

// 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;
Copy link
Collaborator

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?

@Rob--W
Copy link
Member Author

Rob--W commented Mar 20, 2015

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 @page { size: 240pt 440pt; }.

I'll try to improve my understanding of how the page sizes are determined/rounded/scaled in relation to the paper size.

@Rob--W Rob--W changed the title Use pixels instead of points for page size. Set page size via @page + size Mar 21, 2015
@Rob--W
Copy link
Member Author

Rob--W commented Mar 21, 2015

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.

@Snuffleupagus
Copy link
Collaborator

@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() {
Copy link
Collaborator

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.
@Rob--W
Copy link
Member Author

Rob--W commented Mar 23, 2015

@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

  1. Open http://kniel.de/download/96141656.02.pdf
  2. Click on the print button to generate a print preview in Chrome and/or print to a PDF file.
  3. Verify that two pages are previewed without blank pages, and without cut-off (A4 size).

Test 2

  1. Open https://dl.dropboxusercontent.com/u/7246095/example_RTL.pdf
  2. Repeat step 2-3 of test 1 (expect 5 pages).

Test 3

  1. Open the tracemonkey paper (not A4, probably US letter?)
  2. Repeat step 2-3 of test 1 (expect 14 pages).

@Rob--W
Copy link
Member Author

Rob--W commented Apr 28, 2015

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
Copy link
Contributor

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?

Copy link
Member Author

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.

Copy link
Contributor

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.

Copy link
Member Author

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.

Copy link
Contributor

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?

Copy link
Member Author

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.

Copy link
Contributor

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

@Rob--W
Copy link
Member Author

Rob--W commented May 9, 2015

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).

@timvandermeij
Copy link
Contributor

@yurydelendik Could you review this?

@Rob--W
Copy link
Member Author

Rob--W commented May 14, 2015

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.

@timvandermeij
Copy link
Contributor

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.

@Rob--W
Copy link
Member Author

Rob--W commented May 14, 2015

@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.

@timvandermeij
Copy link
Contributor

@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.

timvandermeij added a commit that referenced this pull request May 14, 2015
@timvandermeij timvandermeij merged commit d7aa95d into mozilla:master May 14, 2015
@timvandermeij
Copy link
Contributor

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!

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

Successfully merging this pull request may close these issues.

Printing
6 participants