bookletify now supports piping
This commit is contained in:
@@ -20,6 +20,7 @@ Questa repository contiene i seguenti script:
|
||||
- trim
|
||||
- diversi tipi di paper size (A4 di default, per poter mandare in stampa senza dover mettere "fit-to-page")
|
||||
- aggiunta di un bordo interno (a sinistra per le pagine dispari, a destra per le pagine pari) per la rilegatura
|
||||
- pipe da stdin / a stdout
|
||||
|
||||
Dipendenze: `PyPDF2`
|
||||
|
||||
|
||||
+41
-19
@@ -1,6 +1,8 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import os
|
||||
import sys
|
||||
import io
|
||||
import argparse
|
||||
from PyPDF2 import PageObject, PdfReader, PdfWriter, PaperSize, Transformation
|
||||
|
||||
@@ -13,19 +15,28 @@ def main():
|
||||
size on A4 paper"""
|
||||
|
||||
parser = argparse.ArgumentParser(prog=PROGRAM_NAME, description=DESCRIPTION)
|
||||
parser.add_argument('filename')
|
||||
parser.add_argument('input',
|
||||
help="""name of the input file to bookletify. Use '-'
|
||||
to read file from stdin""")
|
||||
parser.add_argument('output',
|
||||
help="""name of the output file to write to. Use '-' to
|
||||
write to stdout""")
|
||||
parser.add_argument('-q', '--quiet',
|
||||
action='store_true',
|
||||
help='suppress stdout')
|
||||
help="""suppress stdout. Not suppressed if the flag
|
||||
this flag is absent. Automatically suppressed if
|
||||
output is '-' (see output), to avoid broken
|
||||
pdfs""")
|
||||
parser.add_argument('-s', '--size',
|
||||
action='store',
|
||||
choices=['A0', 'A1', 'A2', 'A3', 'A4', 'A5', 'A6', 'A7', 'A8', 'C4'],
|
||||
choices=['A0', 'A1', 'A2', 'A3', 'A4', 'A5',
|
||||
'A6', 'A7', 'A8', 'C4'],
|
||||
default='A4',
|
||||
metavar='SIZE',
|
||||
help="""set final paper size. Possible values: 'A0',
|
||||
..., 'A8' or 'C4'. Default value is 'A4'""")
|
||||
parser.add_argument('-b', '--binding-margin',
|
||||
default=10,
|
||||
default=8.0,
|
||||
type=float,
|
||||
metavar='MARGIN',
|
||||
help="""internal margin for the binding, expressed in
|
||||
@@ -36,7 +47,7 @@ def main():
|
||||
decreasing this margin does not trim any content on
|
||||
the final pdf, the content gets scaled down (or up)
|
||||
to fit to the final width minus the binding margin.
|
||||
Default value is 10""")
|
||||
Default value is 8""")
|
||||
parser.add_argument('-t', '--trim',
|
||||
default=[0,0,0,0],
|
||||
type=float,
|
||||
@@ -54,21 +65,25 @@ def main():
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
if args.output == '-':
|
||||
# If output is written to stdout, suppress other stdout output such as
|
||||
# current state of the program and progress bars
|
||||
args.quiet = True
|
||||
|
||||
info = lambda text='', end='\n': print(text, end=end)
|
||||
if args.quiet:
|
||||
info = lambda text='', end='\n': None
|
||||
|
||||
prog = lambda curr, max: info(f"\r{curr} / {max}", '\n' if curr == max else '')
|
||||
|
||||
input_name = args.filename
|
||||
if not os.path.isfile(input_name):
|
||||
print(f'file {input_name} does not exist')
|
||||
exit(1)
|
||||
|
||||
output_name = f'{input_name}-rearranged'
|
||||
if (input_name.endswith('.pdf')):
|
||||
output_name = f'{input_name[0:-4]}-rearranged.pdf'
|
||||
|
||||
reader = None
|
||||
if args.input == '-':
|
||||
reader = PdfReader(sys.stdin.buffer)
|
||||
else:
|
||||
if not os.path.isfile(args.input):
|
||||
print(f'file {args.input} does not exist')
|
||||
exit(1)
|
||||
reader = PdfReader(args.input)
|
||||
|
||||
fullwidth = getattr(PaperSize, args.size).width
|
||||
fullheight = getattr(PaperSize, args.size).height
|
||||
@@ -76,7 +91,10 @@ def main():
|
||||
smallheight = fullwidth
|
||||
# Convert mm to pixels at 72 dpi
|
||||
bindingwidth = args.binding_margin * 72.0 / 25.4
|
||||
smallblank = PageObject.create_blank_page(width=smallwidth - bindingwidth, height=smallheight)
|
||||
smallblank = PageObject.create_blank_page(
|
||||
width=smallwidth - bindingwidth,
|
||||
height=smallheight
|
||||
)
|
||||
|
||||
tt = args.trim[0] * 72.0 / 25.4
|
||||
tr = args.trim[1] * 72.0 / 25.4
|
||||
@@ -84,11 +102,9 @@ def main():
|
||||
tl = args.trim[3] * 72.0 / 25.4
|
||||
|
||||
# Read the input
|
||||
reader = PdfReader(input_name)
|
||||
info("Reading...")
|
||||
n = len(reader.pages)
|
||||
pages = []
|
||||
|
||||
info("Reading...")
|
||||
for i in range(n):
|
||||
prog(i, n)
|
||||
pages.append(reader.pages[i])
|
||||
@@ -143,7 +159,13 @@ def main():
|
||||
prog(int(m/2), int(m/2))
|
||||
|
||||
info("\nSaving...")
|
||||
output.write(output_name)
|
||||
if args.output == '-':
|
||||
data = io.BytesIO()
|
||||
output.write(data)
|
||||
sys.stdout.buffer.write(data.getvalue())
|
||||
else:
|
||||
output.write(args.output)
|
||||
|
||||
info("\nDone!")
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user