PHP: Retrieve The PDF Document Of The Letter Or Postcard With GetPDF()

<?php
 
/************* Settings  Begin ****************/
 
$username = '********';
$password = '********';
$id = 99999999; // ID of transaction being queried
$output_filename = 'PostalMethods_' . $id . '.pdf'; // Comment out to view on screen
 
/************* Settings  End ******************/
 
$soapclient = new SoapClient('https://api.postalmethods.com/2009-02-26/PostalWS.asmx?WSDL');
$result = $soapclient->GetPDF(array('Username'    => $username,
                                    'Password'    => $password,
                                    'ID'   => $id));
 
//print_r($result);
//exit;
 
// Extract returned fields
$result_code = $result->GetPDFResult->ResultCode;
$file_data   = $result->GetPDFResult->FileData;
 
if ( $result_code == -3000) { 
    // an letter has been successfully retrieved
    if ($output_filename != ''){ 
        // output to file
		print "PDF successfully retrieved<br>";
        if (!$handle = fopen($output_filename, 'w')) {
            echo "Cannot open file ($output_filename)";
            exit;
            }
        if (fwrite($handle, $file_data) === FALSE) {
            echo "Cannot write to file $output_filename";
            exit;
            }
		print "PDF successfully written<br>";
        fclose($handle);
        } else { 
            // output to screen
            header ("Content-type: application/pdf"); 
            echo $file_data;
            }
 
} else {
    // no letter retrieved
    echo "Error: <a href=\"http://www.postalmethods.com/resources/reference/status-codes\">$result_code</a><br>";
}
 
?>