Output PDF to PHP from an array of bytes

I get a byte array from the WCF service, which generated a PDF file and converted it to a byte array. I need to get a byte array and use PHP or Javascript (or jQuery) to take this byte array and convert it back to a downloadable PDF file. I would prefer a solution in Javascript, but PHP will work just fine too.

The code I use to get the PDF:

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

var_dump($result); -

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

The array has been going on for some time ... therefore I have provided a small part, for example, for purposes.

Where can I start getting pdf from this array?

EDIT To clarify, the WCF service returns the actual PDF file in a byte array only. I need to save this byte array as PDF on the client machine. I used fwrite, etc., but I have to miss something, because I do not see it working.

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

+3
source share
5 answers

I had to do the same. For me, I created the server on the server side of the PDF, but wanted to send it with a bunch of other data to the client in the AJAX response. Here is the javascript I've finished with. The Blob and saveAs methods are rather new technologies, so you may want (like me) to get policies for each of them related to it.

 // 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); 
+13
source

At first I thought it would be very useful to use the JavaScript library PDF.js , but then I realized that you want to download the file. The best place for this would be in PHP , setting all the appropriate headers along the way.

However, there may be something that will work for you, but I have never tried it using PDF . I saw that in some browsers the data image was well preserved. A great example of this is Canvas2Image . The code will look like this:

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

Where base64PDFdata will be your byte array converted to a string representation of base 64. There is no guarantee for this to work, but it may be worth it.

+2
source

Set the MIME header with the header () to match the PDF file and then print the byte array.

Something like this: In PHP, it displays as an array of bytes and a stream. Which one is better?

0
source

Now I see what you want to do!

Try the following:

 <?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); ?> 
0
source

As mentioned in my comment on your question, it looks like you are getting a string representation of an array of decimal representations of bytes. for example "[37,80,68,70]"

You probably need to convert this.

You can do 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); 

Notes on setting headers to force downloads are also relevant, so you should also use this.

Here's the last 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 suitable as it adds extra formatting to the output.

Also, note: you do not run any tests on the result of curl, if that fails, you probably should handle the output differently.

0
source

Source: https://habr.com/ru/post/1236366/


All Articles