2

I have not been able to find a solution using PHP. Basically, when a user clicks on the "download PDF" link, It will call a PHP function that takes a byte array and parses it as a PDF. I dont know how to go about it. Any help would be great!

EDIT: I'm getting the "byte array" like this:

<?php
$userPDF = $_POST['username'];
$passPDF = $_POST['password'];
$idPDF = 'MO-N007175A';
//PDF Function
$data = array("id" => $idPDF, "username" => $userPDF, "password" => $passPDF);                                                                    
$data_string = json_encode($data);                                                
$ch = curl_init('http://*****.com/****/v1/DealPdf');                                                                      
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                 
curl_setopt($ch, CURLOPT_VERBOSE, 1 );                                                                  
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: application/json')                                                                       
);                                                                                                                   
$result = curl_exec($ch);
var_dump($result);
?>

The "$result" is supposedly the byte array that looks like this:

string(1053240) "[37,80,68,70,45,49,46,54,10,37,211,244,204,225,10,49,32,48,32,111,98

..etc. Basically I need to take what im getting back and save it as a PDF (or any generic file for the matter).

13
  • 2
    And this byte array comes from... where? There's no such thing as a 'byte array' in PHP. Commented Mar 8, 2012 at 17:21
  • It is coming from a WCF service, and I'm retrieving it from POST. Commented Mar 8, 2012 at 17:31
  • So...how do you want to write the object to file? As a JSON string, I presume? Commented Mar 8, 2012 at 17:35
  • @Jack Maney - I added clarification. I dont need to write that JSON object to a file.. That is there just for giving the full idea of how Im doing things. I need to take what I get back from a WCF service, (supposedly a byte array) and save it as a PDF. Commented Mar 8, 2012 at 17:37
  • 1
    For creating PDF's i would recommend TCPDF sourceforge.net/projects/tcpdf/files it is stable enough to be usable on production, I have used it myself many times, also you should first have a clear view of what the services is returning, can you dump the results of your curl? see what you are getting back, from there try to find the best way to parse it then you can worry about the PDF part, which is in my opinion the less trivial part. Commented Mar 8, 2012 at 17:43

2 Answers 2

3

GiDo's answer will work, but will take very long for large files. I would recommend doing the following:

$bytes = json_decode($results);
$bytesStr = pack('C*', ...$bytes);
file_put_contents('myfile.pdf', $bytesStr);
Sign up to request clarification or add additional context in comments.

Comments

2

You can try something like this:

// suppose that the response is a JSON array (look like it)
$bytes = json_decode($results);

$fp = fopen('myfile.pdf', 'wb+');

while(!empty($bytes)) {
    $byte1 = array_shift($bytes);
    $byte2 = array_shift($bytes);
    if(!$byte2) {
      $byte2 = 0;
    }

    fwrite($fp, pack("n*", ($byte1 << 8) + $byte2); // big endian byte order ?
}

fclose($fp);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.