Output a PDF in PHP from a byte array Output a PDF in PHP from a byte array curl curl

Output a PDF in PHP from a byte array


I had to do the same thing. For me, I was generating the PDF server-side, but wanted to send it down with a bunch of other data to the client in an AJAX response. Here's the JavaScript that I ended up with. The Blob and saveAs methods are rather new technologies, so you might want to (as I did) get the polyfills for each of them, linked to above.

// Convert the Base64 string back to text.var byteString = atob(data.reportBase64Bytes);// Convert that text into a byte array.var ab = new ArrayBuffer(byteString.length);var ia = new Uint8Array(ab);for (var i = 0; i < byteString.length; i++) {    ia[i] = byteString.charCodeAt(i);}// Blob for saving.var blob = new Blob([ia], { type: "application/pdf" });// Tell the browser to save as report.pdf.saveAs(blob, "report.pdf");// Alternatively, you could redirect to the blob to open it in the browser.//document.location.href = window.URL.createObjectURL(blob);


At first, I thought this would be a great use of the JavaScript library PDF.js, but then I realized that you wanted to download the file. The best place to do this would be in PHP setting all of the appropriate headers along the way.

However, there may be something that will work for you, but I have never tried it with PDFs. I have seen image saves done well with data url's in some browsers. A great example of this is Canvas2Image. The code would look something liket he following:

document.location.href = "data:application/pdf;base64," + base64PDFdata;

Where base64PDFdata would be your byte array converted to base 64 string representation. No guarantees on this working, but it may be worth a shot.


As mention in my comment to your question, it looks like you're getting a string representation of an array of decimal representations of bytes. e.g. "[37,80,68,70]"

You likely need to transform that.

Can be done like this:

// with:$data = "[50,20,36,34,65]";// remove brackets$data = trim($data,'[]');// split into array on ',' character$data = explode(',',$data);// transform each decimal value to a byte   representation. chr does just that$data = array_map('chr',$data);// turn the resulting array back into a string$data = implode('',$data);

The comments about setting the headers to force download are also relevant so you should use that too.

Here's the final code:

<?php// This is good to keepheader('Content-Type: application/pdf');header('Content-Disposition: attachment; filename="output.pdf"');$userPDF = $_POST['username'];$passPDF = $_POST['password'];$idPDF = 'MO-N007175A';//PDF Function$result = array("id" => $idPDF, "username" => $userPDF, "password" => $passPDF);                                                                    $result_string = json_encode($result);                                                $ch = curl_init('http://********.com/******/v1/DealPdf');                                                                      curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     curl_setopt($ch, CURLOPT_POSTFIELDS, $result_string);                                                                 curl_setopt($ch, CURLOPT_VERBOSE, 1 );curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));$result = curl_exec($ch);// remove brackets$result = trim($result,'[]');// split into array on ',' character$result = explode(',',$result);// transform each decimal value to a byte   representation. chr does just that$result = array_map('chr',$result);// turn the resulting array back into a string$result = implode('',$result);echo $result;?>

Note that echo is used here, var_dump is not good as it adds extra formatting to the output.

Also, note, you're not doing any testing on the result of the curl, if it fails you should probably handle the output differently.