Print to PDF Question

For starters: I read a lot of other posts regarding this issue, but wanted to ask if there is any updated solutions.
I already read this:How to click on download of pdf and save inside browser windw in robot framrwork

So I have a print form to a pdf button, and want to save that pdf for further comparisons in the same testcase.

Is there a way to use Browser keywords, such as “Promise to wait for download” to download pdf to current directory?
I have tried many ways without succeeding, the problem being that “save file as” is not part of the browser context. I can get to the stage where pressing print would get me to “save file as” part when manually doing the steps.

Also as the pdf is generated at that instance, there is no direct url to file

-Thanks

Hello,

Been there too, where I needed to check pdf content, and also get a screenshot of the content.
From my understanding and search in ways to interact with the PDF viewer, main issue is that buttons (i.e. the print/download one) are in shadow-dom and not easily interactable.

On my side I finally choose to do this :

  1. Start browser (Chrome here) with options to directly download the file
  2. Check content with PyPDF2 and a .py file
  3. Eventually , as the PDF is directly downloaded and not displayed , open it to perform the required screenshot. Note here that it’s OK while your content his in first page

Challenging thing then is if you need to scroll down the pdf for visual checks.

Regards.

2 Likes

How do you directly download the pdf?
That has been my issue, cant seem to be able to get that file for comparison.
In my case there is no one file to download, so I could use that Download -keyword properly.

And yes, I use chrome also
-Joel

Hi,

It’s not a keyword it’s chrome startup option. Found several issues on forums and all, but finally this worked for me :

${browser_prefs}    Create Dictionary     download.default_directory=_yourfolder      plugins.always_open_pdf_externally=${True}
${chrome_options}=    Evaluate    sys.modules['selenium.webdriver'].ChromeOptions()    sys, selenium.webdriver
Call Method    ${chrome_options}    add_argument    --log-level\=3
Call Method    ${chrome_options}    add_experimental_option    prefs    ${browser_prefs}

Then Open Browser with options :

  Open Browser    url   chrome    options=${chrome_options}
  • Note : The -log-level is not necessary, it’s just an example if you have to add other arguments/options (gpu, sandbox…) to your ${chrome_options}.
  • Note : for yourfolder, depending of your OS you could use ${/} for path (have it working on Windows and Linux for example)

This will the PDF downloaded where it should normally open in a new tab.

Best Regards.

Charlie

2 Likes

Have you tried this when running your scripts in headless mode? TYIA!

Hi Charlie,

I would like to ask for your assistance with opening a PDF file in a new tab and then taking a screenshot of the PDF using Robot Framework.

I tried to open the PDF using a URL format like this: file://.../.../file.pdf, but it cannot open properly. Could you please help me resolve this issue?

Thank you for your time and assistance.

Best regards,
Ngodings

Hi,

If you simply need to open as you required you can do something like this.

It considering you already have a browser opened, and just need to open, screen then return to the first browser instance:

VAR    ${chrome_options}    *your_options*
Open Browser    about:blank    chrome    options=${chrome_options}
@{browserID}    Get Browser Ids
Go To    file://${pdfpath}
Sleep    1s       # Wait that the pdf/page is visible, to adapt
Capture Page Screenshot    mypage.png
Close Browser
Switch Browser    ${browserID}[0]

Note however that with Chrome and Selenium, this will open the file with Chrome Viewer which uses shadow-DOM, so you won’t be able to interact easily (or at all). You would need to use js or even Browser library).

If you need more than that, such as comparison, I would highly suggest you use a library like this:

You can easily download file as you did, get the latest pdf with a small RF script based on file modified nfo or naming, then pass it for to *Compare Images* keywords.

You can also use masks if you a have non fixed values such as date or our pattern.

Regards.
Charlie