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

Skip to content

Commit 6d0594a

Browse files
committed
Merge branch 'feature/evenOdd' into develop
2 parents ca0f785 + edfde58 commit 6d0594a

File tree

6 files changed

+223
-5
lines changed

6 files changed

+223
-5
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ testrender/output/
2020
.coverage
2121
htmlcov/
2222
xhtml2pdf.egg-info
23+
*.pdf

test/cookbook.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,8 @@
2424
Most people know how to write a page with HTML and CSS. Why not using these skills to dynamically generate PDF documents using it? The "pisa" project http://www.htmltopdf.org enables you to to this quite simple.
2525
"""
2626

27-
import cStringIO
28-
import ho.pisa as pisa
29-
import os
27+
from xhtml2pdf import pisa
28+
import cStringIO as StringIO
3029

3130
# Shortcut for dumping all logs to the screen
3231
pisa.showLogging()
@@ -40,7 +39,7 @@ def HTML2PDF(data, filename, open=False):
4039
"""
4140

4241
pdf = pisa.CreatePDF(
43-
cStringIO.StringIO(data),
42+
StringIO.StringIO(data),
4443
file(filename, "wb"))
4544

4645
if open and (not pdf.err):
@@ -72,4 +71,4 @@ def HTML2PDF(data, filename, open=False):
7271
</body></html>
7372
"""
7473

75-
HTML2PDF(HTMLTEST, "test.pdf", open=True)
74+
HTML2PDF(HTMLTEST, "test.pdf", open=False)

test/test-template-even-odd.html

Lines changed: 138 additions & 0 deletions
Large diffs are not rendered by default.

test/testEvenOddPage.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# -*- coding: utf-8 -*-
2+
#############################################
3+
## (C)opyright by Dirk Holtwick ##
4+
## All rights reserved ##
5+
#############################################
6+
7+
__version__ = "$Revision: 176 $"
8+
__author__ = "$Author: kgrodzicki $"
9+
__date__ = "$Date: 2011-01-15 10:11:47 +0100 (Fr, 15 July 2011) $"
10+
11+
"""
12+
HTML/CSS to PDF converter
13+
Test for support left/right (even/odd) pages
14+
"""
15+
16+
from cookbook import HTML2PDF
17+
18+
if __name__ == "__main__":
19+
xhtml = open('test-template-even-odd.html')
20+
HTML2PDF(xhtml.read(), "testEvenOdd.pdf")

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)