Search code examples
phpperlpdfwww-mechanize

Save PDF file to server


Save a dynamically generated PDF file from a URL to disk. The PDF file is generated using ASPX on a url, sample url -

I have tried the basic file_get_contents but it saves an empty file.

http://url-address/LabReport_Interface.aspx?TestID=LSHHI764&PID=LSHHI637515&LedgerTransactionNo=401411000222

$handle = fopen("./reports/test.pdf", "wb"); 
if (fwrite($handle, file_get_contents($url)) === FALSE) { 
        echo "Cannot write to file ($filename)"; 
} 
fclose($handle);

Thanks in advance. Any other turnaround would also be appreciated.


Solution

  • I have found the solution, I used a perl call from php and saved pdf using perl's www-mechanize

    #!/usr/bin/perl
    
    use WWW::Mechanize;
    
    my $url = $ARGV[0];
    my $filename = $ARGV[1];
    
    my $mech = WWW::Mechanize->new();
    
    $mech->get($url);
    $mech->success();
    $mech->save_content($filename);