从字节数组输出PHP中的PDF [英] Output a PDF in PHP from a byte array
问题描述
我正在从生成PDF和它转换为一个字节数组WCF服务的字节数组。我需要能够得到字节数组,它可以使用PHP或JavaScript(或jQuery的)采取的字节数组并将其转换回下载的PDF。我想preFER在Javascript的解决方案,但PHP将工作也没关系。
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.
在code我使用来获取PDF是:
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);
?>
的的var_dump($结果);
是
字符串(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.
我从哪里开始就得到一个PDF出此阵的?
Where do I start on getting a PDF out of this array?
修改
为了澄清 - WCF服务将返回一个实际PDF,只是在一个字节数组。我需要这个字节数组保存为客户机上的PDF文件。我用的fwrite等等,但我必须失去了一些东西,因为我没有看到它的工作。
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.
此外 - 如果我不使用FWRITE,它在哪里输出的文件?
修改
Also - If I do use fwrite, where does it output the file? EDIT
推荐答案
我必须做同样的事情。对于我来说,我是生成PDF服务器端,但希望在Ajax响应一堆其他数据向客户端发送了下去。下面是我结束了使用JavaScript。该斑点并的的saveAs 方法是相当新的技术,所以你可能想(像我一样)得到polyfills为他们每个人,联系到上面。
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);
这篇关于从字节数组输出PHP中的PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!