~ubuntu-branches/ubuntu/vivid/pdfposter/vivid-proposed

« back to all changes in this revision

Viewing changes to .pc/PyPDF2_migration.diff/test/gen-trimmedpage.py

  • Committer: Package Import Robot
  • Author(s): Elena Grandi
  • Date: 2014-09-18 18:51:17 UTC
  • Revision ID: package-import@ubuntu.com-20140918185117-41wdx3poidxhg5l6
Tags: 0.6.0-2
* Migrate from PyPdf to PyPDF2 (Closes: #763256)
* Update maintainer email
* debian/control: bump Standards-Version to 3.9.6 (no changes needed).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
# -*- coding: utf-8 -*-
 
3
#
 
4
# Copyright 2008-2013 by Hartmut Goebel <h.goebel@crazy-compilers.com>
 
5
#
 
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.
 
10
#
 
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.
 
15
#
 
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/>.
 
18
#
 
19
"""
 
20
Generate test PDF documents for pdfposter
 
21
"""
 
22
 
 
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)"
 
26
 
 
27
try:
 
28
    import cStringIO as StringIO
 
29
except ImportError:
 
30
    import StringIO
 
31
 
 
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
 
36
 
 
37
from pyPdf import PdfFileWriter, PdfFileReader
 
38
from pyPdf.generic import RectangleObject
 
39
 
 
40
def draw_box(canvas, color, x,y, width,height, text=None):
 
41
    canvas.setStrokeColorRGB(*color)
 
42
    canvas.rect(x,y, width,height)
 
43
    if text:
 
44
        canvas.setFillColorRGB(*color)
 
45
        canvas.setFont("Helvetica", 3*mm)
 
46
        canvas.drawString(x+1*mm, y+1*mm, text)  
 
47
 
 
48
def genTestFile(filename):
 
49
    stepSize = 2*cm
 
50
    bleed = 3*mm
 
51
    trimmargin = 2*cm
 
52
    cutmargin = trimmargin-bleed
 
53
 
 
54
    def genpage(canv, size,
 
55
                draw_trimbox, draw_bleedbox,
 
56
                set_trimbox, set_bleedbox, set_artbox,
 
57
                title):
 
58
        #--- draw the content
 
59
        # headline
 
60
        canv.setFillColor(lightblue)
 
61
        canv.rect(cutmargin, size[1]-cutmargin,
 
62
                  size[0]-2*cutmargin, -(4*cm+bleed),
 
63
                  fill=1, stroke=0)
 
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)
 
68
        offset = 14*cm
 
69
        for v, n in (
 
70
            (set_trimbox, 'trim box'),
 
71
            (set_bleedbox, 'bleed box'),
 
72
            (set_artbox, 'art box'),
 
73
            ):
 
74
            if v:
 
75
                t = '%s: set' % n
 
76
            else:
 
77
                t = '%s: not set' % n
 
78
            canv.drawString(6*cm, offset, t)
 
79
            offset += 5*mm
 
80
            
 
81
        # main content
 
82
        canv.setFont("Helvetica", 7*mm)
 
83
        offset = 6*cm
 
84
        for x in range(3):
 
85
            for y in range(3):
 
86
                text = u"%x%x" % (x,y)
 
87
                if (x+y) % 2:
 
88
                    canv.setFillColor(pink)
 
89
                else:
 
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,
 
97
                               "His is a caption")
 
98
 
 
99
        # draw the artbox
 
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")
 
102
        # draw trimbox
 
103
        if draw_trimbox:
 
104
            draw_box(canv, (0,1,0), 
 
105
                 trimmargin, trimmargin, size[0]-2*trimmargin, size[1]-2*trimmargin,
 
106
                 text="trim box")
 
107
        # draw bleedbox
 
108
        if draw_bleedbox:
 
109
            draw_box(canv, (0,0,1), 
 
110
                     cutmargin, cutmargin, size[0]-2*cutmargin, size[1]-2*cutmargin)
 
111
 
 
112
        # save the pdf file
 
113
        canv.showPage()
 
114
 
 
115
    def set_boxes(pageNum, size,
 
116
                draw_trimbox, draw_bleedbox,
 
117
                set_trimbox, set_bleedbox, set_artbox,
 
118
                title):
 
119
        page = reader.getPage(pageNum)
 
120
        print 'Page', i, size
 
121
        if set_bleedbox:
 
122
            page.bleedBox = RectangleObject(
 
123
                (cutmargin, cutmargin,
 
124
                 size[0]-cutmargin, size[1]-cutmargin))
 
125
            print ' bleed:', page.bleedBox
 
126
        if set_trimbox:
 
127
            page.trimBox = RectangleObject(
 
128
                (trimmargin, trimmargin,
 
129
                 size[0]-trimmargin, size[1]-trimmargin))
 
130
            print ' trim:', page.trimBox
 
131
        if set_artbox:
 
132
            page.artBox = RectangleObject(artRect)
 
133
            print ' art:', page.artBox
 
134
        writer.addPage(page)
 
135
 
 
136
    #----------- generate the first PDF w/o boxes defined -----------
 
137
    canv = Canvas(None, pagesize=A4)
 
138
 
 
139
    CASES = (
 
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'),
 
146
        )
 
147
    #--- draw the content
 
148
    for args in CASES:
 
149
        genpage(canv, *args)
 
150
    #canv.save() 
 
151
    pdfdata = canv.getpdfdata()
 
152
    
 
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):
 
158
        set_boxes(i, *args)
 
159
    outputStream = open(filename, "wb")
 
160
    writer.write(outputStream)
 
161
    outputStream.close()
 
162
 
 
163
if __name__ == '__main__':
 
164
    import argparse
 
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)