home · contact · privacy
Bookmaker: Reduce reportlab dependency to --nup4 usage.
[misc] / bookmaker.py
index 064537824e6c5211bc79868c31748bea24e3c3fb..e3e91886add8e1343aae7bc206280ccf6b169f6e 100755 (executable)
@@ -1,6 +1,6 @@
 #!/usr/bin/env python3
 """
 #!/usr/bin/env python3
 """
-bookmaker.py is a helper for optimizing PDFs of books for the production of small self-printed, self-bound physical books  Towards this goal it offers various PDF manipulation options that may also be used indepéndently and for other purposes.
+bookmaker.py is a helper for optimizing PDFs of books for the production of small self-printed, self-bound physical books.  Towards this goal it offers various PDF manipulation options that may also be used indepéndently and for other purposes.
 """
 import argparse
 import io
 """
 import argparse
 import io
@@ -13,14 +13,12 @@ try:
     import pypdf
 except ImportError:
     fail_with_msg("Can't run without pypdf installed.")
     import pypdf
 except ImportError:
     fail_with_msg("Can't run without pypdf installed.")
-try:
-    from reportlab.lib.pagesizes import A4
-except ImportError:
-    fail_with_msg("Can't run without reportlab installed.")
 
 # some constants
 
 # some constants
-A4_WIDTH, A4_HEIGHT = A4
 POINTS_PER_CM = 10 * 72 / 25.4
 POINTS_PER_CM = 10 * 72 / 25.4
+A4_WIDTH = 21 * POINTS_PER_CM
+A4_HEIGHT = 29.7 * POINTS_PER_CM
+A4 = (A4_WIDTH, A4_HEIGHT)
 CUT_DEPTH = 1.95 * POINTS_PER_CM
 CUT_WIDTH = 1.05 * POINTS_PER_CM
 MIDDLE_POINT_DEPTH = 0.4 * POINTS_PER_CM
 CUT_DEPTH = 1.95 * POINTS_PER_CM
 CUT_WIDTH = 1.05 * POINTS_PER_CM
 MIDDLE_POINT_DEPTH = 0.4 * POINTS_PER_CM
@@ -55,10 +53,10 @@ Initially declare 5cm crop from the left and 1cm crop from right, but alternate
 Quarter each OUTPUT.pdf page to carry 4 pages from INPUT.pdf, draw stencils into inner margins for cuts to carry binding strings:
     bookmaker.py -i INPUT.pdf -o OUTPUT.pdf --nup4
 
 Quarter each OUTPUT.pdf page to carry 4 pages from INPUT.pdf, draw stencils into inner margins for cuts to carry binding strings:
     bookmaker.py -i INPUT.pdf -o OUTPUT.pdf --nup4
 
-Same as --nup4, but define a printable-region margin of 1.3cm to limit the space for the INPUT.pdf pages in OUTPUT.pdf page quarters:
+Same --nup4, but define a printable-region margin of 1.3cm to limit the space for the INPUT.pdf pages in OUTPUT.pdf page quarters:
     bookmaker.py -i INPUT.pdf -o OUTPUT.pdf -n --print_margin 1.3
 
     bookmaker.py -i INPUT.pdf -o OUTPUT.pdf -n --print_margin 1.3
 
-Same as -n, but draw lines marking printable-region margins, page quarts, spine margins:
+Same --nup4, but draw lines marking printable-region margins, page quarts, spine margins:
     bookmaker.py -i INPUT.pdf -o OUTPUT.pdf -n --analyze
 
 NOTES:
     bookmaker.py -i INPUT.pdf -o OUTPUT.pdf -n --analyze
 
 NOTES:
@@ -73,9 +71,9 @@ The --nup4 quartering puts pages into a specific order optimized for no-tumble d
 
  (front)      (back)
 +-------+   +-------+
 
  (front)      (back)
 +-------+   +-------+
-| 4 | 1 |   | 3 | 2 |
+| 4 | 1 |   | 2 | 3 |
 |-------|   |-------|
 |-------|   |-------|
-| 8 | 5 |   | 7 | 6 |
+| 8 | 5 |   | 6 | 7 |
 +-------+   +-------+
 
 To facilitate this layout, --nup4 also pads the input PDF pages to a total number that is a multiple of 8, by adding empty pages if necessary.
 +-------+   +-------+
 
 To facilitate this layout, --nup4 also pads the input PDF pages to a total number that is a multiple of 8, by adding empty pages if necessary.
@@ -195,6 +193,11 @@ def parse_args():
 
 def main():
     args = parse_args()
 
 def main():
     args = parse_args()
+    if args.nup4:
+        try:
+            import reportlab
+        except ImportError:
+            raise ImportError("-n: need reportlab library installed for --nup4")
 
     # select pages from input files
     pages_to_add = []
 
     # select pages from input files
     pages_to_add = []
@@ -397,11 +400,10 @@ def main():
             print("merged page number %d (of %d)" % (page_count, len(pages_to_add)))
             i += 1
             if i > 3:
             print("merged page number %d (of %d)" % (page_count, len(pages_to_add)))
             i += 1
             if i > 3:
-                from reportlab.pdfgen import canvas
                 if args.analyze:
                     # borders
                     packet = io.BytesIO()
                 if args.analyze:
                     # borders
                     packet = io.BytesIO()
-                    c = canvas.Canvas(packet, pagesize=A4)
+                    c = reportlab.pdfgen.canvas.Canvas(packet, pagesize=A4)
                     c.setLineWidth(0.1)
                     c.line(0, A4_HEIGHT, A4_WIDTH, A4_HEIGHT)
                     c.line(0, half_height, A4_WIDTH, half_height)
                     c.setLineWidth(0.1)
                     c.line(0, A4_HEIGHT, A4_WIDTH, A4_HEIGHT)
                     c.line(0, half_height, A4_WIDTH, half_height)
@@ -420,7 +422,7 @@ def main():
                 x_right_SPINE_LIMIT = A4_WIDTH - x_left_SPINE_LIMIT
                 if args.analyze or front_page:
                     packet = io.BytesIO()
                 x_right_SPINE_LIMIT = A4_WIDTH - x_left_SPINE_LIMIT
                 if args.analyze or front_page:
                     packet = io.BytesIO()
-                    c = canvas.Canvas(packet, pagesize=A4)
+                    c = reportlab.pdfgen.canvas.Canvas(packet, pagesize=A4)
                 if args.analyze:
                     # # spine lines
                     c.setLineWidth(0.1)
                 if args.analyze:
                     # # spine lines
                     c.setLineWidth(0.1)
@@ -462,5 +464,5 @@ def main():
 if __name__ == "__main__":
     try:
         main()
 if __name__ == "__main__":
     try:
         main()
-    except ValueError as e:
+    except (ImportError, ValueError) as e:
         fail_with_msg(e)
         fail_with_msg(e)