Note: You are looking at a static snapshot of documentation related to Robot Framework automations. The most recent documentation is at https://robocorp.com/docs

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:

Html table and pdf file side by side

As always, let's start by adding a new step in our *** Tasks *** section:

*** Tasks *** Insert the sales data for the week and export it as a PDF Open the intranet website Log in Download the Excel file Fill the form using the data from the Excel file Collect the results Export the table as a PDF

Then we add a new keyword:

*** Keywords *** Export the table as a PDF

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

We want to make sure that the table element exists on the page when trying to "grab" it. We'll use the Wait Until Element Is Visible keyword. We just need a locator.

The HTML markup of the table area on the page looks like this:

... <div id="sales-results"> <table class="table table-dark table-striped"> ... </table> </div> ...

The table is wrapped in a <div> element with an id attribute of sales-results. Our locator will then be id:sales-results.

Look at that beautiful code. It's almost like that page was created for this course! 😍

We will modify our keyword like this:

*** Keywords *** Export the table as a PDF Wait Until Element Is Visible id:sales-results

Next, we want to put the HTML markup of that element into a variable. We can do this with the Get Element Attribute keyword (RPA.Browser.Selenium library) like this:

*** Keywords *** Export the table as a PDF Wait Until Element Is Visible id:sales-results ${sales_results_html}= Get Element Attribute id:sales-results outerHTML

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 Get Element Attribute keyword. We pass two arguments to that keyword: the first is the locator for the element (id:sales-results); the second is the name of the attribute of the element we want to get.

In our case, we want all the HTML markup of that element, including everything inside it, so we choose the outerHTML attribute.

You can read more about the Element API if you are interested. It gets pretty technical, though. Please don't say we did not warn you! 🙂

Alright! Let's rerun our robot.

The log shows the robot has now grabbed the HTML markup for the table:

Log containing the markup for the HTML table

Creating the PDF file out of the HTML contents variable

Only one more step to go!

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 keywords... Wax on, wax off... 🥋 Practice will make us perfect! 💪

*** Settings *** Documentation Insert the sales data for the week and export it as a PDF. Library RPA.Browser.Selenium auto_close=${FALSE} Library RPA.Excel.Files Library RPA.HTTP Library RPA.PDF

Now we can add the final line to our keyword.

*** Keywords *** Export the table as a PDF Wait Until Element Is Visible id:sales-results ${sales_results_html}= Get Element Attribute id:sales-results outerHTML Html To Pdf ${sales_results_html} ${OUTPUT_DIR}${/}sales_results.pdf

We use the Html To Pdf keyword 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_DIR}${/}).

And that's it!

Here's what our robot code looks like now:

*** Settings *** Documentation Insert the sales data for the week and export it as a PDF. Library RPA.Browser.Selenium auto_close=${FALSE} Library RPA.Excel.Files Library RPA.HTTP Library RPA.PDF *** Tasks *** Insert the sales data for the week and export it as a PDF Open the intranet website Log in Download the Excel file Fill the form using the data from the Excel file Collect the results Export the table as a PDF *** Keywords *** Open the intranet website Open Available Browser https://robotsparebinindustries.com/ Log in Input Text username maria Input Password password thoushallnotpass Submit Form Wait Until Page Contains Element id:sales-form Download the Excel file Download https://robotsparebinindustries.com/SalesData.xlsx overwrite=True Fill and submit the form for one person [Arguments] ${sales_rep} Input Text firstname ${sales_rep}[First Name] Input Text lastname ${sales_rep}[Last Name] Input Text salesresult ${sales_rep}[Sales] Select From List By Value salestarget ${sales_rep}[Sales Target] Click Button Submit Fill the form using the data from the Excel file Open Workbook SalesData.xlsx ${sales_reps}= Read Worksheet As Table header=True Close Workbook FOR ${sales_rep} IN @{sales_reps} Fill and submit the form for one person ${sales_rep} END Collect the results Screenshot css:div.sales-summary ${OUTPUT_DIR}${/}sales_summary.png Export the table as a PDF Wait Until Element Is Visible id:sales-results ${sales_results_html}= Get Element Attribute id:sales-results outerHTML Html To Pdf ${sales_results_html} ${OUTPUT_DIR}${/}sales_results.pdf

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