Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sele mode so that each input file has an even number of pages #21

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,17 @@ Examples:
# reverse some of the pages in a.pdf by specifying a negative range
stapler sel a.pdf 1-3 9-6 10 output.pdf

The ``sele`` command is an extension of ``sel``. The usage is the same as that
of ``sel``, but ``sele`` inserts an empty page after each input file if that
input file has an odd number of pages so that each input file starts with an
even page number in the generated output. The empty page shall have the same
size as the page previous to it. You may find this mode useful if you read in
double-page layout or if you want to print the concatenated output.

The delete command works almost exactly the same as select, but inverse.
It uses the pages and ranges which you *didn't* specify.


split/burst:
~~~~~~~~~~~~

Expand Down
26 changes: 22 additions & 4 deletions staplelib/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@
from . import CommandError, iohelper
import staplelib


def select(args, inverse=False):
def select(args, inverse=False, even_page=False):
"""
Concatenate files / select pages from files.

inverse=True excludes rather than includes the selected pages from
the file.
even_page=True inserts an empty page at the end of each input
file if it ends with an odd page number.
"""

filesandranges = iohelper.parse_ranges(args[:-1])
Expand All @@ -28,6 +29,7 @@ def select(args, inverse=False):
raise CommandError("Both input and output filenames are required.")

output = PdfFileWriter()
pagecnt = 0
try:
for input in filesandranges:
pdf = input['pdf']
Expand All @@ -51,12 +53,18 @@ def select(args, inverse=False):
print "Using page: {} (rotation: {} deg.)".format(
pageno, rotate)

output.addPage(pdf.getPage(pageno-1)
.rotateClockwise(rotate))
page = pdf.getPage(pageno-1)
output.addPage(page.rotateClockwise(rotate))
pagecnt += 1
else:
raise CommandError("Page {} not found in {}.".format(
pageno, input['name']))

if even_page:
if pagecnt % 2 == 1:
output.addPage(iohelper.create_empty_page(page))
pagecnt += 1

except Exception, e:
raise CommandError(e)

Expand All @@ -66,6 +74,16 @@ def select(args, inverse=False):
iohelper.write_pdf(output, staplelib.OPTIONS.destdir +
os.sep + outputfilename)

def select_even(args, inverse=False):
"""
Concatenate files / select pages from files.

Inserts an empty page at the end of each input file if it ends with
an odd page number.
"""

select(args, inverse, True)


def delete(args):
"""Concatenate files and remove pages from files."""
Expand Down
25 changes: 25 additions & 0 deletions staplelib/iohelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
except ImportError:
from pyPdf import PdfFileWriter, PdfFileReader

from reportlab.lib import pagesizes
from reportlab.pdfgen import canvas
from StringIO import StringIO

from . import CommandError
import staplelib
Expand Down Expand Up @@ -124,3 +127,25 @@ def parse_ranges(files_and_ranges):
current['pages'].append((p, rotate))

return operations


def create_empty_page(refpage=None):
"""Create an empty page with the same size as refpage."""

if refpage is not None:
pagesize = refpage.mediaBox[-2:]
else:
pagesize = pagesizes.letter

c = canvas.Canvas(None)
c.setPageSize(pagesize)
c.showPage()

buf = StringIO()
buf.write(c.getpdfdata())
buf.seek(0)

reader = PdfFileReader(buf)
return reader.getPage(0)


3 changes: 2 additions & 1 deletion staplelib/stapler.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
usage: %prog [options] mode input.pdf ... [output.pdf]

Modes:
cat/sel: <inputfile> [<pagerange>[<rotation>]] ... (output needed)
cat/sel/sele: <inputfile> [<pagerange>[<rotation>]] ... (output needed)
Select the given pages/ranges from input files.
No range means all pages.
del: <inputfile> [<pagerange>[<rotation>]] ... (output needed)
Expand Down Expand Up @@ -68,6 +68,7 @@ def main():
modes = {
"cat": commands.select,
"sel": commands.select,
"sele": commands.select_even,
"split": commands.split,
"burst": commands.split,
"del": commands.delete,
Expand Down