My favorites | Sign in
Project Logo
             
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# -*- coding: utf-8 -*-
# See LICENSE.txt for licensing terms

import sys
import os

from reportlab.platypus import Flowable, Paragraph
from reportlab.lib.enums import TA_LEFT, TA_CENTER, TA_RIGHT

from log import log

try:
for p in sys.path:
d = os.path.join(p, 'uniconvertor')
if os.path.isdir(d):
sys.path.append(d)
from app.io import load
from app.plugins import plugins
import app
from uniconvsaver import save
app.init_lib()
plugins.load_plugin_configuration()
break
else:
raise ImportError
except ImportError:
load = None

try:
from svglib import svglib
except ImportError:
svglib = None


class SVGImage(Flowable):

def __init__(self, filename, width=None, height=None, kind='direct'):
Flowable.__init__(self)
ext = os.path.splitext(filename)[-1]
self._kind = kind
# Prefer svglib for SVG, as it works better
if ext in ('.svg', '.svgz') and svglib is not None:
self._mode = 'svglib'
self.doc = svglib.svg2rlg(filename)
self.width = width
self.height = height
_, _, self._w, self._h = self.doc.getBounds()
if not self.width:
self.width = self._w
if not self.height:
self.height = self._h
# Use uniconvertor for the rest
elif load is not None:
self._mode = 'uniconvertor'
self.doc = load.load_drawing(filename)
self.saver = plugins.find_export_plugin(
plugins.guess_export_plugin('.pdf'))
self.width = width
self.height = height
_, _, self._w, self._h = self.doc.BoundingRect()
if not self.width:
self.width = self._w
if not self.height:
self.height = self._h
else:
self._mode = None
log.error("Vector image support not enabled,"
" please install svglib and/or uniconvertor.")
if self._mode:
self.__ratio = float(self.width)/self.height

def wrap(self, aW, aH):
if self._mode:
if self._kind == 'percentage_of_container':
w, h = self.width, self.height
if not w:
log.warning('Scaling image as % of container with w unset.'
'This should not happen, setting to 100')
w = 100
scale = w/100.
w = aW*scale
h = w/self.__ratio
self.width, self.height = w, h
return w, h
else:
return self.width, self.height
return 0, 0

def drawOn(self, canv, x, y, _sW=0):
if self._mode:
if _sW and hasattr(self, 'hAlign'):
a = self.hAlign
if a in ('CENTER', 'CENTRE', TA_CENTER):
x += 0.5*_sW
elif a in ('RIGHT', TA_RIGHT):
x += _sW
elif a not in ('LEFT', TA_LEFT):
raise ValueError("Bad hAlign value " + str(a))
canv.saveState()
canv.translate(x, y)
canv.scale(self.width/self._w, self.height/self._h)
if self._mode == 'uniconvertor':
save(self.doc, open('.ignoreme.pdf', 'w'), '.ignoreme.pdf',
options=dict(pdfgen_canvas=canv))
os.unlink('.ignoreme.pdf')
elif self._mode == 'svglib':
self.doc._drawOn(canv)
canv.restoreState()


if __name__ == "__main__":
from reportlab.platypus import SimpleDocTemplate
from reportlab.lib.styles import getSampleStyleSheet
doc = SimpleDocTemplate('svgtest.pdf')
styles = getSampleStyleSheet()
style = styles['Normal']
Story = [Paragraph("Before the image", style),
SVGImage(sys.argv[1]),
Paragraph("After the image", style)]
doc.build(Story)
Show details Hide details

Change log

r746 by roberto.alsina on Jul 01, 2009   Diff
pep8
Go to: 
Sign in to write a code review

Older revisions

r682 by c...@online.de on Jun 17, 2009   Diff
Some clean-up (mostly PEP-8) in
svgimage.
r679 by roberto.alsina on Jun 17, 2009   Diff
SVG images now handle % as a width
unit correctly.
r585 by roberto.alsina on May 25, 2009   Diff
add silly license info to python files
All revisions of this file

File info

Size: 4064 bytes, 120 lines
Hosted by Google Code