-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
ENH: Add papersizes #800
Merged
Merged
ENH: Add papersizes #800
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
"""Helper to get paper sizes.""" | ||
|
||
from collections import namedtuple | ||
|
||
Dimensions = namedtuple("Dimensions", ["width", "height"]) | ||
|
||
|
||
class PaperSize(object): | ||
"""(width, height) of the paper in portrait mode in pixels at 72 ppi.""" | ||
|
||
# Notes how to calculate it: | ||
# 1. Get the size of the paper in mm | ||
# 2. Convert it to inches (25.4 millimeters are equal to 1 inches) | ||
# 3. Convert it to pixels ad 72dpi (1 inch is equal to 72 pixels) | ||
|
||
# All Din-A paper sizes follow this pattern: | ||
# 2xA(n-1) = A(n) | ||
# So the height of the next bigger one is the width of the smaller one | ||
# The ratio is always approximately the ratio 1:2**0.5 | ||
# Additionally, A0 is defined to have an area of 1 m**2 | ||
# Be aware of rounding issues! | ||
A0 = Dimensions(2384, 3370) # 841mm x 1189mm | ||
A1 = Dimensions(1684, 2384) | ||
A2 = Dimensions(1191, 1684) | ||
A3 = Dimensions(842, 1191) | ||
A4 = Dimensions( | ||
595, 842 | ||
) # Printer paper, documents - this is by far the most common | ||
A5 = Dimensions(420, 595) # Paperback books | ||
A6 = Dimensions(298, 420) # Post cards | ||
A7 = Dimensions(210, 298) | ||
A8 = Dimensions(147, 210) | ||
|
||
# Envelopes | ||
C4 = Dimensions(649, 918) | ||
|
||
|
||
_din_a = [ | ||
PaperSize.A0, | ||
PaperSize.A1, | ||
PaperSize.A2, | ||
PaperSize.A3, | ||
PaperSize.A4, | ||
PaperSize.A5, | ||
PaperSize.A6, | ||
PaperSize.A7, | ||
PaperSize.A8, | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from PyPDF2 import papersizes | ||
import pytest | ||
|
||
|
||
def test_din_a0(): | ||
dim = papersizes.PaperSize.A0 | ||
area_square_pixels = float(dim.width) * dim.height | ||
|
||
# 72 pixels is 1 inch | ||
area_square_inch = area_square_pixels / 72**2 | ||
|
||
# 25.4 millimeter is equal to 1 inches | ||
area_square_mm = area_square_inch * (25.4)**2 | ||
assert abs(area_square_mm - 999949) < 100 | ||
conversion_factor = 72 / 25.4 | ||
assert (dim.width - 841 * conversion_factor) < 1 | ||
assert (dim.width - 1189 * conversion_factor) < 1 | ||
|
||
|
||
|
||
@pytest.mark.parametrize("dimensions", papersizes._din_a) | ||
def test_din_a_ratio(dimensions): | ||
assert abs(dimensions.height - dimensions.width * 2**0.5) <= 2.5 | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"dimensions_a, dimensions_b", list(zip(papersizes._din_a, papersizes._din_a[1:])) | ||
) | ||
def test_din_a_doubling(dimensions_a, dimensions_b): | ||
assert abs(dimensions_a.height - 2 * dimensions_b.width) <= 4 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
In case anybody wonders about the weird float conversion: https://twitter.com/_martinthoma/status/1517767880686936065