Output a PDF in PHP from a byte array

前端 未结 5 1180
北恋
北恋 2021-01-01 06:00

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

5条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-01 06:51

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

提交回复
热议问题