rachard11 has asked for the wisdom of the Perl Monks concerning the following question:

The Perl code I am using (below) downloads a pdf file that I cannot read in Adobe Acrobat (Adobe message: "There was an error opening this document. The file is damaged and could not be repaired." Is there an alternative Perl module or method that will retain the file's readability? The file opens fine if I just navigate to the site in Chrome, download the file manually, and open in Acrobat.

#!/usr/bin/perl use strict; use warnings; use LWP; my $ua = LWP::UserAgent->new; my $output_file = "F:/SCAC Complaints/file_output.pdf" ; my $get_file = "http://securities.stanford.edu/filings-documents/1061/ +DI00_04/201753_f01c_17CV01097.pdf" ; open (my $fh1, '>', $output_file ) ; my $response =$ua->get($get_file); if ($response->is_success) { print $fh1 $response->content; close $fh1 ; } else { print "Error in $get_file \n" ; }

Replies are listed 'Best First'.
Re: Perl script to download readable .pdf file.
by choroba (Cardinal) on May 05, 2017 at 15:37 UTC
    I don't have Adobe Acrobat, but I can open the downloaded file in okular without problems.

    Have you tried setting binmode on $fh1 before printing to it?

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
      I was not familiar with binmode, but I just tried it and it worked! Thanks!
Re: Perl script to download readable .pdf file.
by Anonymous Monk on May 05, 2017 at 15:41 UTC
    Maybe try open mode '>:raw'
Re: Perl script to download readable .pdf file.
by BillKSmith (Monsignor) on May 06, 2017 at 11:17 UTC
    I understand that you have already solved your problem. However, you should be aware that I had another problem with a nearly identical program. The site would not download copyrighted material to a 'browser' they did not recognize. The downloaded file actually was an html file containing the explanation (perl still thought it was pdf.)
    Bill
      And the solution to that is to set $ua->agent to something the site recognizes, eg
      $ua->agent('Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:53.0) Gec +ko/20100101 Firefox/53.0');
        Thanks! To bad that I did not ask while the site was still in service. I had no idea that there was a solution so I gave up and downloaded the files by hand with my browser.
        Bill