Downloadpdfmeta 1233
Downloadpdfmeta 1233
Downloadpdfmeta 1233
https://ebookmeta.com/product/image-processing-masterclass-with-
python-50-solutions-and-techniques-1st-edition-dey/
https://ebookmeta.com/product/primary-mathematics-3a-hoerst/
https://ebookmeta.com/product/introduction-to-digital-music-with-
python-programming-learning-music-with-code-1st-edition-michael-
horn/
https://ebookmeta.com/product/productive-and-efficient-data-
science-with-python-with-modularizing-memory-profiles-and-
parallel-gpu-processing-1st-edition-tirthajyoti-sarkar/
Natural Language Processing in Action Understanding
analyzing and generating text with Python 1st Edition
Hobson Lane
https://ebookmeta.com/product/natural-language-processing-in-
action-understanding-analyzing-and-generating-text-with-
python-1st-edition-hobson-lane/
https://ebookmeta.com/product/practical-natural-language-
processing-with-python-with-case-studies-from-industries-using-
text-data-at-scale-1st-edition-mathangi-sri-2/
https://ebookmeta.com/product/practical-natural-language-
processing-with-python-with-case-studies-from-industries-using-
text-data-at-scale-1st-edition-mathangi-sri/
https://ebookmeta.com/product/mastering-python-forensics-master-
the-art-of-digital-forensics-and-analysis-with-python-1st-
edition-michael-spreitzenbarth-johann-uhrmann/
https://ebookmeta.com/product/practical-channel-aware-resource-
allocation-with-matlab-and-python-code-michael-ghorbanzadeh/
ReportLab - PDF Processing with Python
Michael Driscoll
*****
This is a Leanpub book. Leanpub empowers authors and publishers with the
Lean Publishing process. Lean Publishing is the act of publishing an in-progress
ebook using lightweight tools and many iterations to get reader feedback, pivot
until you have the right book and build traction once you do.
*****
In this book, you will learn how to use Reportlab to create PDFs too. This book
will be split into three sections. We will be covering the following topics in the
first section:
The canvas
Drawing
Working with fonts
PLATYPUS
Paragraphs
Tables
Other Flowables
Graphics
and More!
In the second section, we will learn about data processing. The idea here is to
take in several different data formats and turn them into PDFs. For example, it is
quite common to receive data in XML or JSON. But learning how to take that
information and turn it into a report is something that isn’t covered very often.
You will learn how to do that here. In the process we will discover how to make
multipage documents with paragraphs and tables that flow across the pages
correctly.
The last section of the book will cover some of the other libraries you might
need when working with PDFs with Python. In this section we will learn about
the following:
PyPDF2
pdfminer
PyFPDF
I realized that one way for me to remember how to do certain things in Python
was to write about them and that’s how my Python blog came about:
http://www.blog.pythonlibrary.org/. As I wrote, I would receive feedback from
my readers and I ended up expanding the blog to include tips, tutorials, Python
news, and Python book reviews. I work regularly with Packt Publishing as a
technical reviewer, which means that I get to try to check for errors in the books
before they’re published. I also have written for the Developer Zone (DZone)
and i-programmer websites as well as the Python Software Foundation. In
November 2013, DZone published The Essential Core Python Cheat Sheet
that I co-authored. I have also self-published the following books:
The >>> is a Python prompt symbol. You will see this in the Python interpreter
and in IDLE. Other code examples will be shown in a similar manner, but
without the >>>. Most of the book will be done creating examples in regular
Python files, so you won’t be seeing the Python prompt symbol all that often.
When you are using a Python Virtual Environment, you will need to first activate
it. Activation of a virtual environment is like starting a virtual machine up in
VirtualBox or VMWare, except that in this case, it’s just a Python Virtual
Environment instead of an entire operating system.
Creating a virtual sandbox with the virtualenv package is quite easy. On Mac and
Linux, all you need to do is the following in your terminal or command prompt:
virtualenv FOLDER_NAME
You should still change directories into your new folder, but instead of bin, there
will be a Scripts folder that can run activate out of:
Scripts\activate
Once activated, you can install any other 3rd party Python package.
Note: It is recommended that you install all 3rd party packages, such as
ReportLab or Pillow, in a Python Virtual Environment or a user folder. This
prevents you from installing a lot of cruft in your system Python installation.
I would also like to mention that pip supports a –user flag that tells it to install
the package just for the current user if the platform supports it. There is also an –
update flag (or just -U) that you an use to update a package. You can use this
flag as follows:
python -m pip install PACKAGE_NAME --upgrade
While you can also use pip install PACKAGE_NAME, it is now becoming a
recommended practice to use the python -m approach. What this does differently
is that it uses whatever Python is on your path and installs to that Python version.
The -m flag tells Python to load or run a module which in this case is pip. This
can be important when you have multiple versions of Python installed and you
don’t know which version of Python pip itself will install to. Thus, by using the
python -m pip approach, you know that it will install to the Python that is
mapped to your “python” command.
Dependencies
You will need the Python language installed on your maching to use ReportLab.
Python is pre-installed on Mac OS and most Linux distribututions. Reportlab 3
works with both Python 2.7 and Python 3.3+. You can get Python at
https://www.python.org/. They have detailed instructions for installing and
configuring Python as well as building Python should you need to do so.
ReportLab depends on the Python Imaging Library for adding images to PDFs.
The Python Imaging Library itself hasn’t been maintained in years, but you can
use the Pillow (https://pillow.readthedocs.io/en/latest/) package instead. Pillow
is a fork of the Python Imaging Library that supports Python 2 and Python 3 and
has lots of new enhancements that the original package didn’t have. You can
install it with pip as well:
python -m pip install pillow
You may need to run pip as root or Administer depending on where your Python
is installed or if you are installing to a virtualenv. You may find that you enjoy
Pillow so much that you want to install it in your system Python in addition to
your virtual environment.
Installation
Reportlab 3 works with both Python 2.7 and Python 3.3+. This book will be
focusing on using Python 3 and ReportLab 3.x, but you can install ReportLab 3
the same way in both versions of Python using pip:
python -m pip install reportlab
If you are using an older version of Python such as Python 2.6 or less, then you
will need to use ReportLab 2.x. These older versions of ReportLab have *.exe
installers for Windows or a tarball for other operating systems. If you happen to
run a ReportLab exe installer, it will install to Python’s system environment and
not your virtual environment.
If you run into issues installing ReportLab, please go to their website and read
the documentation on the subject at https://www.reportlab.com/
As I said, there are a lot of other settings that you can modify in that Python
script. I highly recommend opening it up and reading through the various
options to see if there’s anything that you will need to modify for your
environment. In fact, you can do so in your Python interpreter by doing the
following:
>>> from reportlab import rl_settings
>>> rl_settings.verbose
0
>>> rl_settings.shapeChecking
1
You can now easily check out each of the settings in an interactive manner.
Reader Feedback
I welcome your feedback. If you’d like to let me know what you thought of this
book, you can send comments to the following email address:
comments@pythonlibrary.org
Errata
I try my best not to publish errors in my writings, but it happens from time to
time. If you happen to see an error in this book, feel free to let me know by
emailing me at the following:
errata@pythonlibrary.org
Code Examples
Code from the book can be downloaded from Github at the following address:
https://github.com/driscollis/reportlabbookcode
http://bit.ly/2nc7sbP
In this section of the book, you will learn how to create PDFs using ReportLab’s
low level canvas API. You will then move on to learn how to create PDFs using
ReportLab’s Flowable via their PLATYPUS sub-module. Then we will wrap up
Part I by learning about ReportLab’s graphics capabilities and the special
features you can apply to a PDF such as page transitions and encryption.
pretty much any layout that you can think of. I have used it to
replicate many complex page layouts over the years. In this chapter we
Draw text
Learn about fonts and text colors
Creating a text object
Draw lines
Draw various shapes
The pdfgen package is very low level. You will be drawing or “painting”
on a canvas to create your PDF. The canvas gets imported from the pdfgen
package. When you go to paint on your canvas, you will need to specify
X/Y coordinates that tell ReportLab where to start painting. The default
is (0,0) whose origin is at the lowest left corner of the page. Many
desktop user interface kits, such as wxPython, Tkinter, etc, also have
this concept. You can place buttons absolutely in many of these kits
using X/Y coordinates as well. This allows for very precise placement of
the elements that you are adding to the page.
The other item that I need to make mention of is that when you are
points you are from the origin. It’s points, not pixels or
millimeters or inches. Points! Let’s take a look at how many points are
Here we learn that a letter is 612 points wide and 792 points high.
Let’s find out how many points are in an inch and a millimeter,
respectively:
# hello_reportlab.py
c = canvas.Canvas("hello.pdf")
c.drawString(100, 100, "Welcome to Reportlab!")
c.showPage()
c.save()
Canvas object. You will note that the only requirement argument is a
tell it to start drawing the string 100 points to the right of the
origin and 100 points up. After that we call showPage() method. The
showPage() method will save the current page of the canvas. It’s
also ends the current page. If you draw another string or some other
page. Finally we call the canvas object’s save() method, which save
the document to disk. Now we can open it up and see what our PDF looks
like:
Fig. 1-1: Welcome to ReportLab
What you might notice is that our text is near the bottom of the
document. The reason for this is that the origin, (0,0), is the bottom
text, we were telling it to start painting 100 points from the left-hand
side and 100 points from the bottom. This is in contrast to creating a
whatever is in the ReportLab config, which is usually A4. There are some
arguments:
def __init__(self,filename,
pagesize=None,
bottomup = 1,
pageCompression=None,
invariant = None,
verbosity=0,
encrypt=None,
cropMarks=None,
pdfVersion=None,
enforceColorSpace=None,
):
Here we can see that we can pass in the pagesize as an argument. The
want to change the origin from the default of bottom left, then you can
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
primary use case for turning on page compression is when you have a huge
info (for regression testing). I have never seen anyone use this
argument in their code and since the source says it is for regression
zero (0), ReportLab will allow other applications to capture the PDF
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.
encrypt, that string will be the password for the PDF. If you want
encrypt argument.
Crop marks are used by printing houses to know where to crop a page.
When you set cropMarks to True in ReportLab, the page will become 3 mm
larger than what you set the page size to and add some crop marks to the
corners. The object that you can pass to cropMarks contains the
The pdfVersion argument is used for ensuring that the PDF version is
appropriate color settings within the PDF. You can set it to one of the
following:
cmyk
rgb
sep
sep_black
sep_cmyk
be used to do the color enforcement. You can also pass in a callable for
color enforcement.
Let’s go back to our original example and update it just a bit. Now as I
images, etc) using points. But thinking in points is kind of hard when
(http://stackoverflow.com/questions/4726011/wrap-text-in-a-table-reportlab):
return x, y
the page. You can also pass in a unit size. This will allow you to do
the following:
# canvas_coords.py
c = canvas.Canvas("hello.pdf", pagesize=letter)
width, height = letter
from the left and 20 mm from the top of the page. Yes, you read that
right. When we use the coord function, it uses the height to swap
the origin’s y from the bottom to the top. If you had set your Canvas’s
following:
return x, y
# canvas_coords2.py
c = canvas.Canvas("hello.pdf", bottomup=0)
That seems pretty straight-forward. You should take a minute or two and
play around with both examples. Try changing the x and y coordinates
that you pass in. Then try changing the text too and see what happens!
Canvas Methods
The canvas object has many methods. Let’s learn how we can use some
of them to make our PDF documents more interesting. One of the easiest
methods to use setFont, which will let you use a PostScript font
name to specify what font you want to use. Here is a simple example:
# font_demo.py
if __name__ == '__main__':
my_canvas = canvas.Canvas("fonts.pdf",
pagesize=letter)
fonts = my_canvas.getAvailableFonts()
font_demo(my_canvas, fonts)
my_canvas.save()
we can use on the system that the code is ran on. Then we will pass the
canvas object and the list of font names to our font_demo function.
Here we loop over the font names, set the font and call the
drawString method to draw each font’s name to the page. You will
also note that we have set a variable for the starting Y position that
If you want to change the font color using a canvas method, then you
long as you call that before you draw the string, the color of the text
Another fun thing you can is use the canvas’s rotate method to draw
A pair of ciliated excretory tubes open into the vestibule. These are
similar in structure to the "head-kidneys" of the larvae of Polychaet
worms, or to the excretory organs of adult Rotifers. Flame-cells have
been described by Davenport in the stalk of Urnatella, but it is not
known whether they are connected with the excretory tubes of the
calyx. The animals are either hermaphrodite or have separate sexes,
and the generative organs open by ducts of their own into the
vestibule. The nervous system consists of a ganglion placed
between the mouth and the anus, giving off a set of nerves, many of
which end in delicate tactile hairs placed on the tentacles or other
parts of the body.[544]
CHAPTER XVIII
POLYZOA (continued)
FRESH-WATER POLYZOA—PHYLACTOLAEMATA—OCCURRENCE—
STRUCTURE OF CRISTATELLA—DIVISION OF COLONY—MOVEMENTS OF
COLONY—RETRACTION AND PROTRUSION OF POLYPIDES IN POLYZOA—
STATOBLASTS—TABLE FOR DETERMINATION OF GENERA OF FRESH-
WATER POLYZOA—REPRODUCTIVE PROCESSES OF POLYZOA—
DEVELOPMENT—AFFINITIES—METAMORPHOSIS—BUDDING.
The tentacles are about eighty to ninety in number, and they are, as
in other Phylactolaemata, united at their bases by a delicate web.
The lophophore is horse-shoe-shaped (Fig. 236, 3) throughout the
group, with the exception of Fredericella, in which genus it is circular.
Fig. 248 shows a colony shortly after division has taken place. The
colony had moved forwards, in a direction away from its apex, for
three days in a nearly straight line, the distances moved in each day
being respectively 6, 8½, 8½ mm. These observations, for which I
am indebted to Mr. Lister, show a considerably higher speed than in
those recorded by Trembley, who observed no colony which moved
more than half an inch (12.5 mm.) in eight days.
In all these cases the egg develops inside the parent, and it was
hardly known, before the publication of the interesting researches of
M. Prouho,[572] that some of the Polyzoa lay eggs which develop
externally. In these cases a considerable number of eggs are
produced simultaneously by a single zooecium. M. Prouho further