Skip to content

Commit

Permalink
introduce --longarm / -l for long-arm stapler guides
Browse files Browse the repository at this point in the history
  • Loading branch information
pb- committed Oct 15, 2014
1 parent c5265db commit b324bf3
Showing 1 changed file with 57 additions and 32 deletions.
89 changes: 57 additions & 32 deletions mkbooklet
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,55 @@ startxref

return tpl % (ws, hs, 143 + l, 221 + l)

def create_pdf(content):
template = """%%PDF-1.5
%%%%%%%%%%
1 0 obj << /Kids [2 0 R] /Count 1 /Type /Pages >> endobj
3 0 obj << >> endobj
4 0 obj << /Length %d >>
stream
q
Q
%s
endstream
endobj
2 0 obj << /Group << /CS /DeviceRGB /Type /Group /S /Transparency >> /Parent 1 0 R /Resources 3 0 R /MediaBox [0 0 841.889764 595.275591] /Contents 4 0 R /Type /Page >> endobj
5 0 obj << /Pages 1 0 R /Type /Catalog >> endobj
6 0 obj << /Creator (mkbooklet http://github.com/pb-/mkbooklet) /Producer (mkbooklet http://github.com/pb-/mkbooklet) >> endobj
xref
0 7
0000000000 65535 f
0000000015 00000 n
%010d 00000 n
0000000074 00000 n
0000000097 00000 n
%010d 00000 n
%010d 00000 n
trailer
<< /Info 6 0 R /Root 5 0 R /Size 7 >>
startxref
%d
%%%%EOF""" % (4+len(content), content, 155+len(content), 351+len(content), 402+len(content), 531+len(content))
return template


def generate_guides_longarm():
width = 1
content = '2.83464567 0 0 2.83464567 0 0 cm 0.1 w 1 0 0 1 148.5 21 cm\n'

for i in range(5):
if i != 0:
content += "1 0 0 1 0 42 cm\n"

content += """
-%d 0 m
%d 0 l
s
""" % (width, 2*width)

return create_pdf(content)


def generate_guides(num, a5mode, sigmode):
vmargin = 6
Expand Down Expand Up @@ -140,36 +189,7 @@ s
s
""" % (h, ' ' if (a5mode or sigmode) else '-', ' ' if (a5mode or sigmode) else '-')

template = """%%PDF-1.5
%%%%%%%%%%
1 0 obj << /Kids [2 0 R] /Count 1 /Type /Pages >> endobj
3 0 obj << >> endobj
4 0 obj << /Length %d >>
stream
q
Q
%s
endstream
endobj
2 0 obj << /Group << /CS /DeviceRGB /Type /Group /S /Transparency >> /Parent 1 0 R /Resources 3 0 R /MediaBox [0 0 841.889764 595.275591] /Contents 4 0 R /Type /Page >> endobj
5 0 obj << /Pages 1 0 R /Type /Catalog >> endobj
6 0 obj << /Creator (mkbooklet http://github.com/pb-/mkbooklet) /Producer (mkbooklet http://github.com/pb-/mkbooklet) >> endobj
xref
0 7
0000000000 65535 f
0000000015 00000 n
%010d 00000 n
0000000074 00000 n
0000000097 00000 n
%010d 00000 n
%010d 00000 n
trailer
<< /Info 6 0 R /Root 5 0 R /Size 7 >>
startxref
%d
%%%%EOF""" % (4+len(content), content, 155+len(content), 351+len(content), 402+len(content), 531+len(content))
return template
return create_pdf(content)


def median(l):
Expand Down Expand Up @@ -201,10 +221,11 @@ parser.add_argument('-c', '--nocrop', action='store_true', default=False, help='
parser.add_argument('-C', '--croponly', action='store_true', default=False, help='only crop the input file, do not create a booklet')
parser.add_argument('-g', '--noguides', action='store_true', default=False, help='do not apply any guides on the first page')
parser.add_argument('-S', '--signature', dest='signature', type=int, action='store', default=None, help='use at most given number of sheets for one signature instead of producing one big signature')
parser.add_argument('-l', '--longarm', action='store_true', default=False, help='generate staple guides for long-arm staplers')
parser.add_argument('-s', '--smartbbox', action='store_true', default=False, help='use the smart algorithm when obtaining the bbox')
parser.add_argument('-p', '--bboxpage', dest='bboxpage', type=int, action='store', default=None, help='use given page only to obtain the bounding box')
parser.add_argument('-b', '--bbox', action='store', default=None, help='use given bounding box, specified as either x1,y1,x2,y2 or x,y+w,h')
parser.add_argument('--version', action='version', version='%(prog)s 11.10a')
parser.add_argument('--version', action='version', version='%(prog)s 14.10')
parser.add_argument('FILE')
args = parser.parse_args()

Expand All @@ -230,6 +251,7 @@ croponly = args.croponly
bboxpage = args.bboxpage
smartbbox = args.smartbbox
signature = args.signature
longarm = args.longarm

tmpdir = tempfile.mkdtemp(prefix='mkbooklet-')
os.chdir(tmpdir)
Expand Down Expand Up @@ -363,7 +385,10 @@ if not noguides and not croponly:
for i in range(pi.getNumPages()):
p = pi.getPage(i)
if i == 0:
grid = pyPdf.PdfFileReader(StringIO.StringIO(generate_guides(5, a5mode, bool(signature))))
if longarm:
grid = pyPdf.PdfFileReader(StringIO.StringIO(generate_guides_longarm()))
else:
grid = pyPdf.PdfFileReader(StringIO.StringIO(generate_guides(5, a5mode, bool(signature))))
p.mergePage(grid.getPage(0))
po.addPage(p)

Expand Down

0 comments on commit b324bf3

Please sign in to comment.