如何使用javascript中pdf的字节数组在浏览器中显示pdf?

用户

我有一个控制器,它发送响应实体作为对ajax调用的响应,该调用以pdf的字节数组形式。现在,我想在浏览器中显示它,但没有任何效果。我尝试了来自旧stackoverlow问题的所有建议,但没有任何效果。

这是我对弹簧控制器的回应

     `  %PDF-1.4
       %����
       6 0 obj
  <</Filter/FlateDecode/Length 1983>>stream
  x�� .......... [snip rest of the output]`

这是我的ajax代码

 $(".form").submit(function(e) {
                    var form = $(this);
                    var url = _contextPath + "pdf/" + id;
                    $.ajax({
                        type: "GET",
                        url: url,
                        data: form.serialize(),
                        datatype: "application/pdf",
                        success: function(data, textStatus, jqXHR)
                        {
                            console.log(data);
                            let pdfWindow = window.open("");
                            var bb = btoa(encodeURIComponent((data.replace(/[\u00A0-\u2666]/g, function(c) {
                                return '&#' + c.charCodeAt(0) + ';';
                            }))));
                            console.log(bb);
                            var file = new Blob([bb], {type:'application/pdf'});
                            console.log(file);
                            var fileUrl = URL.createObjectURL(file);
                            pdfWindow.document.write("<iframe width='100%' height='100%' src= '"+file+"'></iframe>");
                            /*var pdfData = btoa(unescape(encodeURIComponent(data)));
                            console.log(pdfData);
                            var pdfDataa = atob(pdfData);
                            console.log(pdfDataa);*/
                           /* var bb = btoa(encodeURIComponent((data.replace(/[\u00A0-\u2666]/g, function(c) {
                                return '&#' + c.charCodeAt(0) + ';';
                            }))));
                            console.log(bb);
                            var file = new Blob([bb], {type:'application/pdf'});
                            var fileUrl = URL.createObjectURL(file);
                            window.open(fileUrl,'', 'height=650,width=840');*/
                            //console.log(data);
                        //    window.open("data:application/pdf;base64, " + data, '', 'height=650,width=840');
                            /*var blob = new Blob( [data], { type: "application/pdf" });
                            var fileURL = URL.createObjectURL(blob);
                            var win = window.open();
                            win.document.write('<iframe src="' + fileURL + '" frameborder="0"' +
                                ' style="border:0; top:0px; left:0px; bottom:0px;' +
                                ' right:0px; width:100%; height:100%;" allowfullscreen></iframe>')*/
                           /* var datauri = 'data:application/pdf;base64,' + Base64.encode(data);
                            var win = window.open();
                            win.document.write('<iframe src="' + datauri + '" frameborder="0"' +
                                ' style="border:0; top:0px; left:0px; bottom:0px;' +
                                ' right:0px; width:100%; height:100%;" allowfullscreen></iframe>');*/
                            //var base64EncodedStr = btoa(unescape(encodeURIComponent(data)));
                            //window.open(data,"_blank","scrollbars=yes,resizable=yes");
                            //window.open("data:application/pdf," + encodeURI(data));
                           // window.open("data:application/pdf," + escape(data));
                            //window.open("data:application/pdf," + base64EncodedStr);
                          //  window.open("data:application/octet-stream;charset=utf-16le;base64,"+base64EncodedStr);

                          //  let pdfWindow = window.open("")
                           //   pdfWindow.document.write("<iframe width='100%' height='100%' src='data:application/pdf;base64, "
                           //     + blob+"'></iframe>");
                          /*  const byteArray = data;
                            const blob = new Blob([byteArray], {type: 'application/pdf'});
                            const blobURL = URL.createObjectURL(blob);
                            var win = window.open();
                            win.document.write('<iframe src="' + blobURL + '" frameborder="0"' +
                                ' style="border:0; top:0px; left:0px; bottom:0px;' +
                                ' right:0px; width:100%; height:100%;" allowfullscreen></iframe>');*/
                           /* var len = data.length;
                            var buffer = new ArrayBuffer(len);
                            var view = new Uint8Array(buffer);
                            for (var i = 0; i < len; i++) {
                                view[i] = binary.charCodeAt(i);
                            }
                            */
                            /*var base64EncodedStr = btoa(unescape(encodeURIComponent(data.toString())));
                            var pdfData = base64EncodedStr;

                            var x = window.open();
                            var iframe = x.document.createElement('iframe')
                            iframe.width = '100%'
                            iframe.height = '100%'
                            iframe.frameBorder = 0
                            iframe.style = "border: 0"
                            iframe.src = "data:application/pdf;base64, " + pdfData
                            x.document.body.appendChild(iframe);*/
                           // $('.form').unbind('submit').submit();

                        }
                    });
                    e.preventDefault();
                });

我尝试了所有事情,但没有工作

用户

在这里找到解决方案,我正在以%PDF-1% 的形式发送来自spring控制器的字节字节所以我从spring控制器发送base64编码的字符串,并将base64编码的字符串发送到浏览器,并且可以正常工作。

JavaScript代码:

  var arrrayBuffer = base64ToArrayBuffer(data); //data is the base64 encoded string
                            function base64ToArrayBuffer(base64) {
                                var binaryString = window.atob(base64);
                                var binaryLen = binaryString.length;
                                var bytes = new Uint8Array(binaryLen);
                                for (var i = 0; i < binaryLen; i++) {
                                    var ascii = binaryString.charCodeAt(i);
                                    bytes[i] = ascii;
                                }
                                return bytes;
                            }
                            var blob = new Blob([arrrayBuffer], {type: "application/pdf"});
                            var link = window.URL.createObjectURL(blob);
                            window.open(link,'', 'height=650,width=840');

在Spring Controller中将字节数组转换为base64编码的字符串

 String encodedString = Base64.getEncoder().encodeToString(bytearrayofpdf);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在浏览器中使用打印按钮将字节数组显示为PDF吗?

如何在Javascript中从<embed>内的字节数组显示PDF

从字节数组在Xamarin App中显示PDF

如何使用jsp或javascript在服务器上的浏览器中显示pdf

如何从Angular 9中的字节数组中打开PDF?

如何从Spring Controller返回字节数组作为图像,以便浏览器可以显示它?

如何在PDF(1.4)字节数组中搜索目标字符串?

如何在C#中将标记的src设置为字节数组中的pdf内容

如何将Pdf文件转换为php中的字节数组并发送?

如何从 GET 申请中获取 pdf 档案的字节数组?

如何在Javascript中存储字节数组

如何在浏览器中显示PDF文件

如何显示在HTML / JavaScript中存储为字节数组的图像?

使用Rest服务在浏览器中显示pdf

使用Java Servlet在浏览器中显示Pdf

如何将字节数组转换为pdf?

如何将字节数组转换为pdf

返回pdf字节数组WCF

从字节数组创建损坏的PDF

如何使用servlet在浏览器中编写pdf文件?

如何使用Python在浏览器中预览pdf文件?

如何从C中的字节数组中删除字节?

在 mvc 控制器的操作中返回图像字节数组并在 javascript 中显示图像

使用 React/Redux 从 ASP.NET WEB API 获取要在 PDF 中呈现的字节数组

如何使用纯Java代码获取pdf字节数组的页面大小?

如何使用iText7将.p7s字节数组插入PDF?

将Word文档转换为内存中的pdf字节数组

从WebAPI发送的字节数组中的Angular 6 open pdf

在 blazor 中显示字节数组中的图像