Last active
December 18, 2015 16:08
-
-
Save chadcooper/5808828 to your computer and use it in GitHub Desktop.
Creating a PDF using flowables.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from reportlab.pdfgen.canvas import Canvas | |
from reportlab.lib.styles import getSampleStyleSheet | |
from reportlab.lib.units import inch | |
from reportlab.lib import colors | |
from reportlab.platypus import Paragraph, Frame, TableStyle, Table, Spacer, Image | |
from reportlab.lib.pagesizes import letter | |
import datetime | |
c = Canvas('doc.pdf', pagesize=letter) | |
f = Frame(inch, inch, 6.5*inch, 9*inch, showBoundary=0) | |
styles = getSampleStyleSheet() | |
styleN = styles['Normal'] | |
styleH = styles['Heading1'] | |
story = [] | |
#add some flowables | |
story.append(Paragraph("<b>VIESORE TPS Report</b>",styleH)) | |
story.append(Spacer(1, -0.3 * inch)) | |
story.append(Paragraph("_" * 45, styleH)) | |
# More header info in table | |
t = datetime.datetime.now() | |
d = [["Project name: Evil Empire", "Report created date: " + str(t)], | |
["No. of Kops: 3", "Report created time: " + str(t)]] | |
h_table = Table(d, 2 * [3.25 * inch], 2 * [0.2 * inch]) | |
h_table.setStyle((TableStyle([("ALIGN", (1, 0), (1, -1), "RIGHT")]))) | |
# Our table data | |
data = [["ATTRIBUTE", "VALUE", "ATTRIBUTE", "VALUE"]] | |
for i in range(1, 11): | |
data.append([str(i), str(i), str(i), str(i)]) | |
t = Table(data, colWidths=110) | |
t.setStyle(TableStyle([("BOX", (0, 0), (-1, -1), 0.25, colors.black), | |
("INNERGRID", (0, 0), (-1, -1), 0.25, colors.black), | |
("ALIGN", (1, 1), (-1, -1), "LEFT"), | |
("BACKGROUND", (0, 0), (3, 0), colors.darkgrey)])) | |
data_len = len(data) | |
for each in range(1, data_len): | |
if each % 2 == 0: | |
bg_color = colors.lightgrey | |
else: | |
bg_color = colors.white | |
t.setStyle(TableStyle([('BACKGROUND', (0, each), (-1, each), bg_color)])) | |
story.append(h_table) | |
story.append(Spacer(1, 0.2 * inch)) | |
story.append(t) | |
story.append(Spacer(1, 0.4 * inch)) | |
render = "image.jpg" | |
im = Image(render) | |
story.append(im) | |
f.addFromList(story,c) | |
c.save() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment