Getting Started with Reportlab
When you are positioning an item in a PDF, you are positioning by the number of points you are from the origin. It’s points, not pixels or millimeters or inches.
The Canvas Object
The showPage()
method will save the current page of the canvas. It’s actually not required, but it is recommended. The showPage()
method also ends the current page. If you draw another string or some other element after calling showPage()
, that object will be drawn to a new page.
Note that since we didn’t specify a page size, it defaults to whatever is in the ReportLab config, which is usually A4.
Let’s look at the Canvas’s constructor to see what it takes for arguments:
def __init__(
self,
filename,
pagesize=None,
bottomup=1,
pageCompression=None,
invariant=None,
verbosity=0,
encrypt=None,
cropMarks=None,
pdfVersion=None,
enforceColorSpace=None,
):
pass
Here we can see that we can pass in the pagesize
as an argument. The pagesize
is actually a tuple of width and height in points. If you want to change the origin from the default of bottom left, then you can set the bottomup
argument to 0, which will change the origin to the top left.
The pageCompression
argument is defaulted to zero or off. Basically it will tell ReportLab whether or not to compress each page. When compression is enabled, the file generation process is slowed. If your work needs your PDFs to be generated as quickly as possible, then you’ll want to keep the default of zero. However if speed isn’t a concern and you’d like to use less disk space, then you can turn on page compression. Note that images in PDFs will always be compressed, so the primary use case for turning on page compression is when you have a huge amount of text or lots of vector graphics per page.
The next argument is verbosity
, which is used for logging levels. At zero (0), ReportLab will allow other applications to capture the PDF from standard output. If you set it to one (1), a confirmation message will be printed out every time a PDF is created. There may be additional levels added, but at the time of writing, these were the only two documented.
The encrypt
argument is used to determine if the PDF should be encrypted as well as how it is encrypted. The default is obviously None, which means no encryption at all. If you pass a string to encrypt, that string will be the password for the PDF. If you want to encrypt the PDF, then you will need to create an instance of reportlab.lib.pdfencrypt.StandardEncryption
and pass that to the encrypt
argument. 加密的参数
The cropMarks
argument can be set to True, False or to an object. Crop marks are used by printinghouses to know where to crop a page. When you set cropMarks
to True in ReportLab, the page willbecome 3 mm larger than what you set the page size to and add some crop marks to the corners. Theobject that you can pass to cropMarks
contains the following parameters: borderWidth
, markColor
, markWidth
and markLength
. The object allows you to customize the crop marks. 剪裁标记