Output a PDF in PHP from a byte array

…衆ロ難τιáo~ 提交于 2019-12-18 13:36:59

问题


I am getting a byte array from a WCF service that generated the PDF and converted it to a byte array. I need to able to get the byte array and using either PHP or Javascript (or jQuery) take that byte array and convert it back to a downloadable PDF. I would prefer a solution in Javascript, but PHP would work fine too.

The code I'm using to get the PDF is:

<?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_VERBOSE, 1 );
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));

    $result = curl_exec($ch);
    var_dump($result);
?>

The var_dump($result); is

string(1053285) "[37,80,68,70,45,49,46,54,10,37,211,244,204,225, ...

The array goes on for a while... so I only provided a small portion for example purposes.

Where do I start on getting a PDF out of this array?

EDIT To clarify - The WCF Service IS returning an actual PDF, just in a byte array. I need to save this byte array as a PDF on the clients machine. I have used fwrite and so forth, but I must be missing something because I dont see it working.

Also - If I do use fwrite, where does it output the file? EDIT


回答1:


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);



回答2:


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.




回答3:


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 keep
header('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.




回答4:


Set MIME header with header() to match a PDF and then print out byte array.

Something like this: In PHP, output as byte array and stream. Which one is better?




回答5:


Ahh now I see what you want to do!

Try this:

<?php
    header('Content-Type: application/pdf');
    header('Content-Disposition: attachment; filename="downloaded.pdf"');

    $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://localhost/test/file.pdf');                                                                      

    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_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);

    var_dump($result);
?>


来源:https://stackoverflow.com/questions/9707604/output-a-pdf-in-php-from-a-byte-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!