Grabbing a PDF file from the web...how?

F

funkyville

All I need my script to do is go to a website and download a .pdf from
that site and save it to my hard drive. Example,
http://www.anysite.com/report.pdf

How do I do it?
I've searched for hours (groups, CPAN, etc.) and can't find anything.

For a normal html page I would just do something like this:

use LWP::Simple;
my $url;
my $content;
$url = "http://www.yahoo.com";
$content = get($url);

#then save
open(FILE, ">>C:/temp/new.html") || die "$!";
print FILE $content;
close(FILE);


This approach won't work with PDFs.
Does anyone know what to do?
Do I need special modules? What am I missing?
Thanks.
 
A

A. Sinan Unur

(e-mail address removed) (funkyville) wrote in @posting.google.com:
All I need my script to do is go to a website and download a .pdf from
that site and save it to my hard drive. Example,
http://www.anysite.com/report.pdf

How do I do it?

You might want to read the docs for the actual module you are trying to
use: perldoc LWP::Simple

perl -MLWP::Simple -e"getstore(q{http://www.anysite.com/report.pdf}, q
{report.pdf})"

Enter the above as one line in your shell.
 
B

Ben Morrow

Quoth (e-mail address removed) (funkyville):
All I need my script to do is go to a website and download a .pdf from
that site and save it to my hard drive. Example,
http://www.anysite.com/report.pdf

How do I do it?
I've searched for hours (groups, CPAN, etc.) and can't find anything.

For a normal html page I would just do something like this:

use LWP::Simple;
my $url;
my $content;
$url = "http://www.yahoo.com";
$content = get($url);

#then save
open(FILE, ">>C:/temp/new.html") || die "$!";

You want '>', not '>>'.
Use three-arg open.
Use lexical FHs.
Use low-prec or.

open my $PDF, '>', 'C:/temp/new.pdf' or die "can't create new.pdf: $!";

PDFs are binary, so you need

binmode $PDF;

as well.
print FILE $content;
close(FILE);

No need for this with lexical FHs.

Ben
 
J

Joe Smith

funkyville said:
This approach won't work with PDFs.
Does anyone know what to do?
Do I need special modules? What am I missing?

You've got two problems. Other people have pointed out that get() is
not the appropriate method for getting+storing files. The other problem
is that you should be using binmode() when dealing with binary files.

binmode FILE; print FILE $content;

-Joe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,163
Messages
2,570,897
Members
47,434
Latest member
TobiasLoan

Latest Threads

Top