Reportlab Userguide PDF
Reportlab Userguide PDF
User Guide
Table of contents
Table of contents
Table of contents Chapter 1 Introduction 2 6
1.1 About this document . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 1.2 What is the ReportLab PDF Library? . . . . . . . . . . . . . . . . . . . 6 1.3 ReportLab's commercial software . . . . . . . . . . . . . . . . . . . . . 7 1.4 What is Python? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 1.5 Acknowledgements . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 1.6 Installation and Setup . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 1.7 Getting Involved . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 1.8 Site Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 1.9 Learning More About Python . . . . . . . . . . . . . . . . . . . . . . . 9 1.10 Goals for the 2.x and 3.x release series . . . . . . . . . . . . . . . . . 9 1.11 What's New in ReportLab 2.7 . . . . . . . . . . . . . . . . . . . . . . . 9
10
2.1 Basic Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 2.2 More about the Canvas . . . . . . . . . . . . . . . . . . . . . . . . . . 10 2.3 Drawing Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 2.4 The tools: the "draw" operations . . . . . . . . . . . . . . . . . . . . . 12 2.5 The toolbox: the "state change" operations . . . . . . . . . . . . . . . 14 2.6 Other canvas methods. . . . . . . . . . . . . . . . . . . . . . . . . . . 16 2.7 Coordinates (default user space) . . . . . . . . . . . . . . . . . . . . . 16 2.8 Colors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 2.9 Color space checking . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 2.10 Color Overprinting . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 2.11 Standard fonts and text objects . . . . . . . . . . . . . . . . . . . . . 26 2.12 Text object methods . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 2.13 Paths and Lines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 2.14 Rectangles, circles, ellipses . . . . . . . . . . . . . . . . . . . . . . . 38 2.15 Bezier curves . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 2.16 Path object methods . . . . . . . . . . . . . . . . . . . . . . . . . . 41 2.17 Further Reading: The ReportLab Graphics Library . . . . . . . . . . . 46
47
User Guide
Table of contents
3.2 Automatic output font substitution . . . . . . . . . . . . . . . . . . . . 47 3.3 Using non-standard Type 1 fonts . . . . . . . . . . . . . . . . . . . . . 48 3.4 Standard Single-Byte Font Encodings . . . . . . . . . . . . . . . . . . 49 3.5 TrueType Font Support . . . . . . . . . . . . . . . . . . . . . . . . . . 51 3.6 Asian Font Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 3.7 RenderPM tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
54
4.1 Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 4.2 Links and Destinations . . . . . . . . . . . . . . . . . . . . . . . . . . 54 4.3 Outline Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 4.4 Page Transition Effects . . . . . . . . . . . . . . . . . . . . . . . . . . 56 4.5 Internal File Annotations . . . . . . . . . . . . . . . . . . . . . . . . . 56 4.6 Encryption . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
59
5.1 Design Goals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 5.2 Getting started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 5.3 Flowables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 5.4 Guidelines for flowable positioning . . . . . . . . . . . . . . . . . . . . 61 5.5 Frames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 5.6 Documents and Templates . . . . . . . . . . . . . . . . . . . . . . . . 63
Chapter 6 Paragraphs
6.1 Using Paragraph Styles
66
. . . . . . . . . . . . . . . . . . . . . . . . . 66
6.2 Paragraph XML Markup Tags . . . . . . . . . . . . . . . . . . . . . . 70 6.3 Intra-paragraph markup . . . . . . . . . . . . . . . . . . . . . . . . . 70 6.4 Bullets and Paragraph Numbering . . . . . . . . . . . . . . . . . . . . 73
75
7.1 Table User Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 7.2 TableStyle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 7.3 TableStyle User Methods . . . . . . . . . . . . . . . . . . . . . . . . . 76 7.4 TableStyle Commands . . . . . . . . . . . . . . . . . . . . . . . . . . 76
81
Page 3
User Guide
Table of contents
8.3 DocPara(self, expr, format=None, style=None, klass=None, escape=True)81 8.4 DocAssert(self, cond, format=None) . . . . . . . . . . . . . . . . . . . 81 8.5 DocIf(self, cond, thenBlock, elseBlock=[]) . . . . . . . . . . . . . . . . 81 8.6 DocWhile(self, cond, whileBlock) . . . . . . . . . . . . . . . . . . . . . 81
82
9.1 Preformatted(text, style, bulletText=None, dedent=0, maxLineLength=None, splitChars=None, newLineChars=None) . . . . 82 9.2 XPreformatted(text, style, bulletText=None, dedent=0, frags=None) . . 82 9.3 Image(filename, width=None, height=None) . . . . . . . . . . . . . . . 83 9.4 Spacer(width, height) . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 9.5 PageBreak() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 9.6 CondPageBreak(height) . . . . . . . . . . . . . . . . . . . . . . . . . 84 9.7 KeepTogether(flowables) . . . . . . . . . . . . . . . . . . . . . . . . . 84 9.8 TableOfContents() . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 9.9 SimpleIndex() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 9.10 ListFlowable(),ListItem() . . . . . . . . . . . . . . . . . . . . . . . . . 86
88
Chapter 11 Graphics
91
11.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 11.2 General Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 11.3 Charts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 11.4 Labels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 11.5 Axes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 11.6 Bar Charts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 11.7 Line Charts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104 11.8 Line Plots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 11.9 Pie Charts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107 11.10 Legends . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 11.11 Shapes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112 11.12 Widgets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
123
User Guide
Table of contents
Page 5
User Guide
Chapter 1 Introduction
Chapter 1 Introduction
1.1 About this document
This document is an introduction to the ReportLab PDF library. Some previous programming experience is presumed and familiarity with the Python Programming language is recommended. If you are new to Python, we tell you in the next section where to go for orientation. This manual does not cover 100% of the features, but should explain all the main concepts and help you get started, and point you at other learning resources. After working your way through this, you should be ready to begin writing programs to produce sophisticated reports. In this chapter, we will cover the groundwork: What is ReportLab all about, and why should I use it? What is Python? How do I get everything set up and running? We need your help to make sure this manual is complete and helpful. Please send any feedback to our user mailing list, which is signposted from www.reportlab.com.
Page 6
User Guide
Chapter 1 Introduction
1.5 Acknowledgements
Many people have contributed to ReportLab. We would like to thank in particular (in alphabetical order): Albertas Agejevas, Alex Buck, Andre Reitz, Andrew Mercer, Benjamin Dumke, Benn B, Chad Miller, Chris Lee, Christian Jacobs, Dinu Gherman, Eric Johnson, Felix Labrecque, Gary Poster, Germn M. Bravo, Guillaume Francois, Hans Brand, Henning Vonbargen, Hosam Aly, Ian Stevens, James Martin-Collar, Jeff Bauer, Jerome Alet, Jerry Casiano, Jorge Godoy, Keven D Smith, Magnus Lie Hetland, Marcel Tromp, Ty Sarna Marius Gedminas, Max M, Michael Egorov, Mike Folwell, Moshe Wagner, Nate Silva, Paul McNett, Peter Johnson, PJACock, Publio da Costa Melo, Randolph Bentson, Robert Alsina, Robert Hlzl, Robert Kern, Ron Peleg, Simon King, Steve Halasz, T Blatter, Tim Roberts, Tomasz Swiderski, Volker Haas, Yoann Roman, and many more. Special thanks go to Just van Rossum for his valuable assistance with font technicalities. Moshe Wagner and Hosam Aly deserve a huge thanks for contributing to the RTL patch, which is not yet on thr trunk. Marius Gedminas deserves a big hand for contributing the work on TrueType fonts and we are glad to include these in the toolkit. Finally we thank Michal Kosmulski for the DarkGarden font for and Bitstream Inc. for the Vera fonts.
Page 7
User Guide
Chapter 1 Introduction
Page 8
User Guide
Chapter 1 Introduction
pageComression: set to non-zero to try and get compressed PDF. allowtableBoundsErrors: set to 0 to force an error on very large Platypus table elements emptyTableAction: Controls behaviour for empty tables, can be 'error' (default), 'indicate' or 'ignore'.
Page 9
User Guide
The above code creates a canvas object which will generate a PDF file named hello.pdf in the current working directory. It then calls the hello function passing the canvas as an argument. Finally the showPage method saves the current page of the canvas and the save method stores the file and closes the canvas. The showPage method causes the canvas to stop drawing on the current page and any further operations will draw on a subsequent page (if there are any further operations -- if not no new page is created). The save method must be called after the construction of the document is complete -- it generates the PDF document, which is the whole purpose of the canvas object.
The filename argument controls the name of the final PDF file. You may also pass in any open file object (such as sys.stdout, the python process standard output) and the PDF document will be written to that. Since PDF is a binary format, you should take care when writing other stuff before or after it; you can't deliver PDF documents inline in the middle of an HTML page! The pagesize argument is a tuple of two numbers in points (1/72 of an inch). The canvas defaults to A4 (an international standard page size which differs from the American standard page size of letter), but it is better to explicitly specify it. Most common page sizes are found in the library module reportlab.lib.pagesizes, so you can use expressions like
from reportlab.lib.pagesizes import letter, A4 myCanvas = Canvas('myfile.pdf', pagesize=letter) width, height = letter #keep for later
NOTE
Page 10
User Guide
If you have problems printing your document make sure you are using the right page size (usually either A4 or letter). Some printers do not work well with pages that are too large or too small. Very often, you will want to calculate things based on the page size. In the example above we extracted the width and height. Later in the program we may use the width variable to define a right margin as width - inch rather than using a constant. By using variables the margin will still make sense even if the page size changes. The bottomup argument switches coordinate systems. Some graphics systems (like PDF and PostScript) place (0,0) at the bottom left of the page others (like many graphical user interfaces [GUI's]) place the origen at the top left. The bottomup argument is deprecated and may be dropped in future Need to see if it really works for all tasks, and if not then get rid of it The pageCompression option determines whether the stream of PDF operations for each page is compressed. By default page streams are not compressed, because the compression slows the file generation process. If output size is important set pageCompression=1, but remember that, compressed documents will be smaller, but slower to generate. Note that images are always compressed, and this option will only save space if you have a very large amount of text and vector graphics on each page. The encoding argument is largely obsolete in version 2.0 and can probably be omitted by 99% of users. Its default value is fine unless you very specifically need to use one of the 25 or so characters which are present in MacRoman and not in Winansi. A useful reference to these is here: http://www.alanwood.net/demos/charsetdiffs.html. The parameter determines which font encoding is used for the standard Type 1 fonts; this should correspond to the encoding on your system. Note that this is the encoding used internally by the font; text you pass to the ReportLab toolkit for rendering should always either be a Python unicode string object or a UTF-8 encoded byte string (see the next chapter)! The font encoding has two values at present: 'WinAnsiEncoding' or 'MacRomanEncoding'. The variable rl_config.defaultEncoding above points to the former, which is standard on Windows, Mac OS X and many Unices (including Linux). If you are Mac user and don't have OS X, you may want to make a global change: modify the line at the top of reportlab/pdfbase/pdfdoc.py to switch it over. Otherwise, you can probably just ignore this argument completely and never pass it. For all TTF and the commonly-used CID fonts, the encoding you pass in here is ignored, since the reportlab library itself knows the right encodings in those cases. The demo script reportlab/demos/stdfonts.py will print out two test documents showing all code points in all fonts, so you can look up characters. Special characters can be inserted into string commands with the usual Python escape sequences; for example \101 = 'A'. The verbosity argument determines how much log information is printed. By default, it is zero to assist applications which want to capture PDF from standard output. With a value of 1, you will get a confirmation message each time a document is generated. Higher numbers may give more output in future. The encrypt argument determines if and how the document is encrypted. By default, the document is not encrypted. If encrypt is a string object, it is used as the user password for the pdf. If encrypt is an instance of reportlab.lib.pdfencrypt.StandardEncryption, this object is used to encrypt the pdf. This allows more finegrained control over the encryption settings. Encryption is covered in more detail in Chapter 4. to do - all the info functions and other non-drawing stuff Cover all constructor arguments, and setAuthor etc.
Page 11
User Guide
c.setStrokeColorRGB(0.2,0.5,0.3) c.setFillColorRGB(1,0,1) # draw some lines c.line(0,0,0,1.7*inch) c.line(0,0,1*inch,0) # draw a rectangle c.rect(0.2*inch,0.2*inch,1*inch,1.5*inch, fill=1) # make text go straight up c.rotate(90) # change color c.setFillColorRGB(0,0,0.77) # say hello (note after rotate the y coord needs to be negative!) c.drawString(0.3*inch, -inch, "Hello World")
Examining this code notice that there are essentially two types of operations performed using a canvas. The first type draws something on the page such as a text string or a rectangle or a line. The second type changes the state of the canvas such as changing the current fill or stroke color or changing the current font type and size. If we imagine the program as a painter working on the canvas the "draw" operations apply paint to the canvas using the current set of tools (colors, line styles, fonts, etcetera) and the "state change" operations change one of the current tools (changing the fill color from whatever it was to blue, or changing the current font to Times-Roman in 15 points, for example). The document generated by the "hello world" program listed above would contain the following graphics.
Figure 2-1: "Hello World" in pdfgen About the demos in this document
This document contains demonstrations of the code discussed like the one shown in the rectangle above. These demos are drawn on a "tiny page" embedded within the real pages of the guide. The tiny pages are 5.5 inches wide and 3 inches tall. The demo displays show the actual output of the demo code. For convenience the size of the output has been reduced slightly.
Hello World
Page 12
User Guide
Line methods
canvas.line(x1,y1,x2,y2) canvas.lines(linelist)
Shape methods
canvas.grid(xlist, ylist) canvas.bezier(x1, y1, x2, y2, x3, y3, x4, y4) canvas.arc(x1,y1,x2,y2) canvas.rect(x, y, width, height, stroke=1, fill=0) canvas.ellipse(x1,y1, x2,y2, stroke=1, fill=0) canvas.wedge(x1,y1, x2,y2, startAng, extent, stroke=1, fill=0) canvas.circle(x_cen, y_cen, r, stroke=1, fill=0) canvas.roundRect(x, y, width, height, radius, stroke=1, fill=0)
The draw string methods draw single lines of text on the canvas.
Text objects are used to format text in ways that are not supported directly by the canvas interface. A program creates a text object from the canvas using beginText and then formats text by invoking textobject methods. Finally the textobject is drawn onto the canvas using drawText.
Path objects are similar to text objects: they provide dedicated control for performing complex graphical drawing not directly provided by the canvas interface. A program creates a path object using beginPath populates the path with graphics using the methods of the path object and then draws the path on the canvas Page 13
User Guide
using drawPath. It is also possible to use a path as a "clipping region" using the clipPath method -- for example a circular path can be used to clip away the outer parts of a rectangular image leaving only a circular part of the image visible on the page.
Image methods
You need the Python Imaging Library (PIL) to use images with the ReportLab package. Examnples of the techniques below can be found by running the script test_pdfgen_general.py in our tests subdirectory and looking at page 7 of the output. There are two similar-sounding ways to draw images. The preferred one is the drawImage method. This implements a caching system so you can define an image once and draw it many times; it will only be stored once in the PDF file. drawImage also exposes one advanced parameter, a transparency mask, and will expose more in future. The older technique, drawInlineImage, stores bitmaps within the page stream and is thus very inefficient if you use the same image more than once in a document; but can result in PDFs which render faster if the images are very small and not repeated. We'll discuss the oldest one first:
canvas.drawInlineImage(self, image, x,y, width=None,height=None)
NOTE NOTE
The drawInlineImage method places an image on the canvas. The image parameter may be either a PIL Image object or an image filename. Many common file formats are accepted including GIF and JPEG. It returns the size of the actual image in pixels as a (width, height) tuple.
canvas.drawImage(self, image, x,y, width=None,height=None,mask=None)
The arguments and return value work as for drawInlineImage. However, we use a caching system; a given image will only be stored the first time it is used, and just referenced on subsequent use. If you supply a filename, it assumes that the same filename means the same image. If you supply a PIL image, it tests if the content has actually changed before re-embedding. The mask parameter lets you create transparent images. It takes 6 numbers and defines the range of RGB values which will be masked out or treated as transparent. For example with [0,2,40,42,136,139], it will mask out any pixels with a Red value from 0 or 1, Green from 40 or 41 and Blue of 136, 137 or 138 (on a scale of 0-255). It's currently your job to know which color is the 'transparent' or background one. PDF allows for many image features and we will expose more of the over time, probably with extra keyword arguments to drawImage.
Ending a page
canvas.showPage()
The showPage method finishes the current page. All additional drawing will be done on another page. Warning! All state changes (font changes, color settings, geometry transforms, etcetera) are FORGOTTEN when you advance to a new page in pdfgen. Any state settings you wish to preserve must be set up again before the program proceeds with drawing!
Page 14
User Guide
Changing Colors
canvas.setFillColorCMYK(c, m, y, k) canvas.setStrikeColorCMYK(c, m, y, k) canvas.setFillColorRGB(r, g, b) canvas.setStrokeColorRGB(r, g, b) canvas.setFillColor(acolor) canvas.setStrokeColor(acolor) canvas.setFillGray(gray) canvas.setStrokeGray(gray)
PDF supports three different color models: gray level, additive (red/green/blue or RGB), and subtractive with darkness parameter (cyan/magenta/yellow/darkness or CMYK). The ReportLab packages also provide named colors such as lawngreen. There are two basic color parameters in the graphics state: the Fill color for the interior of graphic figures and the Stroke color for the boundary of graphic figures. The above methods support setting the fill or stroke color using any of the four color specifications.
Changing Fonts
canvas.setFont(psfontname, size, leading = None)
The setFont method changes the current text font to a given type and size. The leading parameter specifies the distance down to move when advancing from one text line to the next.
Lines drawn in PDF can be presented in a number of graphical styles. Lines can have different widths, they can end in differing cap styles, they can meet in different join styles, and they can be continuous or they can be dotted or dashed. The above methods adjust these various parameters.
Changing Geometry
canvas.setPageSize(pair) canvas.transform(a,b,c,d,e,f): canvas.translate(dx, dy) canvas.scale(x, y) canvas.rotate(theta)
Page 15
User Guide
canvas.skew(alpha, beta)
All PDF drawings fit into a specified page size. Elements drawn outside of the specified page size are not visible. Furthermore all drawn elements are passed through an affine transformation which may adjust their location and/or distort their appearence. The setPageSize method adjusts the current page size. The transform, translate, scale, rotate, and skew methods add additional transformations to the current transformation. It is important to remember that these transformations are incremental -- a new transform modifies the current transform (but does not replace it).
State control
canvas.saveState() canvas.restoreState()
Very often it is important to save the current font, graphics transform, line styles and other graphics state in order to restore them later. The saveState method marks the current graphics state for later restoration by a matching restoreState. Note that the save and restore method invokation must match -- a restore call restores the state to the most recently saved state which hasn't been restored yet. You cannot save the state on one page and restore it on the next, however -- no state is preserved between pages.
Page 16
User Guide
In the default user space the "origin" (0,0) point is at the lower left corner. Executing the coords function in the default user space (for the "demo minipage") we obtain the following.
(4, 2.5)
(2.5,1) in inches
Page 17
User Guide
(4, 2.5)
(2.5,1) in inches
Note: As illustrated in the example it is perfectly possible to draw objects or parts of objects "off the page". In particular a common confusing bug is a translation operation that translates the entire drawing off the visible area of the page. If a program produces a blank page it is possible that all the drawn objects are off the page.
NOTE
This produces a "short and fat" reduced version of the previously displayed operations.
Page 18
User Guide
(4, 2.5)
(2.5,1) in inches
Note: scaling may also move objects or parts of objects off the page, or may cause objects to "shrink to nothing." Scaling and translation can be combined, but the order of the operations are important.
def scaletranslate(canvas): from reportlab.lib.units import inch canvas.setFont("Courier-BoldOblique", 12) # save the state canvas.saveState() # scale then translate canvas.scale(0.3, 0.5) canvas.translate(2.4*inch, 1.5*inch) canvas.drawString(0, 2.7*inch, "Scale then translate") coords(canvas) # forget the scale and translate... canvas.restoreState() # translate then scale canvas.translate(2.4*inch, 1.5*inch) canvas.scale(0.3, 0.5) canvas.drawString(0, 2.7*inch, "Translate then scale") coords(canvas)
NOTE
This example function first saves the current canvas state and then does a scale followed by a translate. Afterward the function restores the state (effectively removing the effects of the scaling and translation) and then does the same operations in a different order. Observe the effect below.
Page 19
User Guide
(4, 2.5)
(4, 2.5)
(2.5,1) in inches
Note: scaling shrinks or grows everything including line widths so using the canvas.scale method to render a microscopic drawing in scaled microscopic units may produce a blob (because all line widths will get expanded a huge amount). Also rendering an aircraft wing in meters scaled to centimeters may cause the lines to shrink to the point where they disappear. For engineering or scientific purposes such as these scale and translate the units externally before rendering them using the canvas.
NOTE
Mirror image
It is interesting although perhaps not terribly useful to note that scale factors can be negative. For example the following function
def mirror(canvas): from reportlab.lib.units import inch canvas.translate(5.5*inch, 0) canvas.scale(-1.0, 1.0) coords(canvas)
Page 20
User Guide
)5.2 ,4(
sehcni ni )1,5.2(
2.8 Colors
There are generally two types of colors used in PDF depending on the media where the PDF will be used. The most commonly known screen colors model RGB can be used in PDF, however in professional printing another color model CMYK is mainly used which gives more control over how inks are applied to paper. More on these color models below.
RGB Colors
The RGB or additive color representation follows the way a computer screen adds different levels of the red, green, and blue light to make any color in between, where white is formed by turning all three lights on full (1,1,1). There are three ways to specify RGB colors in pdfgen: by name (using the color module, by red/green/blue (additive, RGB) value, or by gray level. The colors function below exercises each of the four methods.
def colorsRGB(canvas): from reportlab.lib import colors from reportlab.lib.units import inch black = colors.black y = x = 0; dy=inch*3/4.0; dx=inch*5.5/5; w=h=dy/2; rdx=(dx-w)/2 rdy=h/5.0; texty=h+2*rdy canvas.setFont("Helvetica",10) for [namedcolor, name] in ( [colors.lavenderblush, "lavenderblush"], [colors.lawngreen, "lawngreen"], [colors.lemonchiffon, "lemonchiffon"], [colors.lightblue, "lightblue"], [colors.lightcoral, "lightcoral"]): canvas.setFillColor(namedcolor) canvas.rect(x+rdx, y+rdy, w, h, fill=1) canvas.setFillColor(black) canvas.drawCentredString(x+dx/2, y+texty, name) x = x+dx y = y + dy; x = 0 for rgb in [(1,0,0), (0,1,0), (0,0,1), (0.5,0.3,0.1), (0.4,0.5,0.3)]: r,g,b = rgb canvas.setFillColorRGB(r,g,b) canvas.rect(x+rdx, y+rdy, w, h, fill=1) canvas.setFillColor(black)
Page 21
User Guide
canvas.drawCentredString(x+dx/2, y+texty, "r%s g%s b%s"%rgb) x = x+dx y = y + dy; x = 0 for gray in (0.0, 0.25, 0.50, 0.75, 1.0): canvas.setFillGray(gray) canvas.rect(x+rdx, y+rdy, w, h, fill=1) canvas.setFillColor(black) canvas.drawCentredString(x+dx/2, y+texty, "gray: %s"%gray) x = x+dx
gray: 0.0
gray: 0.25
gray: 0.5
gray: 0.75
gray: 1.0
r1 g0 b0
r0 g1 b0
r0 g0 b1
lavenderblush
lawngreen
lemonchiffon
lightblue
lightcoral
Page 22
User Guide
solid
transparent
Page 23
User Guide
c1 m0 y0 k0
c0 m1 y0 k0
c0 m0 y1 k0
c0 m0 y0 k1
c0 m0 y0 k0
Page 24
User Guide
overprint
knockout
SPUMONI
Figure 2-11: Painting over colors
Page 25