def main():
import sys
import urllib2
from cStringIO import StringIO
from reportlab.platypus import SimpleDocTemplate, Image, Paragraph, PageBreak
from reportlab.lib.styles import ParagraphStyle, getSampleStyleSheet
#This is needed because ReportLab accepts the StringIO as a file-like object,
#but doesn't accept urllib2.urlopen's return value
def get_image(url):
u = urllib2.urlopen(url)
return StringIO(u.read())
styles = getSampleStyleSheet()
styleN = ParagraphStyle(styles['Normal'])
# build doc
if len(sys.argv) > 1:
fn = sys.argv[1]
else:
fn = "filename.pdf"
doc = SimpleDocTemplate(open(fn, "wb"))
elements = [
Paragraph("Hello,", styleN),
Image(get_image("http://www.red-dove.com/images/rdclogo.gif")),
PageBreak(),
Paragraph("world!", styleN),
Image(get_image("http://www.python.org/images/python-logo.gif")),
]
doc.build(elements, canvasmaker=NumberedCanvas)
评论列表
文章目录