Ask a Question

Automate the Download of PDF files from Chrome

cauline
Contributor

Automate the Download of PDF files from Chrome

Does anyone know of a reliable way to interact with Adobe Reader embedded within Chrome to download PDF files?  It appears to be using a 'Flex' control to download.  I've tried to set the default download path, but this doesn't always automatically download the generated PDF.  There are still dialogs that have to be interacted with.

 

Thanks!

5 REPLIES 5
Marsha_R
Community Hero

Let's see the code that you tried


Marsha_R
[Community Hero]
____
[Community Heroes] are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Posts made by [Community Heroes]
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
The [Community Hero] signature is used with permission by SmartBear Software.
https://community.smartbear.com/t5/custom/page/page-id/hall-of-fame

And here under this privacy and security you need to scroll down. And here we have the content settings click on that. And you need to find here the PDF. Ok. So just find the PDF. Section.

Michael547
New Member

Scroll to the privacy & security settings and click Site Settings. On the site settings page, click pdf documents. On the page that follows, turn on the download pdf files instead of automatically opening them in chrome option.

 

mcdvoice

It looks like there are three scenarios that we are dealing with.  First, I needed to switch-off the Adobe Acrobat Chrome Extension:

 

1. Direct Path to PDF generated on web server

Solution:  Use Java IO Buffered Intput Stream.  

 

//var url = JavaClasses.java_net.URL.newInstance("http://pathToServer/ServerName?&reportname=Custom_Bol_##########&type=pdf&fileextn=.pdf,Custom_Bol_W...");
var url = JavaClasses.java_net.URL.newInstance("http://pathToPDF/PDFile.pdf");
var inputStream = url.openStream();

 

var fileParse = JavaClasses.java_io.BufferedInputStream.newInstance(inputStream);
var docObj = JavaClasses.org_apache_pdfbox_pdmodel.PDDocument.load_3(fileParse);

var textStripperObj = JavaClasses.org_apache_pdfbox_text.PDFTextStripper.newInstance()
outputString = textStripperObj.getText(docObj);
docObj.close();

Log.Message("Output PDF String = " +outputString)

 

2. PDF loaded using a long URL and displayed in non-preview mode

Solution:  In this scenerio, I can type 'CTRL S', and save via the Open File Dialog to the folder I need.  This I'm still working on find a reliable method to wait for the PDF file to load in Chrome (Other than a static wait).  After downloaded, I can use the following code:

 

var firstPDF = ProjectSuite.Path + "\pathToPDF\\BOL_Test.pdf";

var outputString = "";
var orderNumber = "60022500"

var browser = Aliases.browser;
browser.pageReportserv.Keys("^s");

 

var dlgSaveAs = browser.dlgSaveAs;
var comboBox = dlgSaveAs.DUIViewWndClassName.Explorer_Pane.FloatNotifySink.ComboBox;

comboBox.SetText(firstPDF);
dlgSaveAs.btnSave.ClickButton();

 

var doc = JavaClasses.java_io.File.newInstance(firstPDF)
var docObj = JavaClasses.org_apache_pdfbox_pdmodel.PDDocument.load_2(doc);

 

var textStripperObj = JavaClasses.org_apache_pdfbox_text.PDFTextStripper.newInstance()
outputString = textStripperObj.getText(docObj);
docObj.close();

Log.Message("Output PDF String = " +outputString)

// Run a basic test to find a string of text
if (aqString.Find(outputString, orderNumber) != -1)
Log.Message("Found Order #: " +orderNumber)
else
Log.Error("Didn't find Order # in search string.");

 

3.  PDF Generated in Preview Mode (Read Only)

Solution:  I'm still working on a way to either download using CTRL 'S' or Buffered Input Stream.  The URL is also lond a has many special characters (for spaces, etc.).

I also want to be able to control where the downloaded file gets put, so that I can locate and run a comparison or test text content.  I don't want all files downloading to the same Chrome Download folder.  I want to set this programatically, since we run these via Jenkin's pipeline jobs.

cancel
Showing results for 
Search instead for 
Did you mean: