从字节数组输出PHP中的PDF [英] Output a PDF in PHP from a byte array
问题描述
我从WCF服务获取一个字节数组,生成PDF并将其转换为字节数组。我需要能够获得字节数组,并使用PHP或Javascript(或jQuery)采取该字节数组,并将其转换回可下载的PDF。我希望在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.
我用来获取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($ result);
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.
我从哪里开始从这个数组中获取PDF?
Where do I start on getting a PDF out of this array?
EDIT
- 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,它会输出文件?
EDIT $ b
Also - If I do use fwrite, where does it output the file? EDIT
推荐答案
我不得不做同样的事情。对我来说,我是生成PDF服务器端,但想要发送一堆其他数据到AJAX响应中的客户端下面是我结束了的JavaScript。 Blob 和saveAs 方法是相当新的技术,所以你可能想要(像我一样)得到每个polyfill,链接到上面。 p>
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屋!