我正在寻找一种方法来使用Perl在Internet Explorer中打开PDF文件,然后保存它.
(我希望用户能够与脚本交互并决定是否发生下载,这就是为什么我想在IE中显示pdf,所以我不能使用像LWP :: Simple这样的东西.)
作为一个例子,这段代码加载(显示)一个pdf,但我无法弄清楚如何让Perl告诉IE保存文件.
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');
我想我可能需要使用OLE方法execWB,但我无法弄明白.
解决方法
您要做的是自动化Internet Explorer UI.有很多图书馆可以做到这一点.您告诉库找到您感兴趣的窗口,然后您可以向窗口发送击键或命令(在您的情况下为CTRL-S).
如何在Perl中执行此操作的概述位于here.
示例语法:
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.
另一种选择是使用LWP:
use LWP::Simple; my $url = 'http://www.aeaweb.org/Annual_Meeting/pdfs/2014_Registration.pdf'; my $file = '2014_Registration.pdf'; getstore($url,$file);