2
# -*- coding: utf-8 -*-
4
# Copyright 2008-2013 by Hartmut Goebel <h.goebel@crazy-compilers.com>
6
# This program is free software: you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation, either version 3 of the License, or
9
# (at your option) any later version.
11
# This program is distributed in the hope that it will be useful, but
12
# WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
# General Public License for more details.
16
# You should have received a copy of the GNU General Public License
17
# along with this program. If not, see <http://www.gnu.org/licenses/>.
20
Generate test PDF documents for pdfposter
23
__author__ = "Hartmut Goebel <h.goebel@crazy-compilers.com>"
24
__copyright__ = "Copyright 2008-2013 by Hartmut Goebel <h.goebel@crazy-compilers.com>"
25
__licence__ = "GNU General Public License version 3 (GPL v3)"
28
import cStringIO as StringIO
32
from reportlab.lib.units import mm, cm
33
from reportlab.lib.colors import black, white, pink, lightblue, blue
34
from reportlab.lib.pagesizes import A4, legal, landscape
35
from reportlab.pdfgen.canvas import Canvas
37
from pyPdf import PdfFileWriter, PdfFileReader
38
from pyPdf.generic import RectangleObject
40
def draw_box(canvas, color, x,y, width,height, text=None):
41
canvas.setStrokeColorRGB(*color)
42
canvas.rect(x,y, width,height)
44
canvas.setFillColorRGB(*color)
45
canvas.setFont("Helvetica", 3*mm)
46
canvas.drawString(x+1*mm, y+1*mm, text)
48
def genTestFile(filename):
52
cutmargin = trimmargin-bleed
54
def genpage(canv, size,
55
draw_trimbox, draw_bleedbox,
56
set_trimbox, set_bleedbox, set_artbox,
60
canv.setFillColor(lightblue)
61
canv.rect(cutmargin, size[1]-cutmargin,
62
size[0]-2*cutmargin, -(4*cm+bleed),
64
canv.setFillColor(black)
65
canv.setFont("Helvetica", 12*mm)
66
canv.drawCentredString(size[0]/2.0, size[1]-trimmargin-2.5*cm, title)
67
canv.setFont("Helvetica", 5*mm)
70
(set_trimbox, 'trim box'),
71
(set_bleedbox, 'bleed box'),
72
(set_artbox, 'art box'),
78
canv.drawString(6*cm, offset, t)
82
canv.setFont("Helvetica", 7*mm)
86
text = u"%x%x" % (x,y)
88
canv.setFillColor(pink)
90
canv.setFillColor(lightblue)
91
canv.rect(offset+x*stepSize, offset+y*stepSize,
92
stepSize, stepSize, fill=True)
93
canv.setFillColor(black)
94
canv.drawCentredString(offset+(x+0.5)*stepSize,
95
offset+(y+0.4)*stepSize, text)
96
canv.drawCentredString(size[0]/2.0, offset-1.7*cm,
100
artRect = (offset-1*cm, offset-2.5*cm, 2*cm+3*stepSize, 3.5*cm+3*stepSize)
101
draw_box(canv, (1,0,1), *artRect, text="art box")
104
draw_box(canv, (0,1,0),
105
trimmargin, trimmargin, size[0]-2*trimmargin, size[1]-2*trimmargin,
109
draw_box(canv, (0,0,1),
110
cutmargin, cutmargin, size[0]-2*cutmargin, size[1]-2*cutmargin)
115
def set_boxes(pageNum, size,
116
draw_trimbox, draw_bleedbox,
117
set_trimbox, set_bleedbox, set_artbox,
119
page = reader.getPage(pageNum)
120
print 'Page', i, size
122
page.bleedBox = RectangleObject(
123
(cutmargin, cutmargin,
124
size[0]-cutmargin, size[1]-cutmargin))
125
print ' bleed:', page.bleedBox
127
page.trimBox = RectangleObject(
128
(trimmargin, trimmargin,
129
size[0]-trimmargin, size[1]-trimmargin))
130
print ' trim:', page.trimBox
132
page.artBox = RectangleObject(artRect)
133
print ' art:', page.artBox
136
#----------- generate the first PDF w/o boxes defined -----------
137
canv = Canvas(None, pagesize=A4)
140
# size, draw, set, title
141
(A4, 1, 1, 1, 1,0,'Full size'),
142
((A4[0]/2, A4[1]), 1, 1, 1,1,0, 'Half width'),
143
((A4[0]/2, A4[1]), 0, 0, 1,1,0, 'Half width'),
144
((A4[0]/2, A4[1]), 1, 1, 0,0,0, 'Half width'),
145
((A4[0]/2, A4[1]), 0, 0, 0,0,0, 'Half width'),
147
#--- draw the content
151
pdfdata = canv.getpdfdata()
153
#----------- generate the second PDF w/ trimbox, bleedbox, artbox set
154
infile = StringIO.StringIO(pdfdata)
155
reader = PdfFileReader(infile)
156
writer = PdfFileWriter()
157
for i, args in enumerate(CASES):
159
outputStream = open(filename, "wb")
160
writer.write(outputStream)
163
if __name__ == '__main__':
165
parser = argparse.ArgumentParser()
166
parser.add_argument('filename',
167
help='Name of output file w/ box metrics')
168
args = parser.parse_args()
169
genTestFile(args.filename)