Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save a pdf file that's been opened in Internet Explorer with OLE and Perl

I am looking for a way to use Perl to open a PDF file in Internet Explorer and then save it.

(I want the user to be able to interact with the script and decide whether downloading occurs, which is why I want to pdf to be displayed in IE, so I cannot use something like LWP::Simple.)

As an example, this code loads (displays) a pdf, but I can't figure out how to get Perl to tell IE to save the file.

use Win32::OLE;
my $ie = Win32::OLE->new("InternetExplorer.Application");
$ie->{Visible} = 1;
Win32::OLE->WithEvents($ie);

$ie->Navigate('http://www.aeaweb.org/Annual_Meeting/pdfs/2014_Registration.pdf');

I think I might need to use the OLE method execWB, but I haven't been able to figure it out.

like image 787
itzy Avatar asked Sep 19 '13 13:09

itzy


People also ask

How do I save a PDF from Internet Explorer?

Right-click on the selected content and choose one of the following: To create a new PDF, choose Convert To Adobe PDF or Convert Web Page To Adobe PDF (Internet Explorer) or Convert Selection to Adobe PDF (Firefox). Then select a name and location for the PDF.

Can not print PDF from Internet Explorer?

In Internet Explorer, go to the "Tools" menu > "Internet Options. Select the "Security" tab. Select the "Internet" zone (if it isn't selected already) Deselect the option called "Enable Protected Mode"


1 Answers

What you want to do is automate the Internet Explorer UI. There are many libraries out there that will do this. You tell the library to find your window of interest, and then you can send keystrokes or commands to the window (CTRL-S in your case).

A good overview on how to do this in Perl is located here.

Example syntax:

my @keys = ( "%{F}", "{RIGHT}", "E", );
for my $key (@keys) {
    SendKeys( $key, $pause_between_keypress );
}

The code starts with an array containing the keypresses. Note the format of the first three elements. The keypresses are: Alt+F, right arrow, and E. With the application open, this navigates the menu in order to open the editor.

Another option is to use LWP:

use LWP::Simple;

my $url = 'http://www.aeaweb.org/Annual_Meeting/pdfs/2014_Registration.pdf';
my $file = '2014_Registration.pdf';

getstore($url, $file);
like image 198
GalacticJello Avatar answered Sep 29 '22 00:09

GalacticJello