Calculating true three-fold PDF in Python
I created a three-column PDF format using frames and reportlab in the article Multi-column PDFs. However, the document I used as an example was meant to be folded, and that was not a true three-fold layout. All of the columns were the same width and they were distributed evenly within the available printing area of the page. In order to make it truly foldable, the borders between each frame need to be distributed evenly across the entire paper width. This means that, unless there are only two columns, the outer frames need to be sized differently than the inner frames.
I divided this into four basic steps:
- Add an inner margin size.
- Set the frame width to be the first fold point, minus the inner margin size.
- Adjust the first and last frame appropriately.
- Draw guides for folding.
I modified the frame code to handle steps one through three:
[toggle code]
- #create the basic page
- pagesize =reportlab.lib.pagesizes.landscape(reportlab.lib.pagesizes.letter)
- pagewidth, pageheight = pagesize
- leftMargin = .25*inch
- rightMargin = .25*inch
- topMargin = .25*inch
- bottomMargin = .25*inch
- innerMargin = .25*inch
- document = platypus.BaseDocTemplate(destination, pagesize=pagesize, leftMargin=leftMargin, rightMargin=rightMargin, topMargin=topMargin, bottomMargin=bottomMargin)
- #create the frames
- frameCount = 3
- foldAt = pagewidth/frameCount
- frameWidth = foldAt-innerMargin
- #leave space for the header
- frameHeight = document.height-.25*inch
- frames = []
- #construct the column frames
- #Note that if the frame width is less than twice the document margin, this will fail
-
for frame in range(frameCount):
- leftMargin = foldAt*frame
- thisWidth = frameWidth
- #adjust the margins appropriately
-
if frame == 0:
- leftMargin = leftMargin + document.leftMargin
- thisWidth = thisWidth - document.leftMargin + innerMargin/2
-
else:
- leftMargin = leftMargin + innerMargin/2
-
if frame == frameCount-1:
- thisWidth = thisWidth - document.rightMargin + innerMargin/2
- column = platypus.Frame(leftMargin, document.bottomMargin, thisWidth, frameHeight)
- frames.append(column)
- document.addPageTemplates(platypus.PageTemplate(frames=frames, onPage=addHeader))
- document.build(parts)
In this version, the leftmost and rightmost column are likely to be a different size than the rest of the columns, because they are also affected by the outer margins. Also, if you’re making pages with a lot of columns, you’ll want to make sure that your columns don’t start hitting negative widths after you apply the margin adjustments.
Drawing a fold guide
We can make it easier for people to fold the sheet by adding a dashed vertical line exactly at the fold.
[toggle code]
-
def addHeader(canvas, document):
- canvas.saveState()
- …
- #draw lines where the paper should be folded
- foldguidestart = pageheight*.48
- foldguideend = pageheight*.52
- #set to 25% gray
- canvas.setStrokeColorRGB(.75, .75, .75)
- #set to a 3-point dotted line
- canvas.setDash(3, 3)
-
for fold in range(1, frameCount):
- foldpoint = foldAt*fold
- canvas.line(foldpoint, foldguidestart, foldpoint, foldguideend)
- canvas.restoreState()
There’s nothing particularly special about this. It creates a line that is about 4% the height of the page, right in the middle of the page, and it does so at each fold point.
- ReportLab Toolkit
- “The ReportLab Open Source PDF library is a proven industry-strength PDF generating solution, that you can use for meeting your requirements and deadlines in enterprise reporting systems.”
More PDF
- Creating searchable PDFs in Ventura
- My searchablePDF script’s behavior changed strangely after upgrading to Ventura. All of the pages are generated at extremely low quality. This can be fixed by generating a JPEG representation before generating the PDF pages.
- Create searchable PDFs in Swift
- This Swift script will take a series of image scans, OCR them, and turn them into a PDF file with a simple table of contents and searchable content—with the original images as the visually readable content.
- Quality compressed PDFs in Mac OS X Lion
- The instructions for creating a “reduce PDF file size” filter in Lion are the same as for earlier versions of Mac OS X—except that for some reason ColorSync saves the filter in the wrong place (or, I guess, Preview is looking for them in the wrong place).
- Adding links to PDF in Python
- It is very easy to add links to PDF documents using reportlab or platypus in Python.
- Multiple column PDF generation in Python
- You can use ReportLab’s Platypus to generate multi-column PDFs in Snakelets, Django, or any Python app.
- Four more pages with the topic PDF, and other related pages
More Python
- Quick-and-dirty old-school island script
- Here’s a Python-based island generator using the tables from the Judges Guild Island Book 1.
- Astounding Scripts on Monterey
- Monterey removes Python 2, which means that you’ll need to replace it if you’re still using any Python 2 scripts; there’s also a minor change with Layer Windows and GraphicConverter.
- Goodreads: What books did I read last week and last month?
- I occasionally want to look in Goodreads for what I read last month or last week, and that currently means sorting by date read and counting down to the beginning and end of the period in question. This Python script will do that search on an exported Goodreads csv file.
- Test classes and objects in python
- One of the advantages of object-oriented programming is that objects can masquerade as each other.
- Timeout class with retry in Python
- In Paramiko’s ssh client, timeouts don’t seem to work; a signal can handle this—and then can also perform a retry.
- 30 more pages with the topic Python, and other related pages