Permalink
Browse files

Merge branch 'bug/landscapeBackground' into develop

  • Loading branch information...
2 parents 7bc2b2f + 3c9f18f commit ff88cb11c87f3bebe0335abed9b3ab1d21d374f6 @kgrodzicki kgrodzicki committed Jul 28, 2011
Showing with 123 additions and 65 deletions.
  1. +76 −58 test/test-background-img.html
  2. +20 −0 test/testBackground.py
  3. +4 −1 xhtml2pdf/context.py
  4. +23 −6 xhtml2pdf/xhtml2pdf_reportlab.py
@@ -1,62 +1,80 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
-<head>
-<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
-<title>Background</title>
-<style>
-@page {
- background: url("img/denker.png");
- @frame text {
- top: 6cm;
- left: 4cm;
- right: 4cm;
- bottom: 4cm;
- -pdf-frame-border: 1;
- }
-}
-
-@page p2 {
- background: url("img/beach.jpg");
- @frame text {
- top: 6cm;
- left: 4cm;
- right: 4cm;
- bottom: 4cm;
- -pdf-frame-border: 1;
- }
-}
-
-@page p3 {
- background-image: url("pdf/background-sample.pdf");
- @frame text {
- top: 6cm;
- left: 4cm;
- right: 4cm;
- bottom: 4cm;
- -pdf-frame-border: 1;
- }
-}
-
-</style>
-</head>
-<body>
-
- <p>
- Hello <strong>World</strong>
- <p>
-
- <pdf:nextpage name="p2" />
-
- <p>
- Hello <strong>World</strong>
- <p>
-
- <pdf:nextpage name="p3" />
-
- <p>
- Hello <strong>World</strong>
- <p>
-
-</body>
+ <head>
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
+ <title>Background</title>
+ <style>
+ @page {
+ background: url("img/denker.png");
+ @frame text {
+ top: 6cm;
+ left: 4cm;
+ right: 4cm;
+ bottom: 4cm;
+ -pdf-frame-border: 1;
+ }
+ }
+
+ @page p2 {
+ background: url("img/beach.jpg");
+ @frame text {
+ top: 6cm;
+ left: 4cm;
+ right: 4cm;
+ bottom: 4cm;
+ -pdf-frame-border: 1;
+ }
+ }
+
+ @page p3 {
+ background-image: url("pdf/background-sample.pdf");
+ @frame text {
+ top: 6cm;
+ left: 4cm;
+ right: 4cm;
+ bottom: 4cm;
+ -pdf-frame-border: 1;
+ }
+ }
+
+ @page p4 {
+ background-image: url("img/beach.jpg");
+ size: a4 landscape;
+ @frame text {
+ top: 6cm;
+ left: 4cm;
+ right: 4cm;
+ bottom: 4cm;
+ -pdf-frame-border: 1;
+ }
+ }
+
+ </style>
+ </head>
+ <body>
+
+ <p>
+ Hello <strong>World</strong>
+ <p>
+
+ <pdf:nextpage name="p2" />
+
+ <p>
+ Hello <strong>World</strong>
+ <p>
+
+ <pdf:nextpage name="p3" />
+
+ <p>
+ Hello <strong>World</strong>
+ <p>
+
+ <pdf:nextpage name="p4" />
+
+ <p>
+ Hello <strong>World</strong>
+ <p>
+
+ </body>
</html>
@@ -0,0 +1,20 @@
+# -*- coding: utf-8 -*-
+#############################################
+## (C)opyright by Dirk Holtwick ##
+## All rights reserved ##
+#############################################
+
+__version__ = "$Revision: 176 $"
+__author__ = "$Author: kgrodzicki $"
+__date__ = "$Date: 2011-01-15 10:11:47 +0100 (Fr, 15 July 2011) $"
+
+"""
+HTML/CSS to PDF converter
+TODO add description
+"""
+
+from cookbook import HTML2PDF
+
+if __name__ == "__main__":
+ xhtml = open('test-background-img.html')
+ HTML2PDF(xhtml.read(), "testBackground.pdf")
@@ -209,12 +209,12 @@ def atPage(self, name, pseudopage, declarations):
if data.has_key("-pdf-page-size"):
c.pageSize = xhtml2pdf.default.PML_PAGESIZES.get(str(data["-pdf-page-size"]).lower(), c.pageSize)
+ isLandscape = False
if data.has_key("size"):
size = data["size"]
# print size, c.pageSize
if type(size) is not types.ListType:
size = [size]
- isLandscape = False
sizeList = []
for value in size:
valueStr = str(value).lower()
@@ -296,6 +296,9 @@ def atPage(self, name, pseudopage, declarations):
pt.pisaBackground = background
pt.pisaBackgroundList = c.pisaBackgroundList
+ if isLandscape:
+ pt.pageorientation = pt.LANDSCAPE
+
# self._pagesize)
# pt.pml_statics = self._statics
# pt.pml_draw = self._draw
@@ -184,12 +184,24 @@ def _has_template_for_name(self, name):
class PmlPageTemplate(PageTemplate):
+ PORTRAIT = 'portrait'
+ LANDSCAPE = 'landscape'
+ # by default portrait
+ pageorientation = PORTRAIT
+
def __init__(self, **kw):
self.pisaStaticList = []
self.pisaBackgroundList = []
self.pisaBackground = None
PageTemplate.__init__(self, **kw)
+ def isPortrait(self):
+ return self.pageorientation == self.PORTRAIT
+
+ def isLandscape(self):
+ return self.pageorientation == self.LANDSCAPE
+
+
def beforeDrawPage(self, canvas, doc):
canvas.saveState()
try:
@@ -213,12 +225,17 @@ def beforeDrawPage(self, canvas, doc):
wfactor = float(width) / iw
height = ph # min(ih, ph) # max
hfactor = float(height) / ih
- factor = min(wfactor, hfactor)
- w = iw * factor
- h = ih * factor
-
- canvas.drawImage(img, 0, ph - h, w, h)
- # print repr(img)
+ factor_min = min(wfactor, hfactor)
+
+ if self.isPortrait():
+ w = iw * factor_min
+ h = ih * factor_min
+ canvas.drawImage(img, 0, ph - h, w, h)
+ elif self.isLandscape():
+ factor_max = max(wfactor, hfactor)
+ w = ih * factor_max
+ h = iw * factor_min
+ canvas.drawImage(img, 0, 0, w, h)
except:
log.exception("Draw background")

0 comments on commit ff88cb1

Please sign in to comment.