Forum Discussion

cauline's avatar
cauline
Contributor
3 years ago

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

    • Cooper5's avatar
      Cooper5
      New Member

      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.

    • cauline's avatar
      cauline
      Contributor

      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_WM011111111111");
      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.).

      • cauline's avatar
        cauline
        Contributor

        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.

  • 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