Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 163c8a5

Browse files
committed
- added implementation for even/odd page
- use in your template: @page template {/* margin etc for main page */} @page template:left {/* left margin etc */} @page template:right {/* right margin etc */}
1 parent 2347e77 commit 163c8a5

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

xhtml2pdf/w3c/cssParser.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,7 @@ def _parseAtPage(self, src):
639639
page, src = self._getIdent(src)
640640
if src[:1] == ':':
641641
pseudopage, src = self._getIdent(src[1:])
642+
page = page + '_' + pseudopage
642643
else:
643644
pseudopage = None
644645

xhtml2pdf/xhtml2pdf_reportlab.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from reportlab.platypus.tables import Table, TableStyle
2727
from xhtml2pdf.reportlab_paragraph import Paragraph
2828
from xhtml2pdf.util import getUID, getBorderStyle
29+
from types import StringType, TupleType, ListType, IntType
2930
import StringIO
3031
import cgi
3132
import copy
@@ -49,6 +50,19 @@
4950

5051
MAX_IMAGE_RATIO = 0.95
5152

53+
class PTCycle(list):
54+
def __init__(self):
55+
self._restart = 0
56+
self._idx = 0
57+
list.__init__(self)
58+
59+
def cyclicIterator(self):
60+
while 1:
61+
yield self[self._idx]
62+
self._idx += 1
63+
if self._idx>=len(self):
64+
self._idx = self._restart
65+
5266
class PmlMaxHeightMixIn:
5367

5468
def setMaxHeight(self, availHeight):
@@ -114,6 +128,51 @@ def afterFlowable(self, flowable):
114128
cgi.escape(copy.deepcopy(flowable.text), 1),
115129
self.page))
116130

131+
def handle_nextPageTemplate(self,pt):
132+
''' if pt has also templates for even and odd page convert it to list '''
133+
if self._has_template_for_name(pt + '_left') and self._has_template_for_name(pt + '_right'):
134+
pt = [pt + '_left', pt + '_right']
135+
136+
'''On endPage change to the page template with name or index pt'''
137+
if type(pt) is StringType:
138+
if hasattr(self, '_nextPageTemplateCycle'): del self._nextPageTemplateCycle
139+
for t in self.pageTemplates:
140+
if t.id == pt:
141+
self._nextPageTemplateIndex = self.pageTemplates.index(t)
142+
return
143+
raise ValueError, "can't find template('%s')"%pt
144+
elif type(pt) is IntType:
145+
if hasattr(self, '_nextPageTemplateCycle'): del self._nextPageTemplateCycle
146+
self._nextPageTemplateIndex = pt
147+
elif type(pt) in (ListType, TupleType):
148+
#used for alternating left/right pages
149+
#collect the refs to the template objects, complain if any are bad
150+
c = PTCycle()
151+
for ptn in pt:
152+
if ptn=='*': #special case name used to short circuit the iteration
153+
c._restart = len(c)
154+
continue
155+
for t in self.pageTemplates:
156+
if t.id == ptn.strip():
157+
c.append(t)
158+
break
159+
if not c:
160+
raise ValueError("No valid page templates in cycle")
161+
elif c._restart>len(c):
162+
raise ValueError("Invalid cycle restart position")
163+
164+
#ensure we start on the first one$
165+
self._nextPageTemplateCycle = c.cyclicIterator()
166+
else:
167+
raise TypeError("Argument pt should be string or integer or list")
168+
169+
def _has_template_for_name(self, name):
170+
result = False
171+
for template in self.pageTemplates:
172+
if template.id == name.strip():
173+
result = True
174+
return result
175+
117176
class PmlPageTemplate(PageTemplate):
118177

119178
def __init__(self, **kw):

0 commit comments

Comments
 (0)