1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 """
22 Use Reportlab and the Image library to convert a profile PNG file into a PDF
23 file. We only bother with the large profiles.
24 """
25 import os.path
26
27 import Image
28 from reportlab.pdfgen import canvas
29 from reportlab.lib.units import inch
30 from reportlab.lib.pagesizes import letter
31
32 from sabx10.map import BORDER
33 from sabx10.oxm import process_rides, parse_top_level
34
35 from consts import LARGE_WIDTH, LARGE_HEIGHT
36
39 """
40 Take the large PNG profile for a ride and convert it to a PDF file with the
41 image embedded in it.
42
43 @param title: title base for PDF file
44 @type title: C{string}
45 @param ride: L{Ride} object to generate PDF for
46 @type ride: L{Ride} object
47 @param in_dir: directory to find PNG file in
48 @type in_dir: C{string}
49 @param in_base: base of name for PNG file
50 @type in_base: C{string}
51 @param out_dir: directory to put PDF file in
52 @type out_dir: C{string}
53 @param out_base: base of name for PDF file
54 @type out_base: C{string}
55 @param seg_index: index of segment in ride to generate PDF for
56 (default to all)
57 @type seg_index: C{string}
58 """
59 profile_name = os.path.join(in_dir, '%s_prof_large_%s_%s.png' %
60 (in_base, ride.index, seg_index))
61 im = Image.open(profile_name)
62 im = im.rotate(90)
63
64 pdf_file_name = os.path.join(out_dir, "%s_prof_%s.pdf" %
65 (out_base, ride.index))
66 page_width, page_height = letter
67 c = canvas.Canvas(pdf_file_name, pagesize=letter)
68 c.drawInlineImage(im, BORDER * inch, BORDER * inch,
69 LARGE_HEIGHT * inch, LARGE_WIDTH * inch)
70 c.rect(BORDER * inch, BORDER * inch, LARGE_HEIGHT * inch, LARGE_WIDTH * inch,
71 stroke=1, fill=0)
72 c.setTitle('%s - %.1f' % (title, ride.distance))
73 c.setAuthor('SABikeRides.com')
74 c.save()
75
77 """
78 Convert the large overall profile for each ride in a rideset into a PDF
79 file.
80
81 @param xml_tree: C{ElementTree} representation of a rideset
82 @type xml_tree: C{ElementTree} stuff
83 @param prof_dir: directory containing the profile PNG files
84 @type prof_dir: C{string}
85 @param prof_base: base name for profile PNG file names
86 @type prof_base: C{string}
87 @param out_dir: directory to put PDF files into
88 @type out_dir: C{string}
89 @param out_base: base name for PDF files
90 @type out_base: C{string}
91 """
92 sabx = parse_top_level(xml_tree)
93 ride_list, bounds = process_rides(xml_tree)
94 for ride in ride_list:
95 _large_profile_to_pdf(sabx['title'], ride,
96 prof_dir, prof_base, out_dir, out_base)
97