Skip to content
This repository
Newer
Older
100644 138 lines (113 sloc) 3.573 kb
1b1fd72e » knoguchi
2010-08-23 set coding: header to utf-8
1 # -*- coding: utf-8 -*-
881850e4 » holtwick
2010-06-17 First commit
2
3 # Copyright 2010 Dirk Holtwick, holtwick.it
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
29fb9938 » knoguchi
2010-08-23 removed trailing whitespace, CRLF to LF
8 #
881850e4 » holtwick
2010-06-17 First commit
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 __version__ = "$Revision: 194 $"
18 __author__ = "$Author: holtwick $"
19 __date__ = "$Date: 2008-04-18 18:59:53 +0200 (Fr, 18 Apr 2008) $"
20
21 import os
22 import sys
23 import cgi
24 import cStringIO
25 import logging
26
8f2ffb1c » elssar
2013-02-06 Fixed Python usage in usage, and in simple.py
27 import xhtml2pdf.pisa as pisa
881850e4 » holtwick
2010-06-17 First commit
28
29 # Shortcut for dumping all logs to the screen
30 pisa.showLogging()
31
32 def dumpErrors(pdf, showLog=True):
33 #if showLog and pdf.log:
34 # for mode, line, msg, code in pdf.log:
35 # print "%s in line %d: %s" % (mode, line, msg)
36 #if pdf.warn:
37 # print "*** %d WARNINGS OCCURED" % pdf.warn
38 if pdf.err:
39 print "*** %d ERRORS OCCURED" % pdf.err
40
41 def testSimple(
42 data="""Hello <b>World</b><br/><img src="img/test.jpg"/>""",
43 dest="test.pdf"):
44
45 """
46 Simple test showing how to create a PDF file from
47 PML Source String. Also shows errors and tries to start
48 the resulting PDF
49 """
50
51 pdf = pisa.CreatePDF(
52 cStringIO.StringIO(data),
53 file(dest, "wb")
54 )
55
56 if pdf.err:
57 dumpErrors(pdf)
58 else:
59 pisa.startViewer(dest)
60
61 def testCGI(data="Hello <b>World</b>"):
62
63 """
64 This one shows, how to get the resulting PDF as a
65 file object and then send it to STDOUT
66 """
67
68 result = cStringIO.StringIO()
69
70 pdf = pisa.CreatePDF(
71 cStringIO.StringIO(data),
72 result
73 )
74
75 if pdf.err:
76 print "Content-Type: text/plain"
77 print
78 dumpErrors(pdf)
79 else:
80 print "Content-Type: application/octet-stream"
81 print
82 sys.stdout.write(result.getvalue())
83
84 def testBackgroundAndImage(
85 src="test-background.html",
86 dest="test-background.pdf"):
87
88 """
89 Simple test showing how to create a PDF file from
90 PML Source String. Also shows errors and tries to start
91 the resulting PDF
92 """
93
94 pdf = pisa.CreatePDF(
95 file(src, "r"),
96 file(dest, "wb"),
97 log_warn = 1,
98 log_err = 1,
99 path = os.path.join(os.getcwd(), src)
100 )
101
102 dumpErrors(pdf)
103 if not pdf.err:
104 pisa.startViewer(dest)
105
106 def testURL(
107 url="http://www.htmltopdf.org",
108 dest="test-website.pdf"):
109
110 """
111 Loading from an URL. We open a file like object for the URL by
112 using 'urllib'. If there have to be loaded more data from the web,
113 the pisaLinkLoader helper is passed as 'link_callback'. The
114 pisaLinkLoader creates temporary files for everything it loads, because
115 the Reportlab Toolkit needs real filenames for images and stuff. Then
116 we also pass the url as 'path' for relative path calculations.
117 """
118 import urllib
119
120 pdf = pisa.CreatePDF(
121 urllib.urlopen(url),
122 file(dest, "wb"),
123 log_warn = 1,
124 log_err = 1,
125 path = url,
126 link_callback = pisa.pisaLinkLoader(url).getFileName
127 )
128
129 dumpErrors(pdf)
130 if not pdf.err:
131 pisa.startViewer(dest)
132
133 if __name__=="__main__":
134
135 testSimple()
136 # testCGI()
137 #testBackgroundAndImage()
138 #testURL()
Something went wrong with that request. Please try again.