Skip to content

Instantly share code, notes, and snippets.

@roberto-arista
Created May 14, 2019 16:51
Show Gist options
  • Save roberto-arista/10591cb164b833b09af653ef892adf7f to your computer and use it in GitHub Desktop.
Save roberto-arista/10591cb164b833b09af653ef892adf7f to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
# coding: utf-8
# -------------- #
# impose booklet #
# -------------- #
### Modules
from os import mkdir
from os.path import basename, join, exists
import drawBot as dB
### Constants
FROM_MM_TO_PT = 2.834627813
CROP_LEN = 5*FROM_MM_TO_PT
CROP_DIS = 2*FROM_MM_TO_PT
### Variables - EDIT HERE!!!
fileName = 'booklet/drafts/20190412_booklet_v02.pdf'
sheetCanvas = 297*FROM_MM_TO_PT, 210*FROM_MM_TO_PT
pagesPerSignature = 16
outputFolder = 'imposed'
marks = False
bleed = {
'ver': 5*FROM_MM_TO_PT,
'hor': 5*FROM_MM_TO_PT,
}
### Instructions
inputPages = dB.numberOfPages(fileName)
assert inputPages % 4 == 0, 'check the amount of input pages'
assert inputPages % pagesPerSignature == 0, 'check the amount of input pages'
if exists(outputFolder) is False:
mkdir(outputFolder)
for signature in range(inputPages//pagesPerSignature):
startingPage = signature*pagesPerSignature
pages = list(range(startingPage+1, startingPage+pagesPerSignature+1))
dB.newDrawing()
for eachPage in range(pagesPerSignature//2):
dB.newPage(*sheetCanvas)
if eachPage % 2 == 1:
lftPageIndex = eachPage
rgtPageIndex = -(eachPage+1)
else:
rgtPageIndex = eachPage
lftPageIndex = -(eachPage+1)
lftPageNumber = pages[lftPageIndex]
rgtPageNumber = pages[rgtPageIndex]
imgWdt, imgHgt = dB.imageSize(fileName)
with dB.savedState():
dB.translate(dB.width()/2, dB.height()/2-imgHgt/2)
dB.image(fileName, (-imgWdt, 0), pageNumber=lftPageNumber)
dB.image(fileName, (0, 0), pageNumber=rgtPageNumber)
if marks is True:
with dB.savedState():
dB.cmykStroke(1, 1, 1, 1)
dB.translate(dB.width()/2, dB.height()/2)
# crop marks
for scaling in [(-1, -1), (1, -1), (-1, 1), (1, 1)]:
with dB.savedState():
dB.scale(*scaling)
dB.translate(imgWdt-bleed['hor'], imgHgt/2-bleed['ver'])
# btm lft
pt1 = CROP_DIS, 0
pt2 = CROP_DIS+CROP_LEN, 0
dB.line(pt1, pt2) # hor
pt3 = 0, CROP_DIS
pt4 = 0, CROP_LEN+CROP_DIS
dB.line(pt3, pt4) # ver
# fold marks
for scaling in [(1, 1), (1, -1)]:
with dB.savedState():
dB.scale(*scaling)
pt5 = 0, imgHgt/2 - bleed['ver'] + CROP_DIS
pt6 = 0, imgHgt/2 - bleed['ver'] + CROP_DIS + CROP_LEN
dB.line(pt5, pt6)
cleanFileName = basename(fileName).replace('.pdf', '')
dB.saveImage(join(outputFolder, f"{cleanFileName}_sign{signature+1:0>2d}.pdf"))
dB.endDrawing()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment