rahardi
rahardi

Reputation: 213

Perl save file from website to desktop

I have this script below, where I want to download a pdf from a URL

#!/usr/bin/perl
use warnings;
use strict;
use LWP::Simple;

my $save = "C:\\Users\\rahard\\Desktop\\";  
my $file = get 'http://locationoffile';

How can I save the pdf to the Desktop? (if I click the URL, it will pront me to save a random name file)

Thank you

/edit some syntax error in $save and edit $file location

Upvotes: 2

Views: 4534

Answers (2)

User_612087
User_612087

Reputation: 31

Could it be that the url is not the origin location of the pdf and you get redirected to them? If the response of get is not a valid pdf, it could be the html-content of the redirecting page.

I would test this first.

Therefor I would use WWW::Mechanize or a similar library.

Upvotes: 0

Tim
Tim

Reputation: 14164

Use open to open a file handle and print to it. Also note that each backslash in $save must be escaped.

my $save = "C:\\Users\\rahard\\Desktop\\";  
my $file = get 'http://file.pdf';

open( FILE, '>', $save . 'filename.pdf' ) or die $!;
binmode FILE;
print FILE $file;
close( FILE );

Upvotes: 2

Related Questions