Creating a PDF
Maria creates a PDF from the sales data table in the intranet and sends it out as a company newsletter to ensure that her colleagues look at it.
After all, it would be a shame if no one saw the result of all that copy-pasting!
Maria copies the table into Microsoft Word and exports it to PDF with some additional software. Our robot, instead, will do it all by itself automatically.
We want to turn the table on the left into the PDF on the right:
As always, let's start by adding a new step in our @task
function, before the log_out()
step:
Then we add a new function:
As always, we plan to do this in steps. (Remember the poor elephant we are eating? 🐘)
Our plan for this keyword is:
- we isolate the part of the page that contains the sales table
- we assign the content (HTML markup) of that part of the page to a variable
- we create a PDF with the HTML content of the table.
Getting the HTML table element out of the page
The HTML markup of the table area on the page looks like this:
Look at that beautiful code. It's almost like that page was created for this course! 😍
The table is wrapped in a <div>
element with an id
attribute of sales-results
. Our selector will then be #sales-results
.
Next, we want to put the HTML markup of that element into a variable, sales_results_html
. We can do this with the inner_html()
function like this:
We will modify our keyword like this:
Ok, we admit this was not too easy to guess. 😅 But no panic! Let's see what's going on on this new line.
We create a variable (sales_results_html
).
We store into it what we get out of the inner_html()
function.
This will return all the HTML markup of that element, innerHTML
.
Alright! Let's rerun our robot.
The log shows the robot has now grabbed the HTML markup for the table:
Creating the PDF file out of the HTML contents variable
Only one more step to go!
Fantastic libraries and where to find them :zap:️
We have ambitious plans for our robot, and along the way, we will add more libraries to make it smarter and allow it to do more things.
But how do we know which libraries are available and how they work?
The best place to start is the Libraries page, where we have compiled a list of useful libraries for Robotic Process Automation.
Take the RPA.PDF
library that look exactly like what we need, for example.
It has its own documentation page where you can see examples on how to use it, and the list of all the keywords and functions it provides.
This library is adapted both for python and Robot Framework.
Now that we have the HTML contents of the table in a variable, we need to create a PDF file out of it.
To do it, we will add the RPA.PDF
library!
Add a new library, get new functions... Wax on, wax off... 🥋 Practice will make us perfect! 💪
Now we can add the final lines to our keyword.
We use the html_to_pdf()
function provided by the RPA.PDF
library
to create a sales_results.pdf
file out of our sales_results_html
variable's contents, and place it again into the output
folder (output/sales_results.pdf
).
And that's it!
Here's what our robot code looks like now:
Let's run the robot one final time.
A new sales_results.pdf
file appears in the output
directory, containing the sales data! 🎉🎉🎉
If you click on the PDF document, VS Code might suggest finding a suitable extension for viewing PDF documents. You can accept and install an extension!
What we learned
- You can create a PDF file from HTML content using the
RPA.PDF
library and thehtml_to_pdf()
function.