-
Notifications
You must be signed in to change notification settings - Fork 18
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
feat: added tile command to cli #35
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,91 @@ | ||||||
""" | ||||||
Crope and merge PDF files into a single PDF as a layout of `xcount` x `ycount` tiles. | ||||||
|
||||||
Especially useful when layouting on A4 printable sticker paper. | ||||||
|
||||||
Currently only limited to A4 (portrait) input and output format. | ||||||
|
||||||
Use '-' to omit the file. | ||||||
|
||||||
`-x` and `-y` parameters specify the tiles size in relation to A4 format. | ||||||
|
||||||
A5: `-x 1 -y 2` (landscape) | ||||||
A6: `-x 2 -y 2` (portrait) - the default | ||||||
A7: `-x 2 -y 4` (landscape) | ||||||
A8: `-x 4 -y 4` (portrait) | ||||||
... | ||||||
|
||||||
Examples | ||||||
|
||||||
pdfly tiles -o out.pdf -x 2 -y 2 A.pdf B.pdf C.pdf D.pdf | ||||||
|
||||||
Merge the top left (A6) corners of provided PDFs (A4) as A6 tiles into a single page (A4). | ||||||
|
||||||
A | B | C | D | A | B | ||||||
--|-- --|-- --|-- --|-- ===> --|-- | ||||||
| | | | C | D | ||||||
|
||||||
|
||||||
pdfly tiles -o out.pdf - A.pdf B.pdf C.pdf | ||||||
|
||||||
Omitting one position by using `-` (also omitted -x 2 -y 2 as those are by default). | ||||||
|
||||||
A | B | C | | A | ||||||
--|-- --|-- --|-- ===> --|-- | ||||||
| | | B | C | ||||||
|
||||||
""" | ||||||
|
||||||
# Stanislav Ulrych <[email protected]>. | ||||||
|
||||||
import os | ||||||
import sys | ||||||
import traceback | ||||||
from pathlib import Path | ||||||
from typing import List | ||||||
|
||||||
from pypdf import PdfReader, PdfWriter, Transformation, PaperSize | ||||||
|
||||||
|
||||||
def main( | ||||||
output: Path, xcount: int, ycount: int, fn_pgrgs: List[str] | ||||||
) -> None: | ||||||
if output: | ||||||
output_fh = open(output, "wb") | ||||||
else: | ||||||
sys.stdout.flush() | ||||||
output_fh = os.fdopen(sys.stdout.fileno(), "wb") | ||||||
|
||||||
xsize = PaperSize.A4.width | ||||||
ysize = PaperSize.A4.height | ||||||
|
||||||
writer = PdfWriter() | ||||||
destPage = writer.add_blank_page(width=xsize, height=ysize) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please follow PEP-8:
Suggested change
|
||||||
try: | ||||||
for i in range(len(fn_pgrgs)): | ||||||
f = fn_pgrgs[i] | ||||||
if f == "-": | ||||||
continue | ||||||
|
||||||
reader = PdfReader(f) | ||||||
for p in reader.pages: | ||||||
t = { | ||||||
"tx": (i % xcount)*(xsize/xcount), | ||||||
"ty": -(i // xcount)*(ysize/ycount) | ||||||
} | ||||||
print(t) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
p.add_transformation(Transformation().translate(**t)) | ||||||
destPage.merge_page(p) | ||||||
|
||||||
writer.write(output_fh) | ||||||
except Exception: | ||||||
print(traceback.format_exc(), file=sys.stderr) | ||||||
print(f"Error while reading {f}", file=sys.stderr) | ||||||
sys.exit(1) | ||||||
finally: | ||||||
output_fh.close() | ||||||
# In 3.0, input files must stay open until output is written. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't that true for Python 2.x as well ... and any programming language for that matter?
Suggested change
|
||||||
# Not closing the in_fs because this script exits now. | ||||||
|
||||||
|
||||||
|
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.
Please don't add such comments