Package sabx10 :: Package profiles :: Module profiles_pdf
[hide private]
[frames] | no frames]

Source Code for Module sabx10.profiles.profiles_pdf

 1  ############################################################################### 
 2  # 
 3  # sabx10 - an SABX file manipulation library 
 4  # Copyright (C) 2009, 2010 Jay Farrimond (jay@sabikerides.com) 
 5  # 
 6  # This file is part of sabx10. 
 7  # 
 8  # sabx10 is free software: you can redistribute it and/or modify it under the 
 9  # terms of the GNU General Public License as published by the Free Software 
10  # Foundation, either version 3 of the License, or (at your option) any later 
11  # version. 
12  # 
13  # sabx10 is distributed in the hope that it will be useful, but WITHOUT ANY 
14  # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 
15  # A PARTICULAR PURPOSE.  See the GNU General Public License for more details. 
16  # 
17  # You should have received a copy of the GNU General Public License along with 
18  # sabx10.  If not, see <http://www.gnu.org/licenses/>. 
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   
37 -def _large_profile_to_pdf(title, ride, in_dir, in_base, out_dir, out_base, 38 seg_index='all',):
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
76 -def ride_profiles_to_pdfs(xml_tree, prof_dir, prof_base, out_dir, out_base):
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