سؤال

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.

هل كانت مفيدة؟

المحلول

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);

نصائح أخرى

ForExecWB here is good thread, however it is not solved: http://www.perlmonks.org/?node_id=477361

$IE->ExecWB($OLECMDID_SAVEAS, $OLECMDEXECOPT_DONTPROMPTUSER, 
$Target);

Why don't you display the PDF in IE then close the IE and save the file using LWP?

You could use Selenium and the perl remote drivers to manage IE

http://search.cpan.org/~aivaturi/Selenium-Remote-Driver-0.15/lib/Selenium/Remote/Driver.pm http://docs.seleniumhq.org/projects/webdriver/

You will also need to download the IE selenium driver - it comes with firefox as standard https://code.google.com/p/selenium/wiki/InternetExplorerDriver

use Selenium::Remote::Driver;

my $driver = new Selenium::Remote::Driver;
$driver->get('http://www.google.com');
print $driver->get_title();
$driver->quit();
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top