Drawing graphics in a PDF isn't very difficult with the ReportLab Toolkit. Let's go back to the pdfgen module and draw a few shapes on the page. Go ahead and create a new canvas to work with: >>> pdf = Canvas("graphics.pdf", pagesize = letter) We'll now draw a line that spans across the top of the page, leaving an inch on its left, right and top: >>> pdf.line(inch, inch * 10, inch * 7.5, inch * 10) We have the freedom to choose whatever colors we want for graphics. Here, we set the stroke color (the color outlining an image) to black and the fill color to lime green: >>> pdf.setStrokeColorRGB(0, 0, 0) Using these colors, we can draw shapes. Notice how we have the option of specifying whether or not we want to stroke or fill the image being drawn. If, however, we choose to omit these variables, the shape will be stroked but not filled: >>> pdf.rect(inch, inch, inch * 2, inch * 2, stroke = True, fill Save the page and then take a look at the result: >>> pdf.showPage() If the shape that you want isn't available, you can also draw it within the ReportLab Toolkit yourself, using paths. With paths, it's possible to draw lines from point to point to construct a shape. After that, you can stroke and fill the shape. Let's set colors, first. Our shape will be stroked with blue and filled with red: >>> pdf.setStrokeColorRGB(0, 0, 1) Next, we have to create a path object to be used: >>> path = pdf.beginPath() Before we begin drawing, let's move it to a starting point: >>> path.moveTo(inch * 4, inch * 4) Now let's get to the drawing part. We'll create three lines that form a triangle by using the lineTo method: >>> path.lineTo(inch * 3, inch * 4) When we're done with a path, we simply have to draw it. We can specify whether we want a stroke or a fill or both: >>> pdf.drawPath(path, True, True) Save the page and take a look at the triangle: >>> pdf.showPage() We're not limited to drawing everything by hand. It's also possible to draw existing images into a PDF document. For example, let's draw the DevShed symbol on a page:
Images can be drawn using the drawImage method of a canvas object, which also returns the image dimensions: >>> pdf.drawImage("devshed.jpg", inch, inch * 10) >>> from reportlab.platypus import Image Conclusion The Portable Document Format is very popular because of its ability to render pages that look exactly the same in many environments. There are many libraries out there that deal with generating PDF documents dynamically, and the ReportLab Toolkit is one of those libraries. In this article, we examined the low-level pdfgen module, which allows text and images to be positioned precisely on a page. While this approach is fine for many purposes, it becomes impractical when dealing with larger amounts of content. For those situations, Platypus is the tool of choice. It takes care of things such as word wrapping and page breaks for us, allowing us to spend more time on other things. You should now be familiar with the basics of the ReportLab Toolkit. From here, try to create scripts that generate dynamic PDF documents from data sources, such as text files (which we took a look at already, though you can certainly attempt to improve our script) and databases.
blog comments powered by Disqus |
|
|
|
|
|
|
|