Introduction
Creating a PDF file from a binary string received from a web service can be a useful task in many web applications. However, using data:uri to embed the binary data in browsers such as IE9 and Firefox can lead to issues. This article explores solutions that work across different browsers.
Solution: File System Creation
As mentioned by the original poster, one solution involves creating a PDF file on the file system and prompting the user to download it. This approach ensures compatibility with various browsers.
Implementation
To implement this solution, you can use the following steps:
Here is a code example:
<code class="javascript">var request = new XMLHttpRequest(); request.open("GET", "/path/to/pdf", true); request.responseType = "blob"; request.onload = function (e) { if (this.status === 200) { var file = window.URL.createObjectURL(this.response); var a = document.createElement("a"); a.href = file; a.download = this.response.name || "detailPDF"; document.body.appendChild(a); a.click(); window.onfocus = function () { document.body.removeChild(a); }; } }; request.send();</code>
Conclusion
This solution allows you to build PDF files from binary strings in a way that works across browsers like IE, FF, Opera, Chrome, and Safari, providing a more reliable method for handling PDF downloads.
The above is the detailed content of How to Build PDF Files from Binary Strings in JavaScript Across Different Browsers?. For more information, please follow other related articles on the PHP Chinese website!