Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/431.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在javascript中将字符串形式的十六进制字节流转换为实际的ByTestStream?_Javascript_Pdf Generation - Fatal编程技术网

如何在javascript中将字符串形式的十六进制字节流转换为实际的ByTestStream?

如何在javascript中将字符串形式的十六进制字节流转换为实际的ByTestStream?,javascript,pdf-generation,Javascript,Pdf Generation,我有一个pdf文件,它是以六边形字符串ByTestStream的形式提供的,如: "255044462D312E330D0A25E2E3CFD30D0A322030206F626A......" (这是一个很长的字符串,所以我刚刚给出了格式) 浏览器中提供的字符串格式 我需要将其转换为十六进制格式,以便通过将此输出流写入pdf文件,在web浏览器中呈现pdf。我需要知道是否有任何内置函数或任何方法可以在Javascript中实现 我知道用Java实现这个功能很简单,但我有一个ABAP后端,它只

我有一个pdf文件,它是以六边形字符串ByTestStream的形式提供的,如:

"255044462D312E330D0A25E2E3CFD30D0A322030206F626A......"
(这是一个很长的字符串,所以我刚刚给出了格式) 浏览器中提供的字符串格式

我需要将其转换为十六进制格式,以便通过将此输出流写入pdf文件,在web浏览器中呈现pdf。我需要知道是否有任何内置函数或任何方法可以在Javascript中实现

我知道用Java实现这个功能很简单,但我有一个
ABAP
后端,它只获取这个字符串,还有
SAPUI5
前端,它是一个基于Javascript的框架

我通过编写一个简单的java程序来检查此ByTestStream的有效性,该程序生成PDF只是为了测试数据是否正确:

public static void main(String[] args) {
  FileOutputStream fop = null;
  File file;
  file = new File("C:/Users/I074098/Desktop/Project Temps/test.pdf");
  fop = new FileOutputStream(file);
  // if file doesnt exists, then create it
  if (!file.exists()) {
    file.createNewFile();
  }
  //I manually added "(byte) 0x" in the above string and 
  //converted it to the following format, I skipped that conversion in this code
  byte[] contentInBytes = new byte[]{
    (byte) 0x25,(byte) 0x50,(byte) 0x44,(byte) 0x46, .....
  } 
  fop.write(contentInBytes);
    fop.flush();
    fop.close();
  } 
这将生成pdf。但我知道这是非常低效的,我不确定所有这些都可以用javascript完成。 我做了很多搜索,但没有结果。我将感谢所有的帮助

问候,, Riswan

//到IE船的方法在这里,
如果(!window.btoa){
var tableStr=“abcdefghijklmnopqrstuvxyzabcdefghijklmnopqrstuvxyz012456789+/”;
var table=tableStr.split(“”);
window.btoa=功能(bin){
对于(变量i=0,j=0,len=bin.length/3,base64=[];i255)抛出新错误(“字符串包含无效字符”);
base64[base64.length]=表[a>>2]+表[((a>4)]+
(isNaN(b)?“=”:表[(b>6)])+
(伊斯南(b+c)?“=”:表[c&63]);
}
返回base64.join(“”);
};
} 
//脚本块
//尝试用这种方式在javascript中打开pdf文件,希望它能帮助您。
函数hexToBase64(str)
{
返回btoa(String.fromCharCode.apply(null,str.replace(/\r |\n/g,“”)。replace(/([\da-fA-F]{2})))/g,“0x$1”)。replace(/+$/,“”)。split(“”));
}
var data=hexToBase64(“25504462D312E330D0A25E2E3CFD30D0A322030206F626A”);//这里传递大的十六进制字符串
//它将像这样在web浏览器中打开
document.location.href='data:application/pdf;base64'+数据;
**//在新窗口中打开**
window.open('data:application/pdf;base64,'+data,“\u blank”,“directories=no,status=no,menubar=no,scrollbars=yes,resizeable=no,width=600,height=280,top=200,left=200”);

非常感谢Tamkeen!我欠你的。它在除IE以外的所有浏览器中都能正常工作。IE中的btoa()无法识别。创建的pdf无法下载(但我可以打印)。由于document.location.href,pdf显示在同一页面上。我尝试了:window.open('data:application/pdf;base64'+binData,'mywindow','location=0,titlebar=0,width=1000,height=600');在新窗口中打开,但它不起作用。关于如何通过设置标题使其可下载并在新窗口中打开,有任何帮助吗?再次衷心感谢。@riswan Answer更新了ie boat方法,您需要添加a代码块,它现在可以工作,对于下载,我不知道我已经在服务器端完成了,而不是在javscript中,您可能需要查看响应标题,即“内容类型”、“应用程序/八位字节流”、“内容处置”、“附件;文件名=文件名.pdf”,如果您更改document.location.href='data:application/pdf;base64',+数据到document.location.href='data:application/octet stream;base64',+数据您将获得t他有一个可下载的选项,但问题是它会。部分扩展文件不是pdf。它可能会帮助你看到这一点,以保存pdf或文本文件再次感谢Tamkeen。十六进制到基数64转换正在发生在IE中的方法,你给了,但我不能显示pdf,因为我猜有一个在IE中的url长度限制,因为整个pdf是附加的对于url,它没有显示。由于IE是用于测试我的应用程序的主要浏览器,这是一个巨大的问题。我可以在一个新窗口中打开。我尝试使用“应用程序/八位字节流”,但它只打开一个空窗口。我不确定是否可以使用Downloadify,但我会研究这个问题。
   //  To IE Boat method is here,

        if (!window.btoa) {
    var tableStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    var table = tableStr.split("");

    window.btoa = function (bin) {
    for (var i = 0, j = 0, len = bin.length / 3, base64 = []; i < len; ++i) {
    var a = bin.charCodeAt(j++), b = bin.charCodeAt(j++), c = bin.charCodeAt(j++);
    if ((a | b | c) > 255) throw new Error("String contains an invalid character");
    base64[base64.length] = table[a >> 2] + table[((a << 4) & 63) | (b >> 4)] +
    (isNaN(b) ? "=" : table[((b << 2) & 63) | (c >> 6)]) +
    (isNaN(b + c) ? "=" : table[c & 63]);
    }
    return base64.join("");
    };
    } 

      //script block
      // try this way  to open you pdf file like this in javascript hope it will help you.
        function hexToBase64(str)
          {
         return btoa(String.fromCharCode.apply(null,str.replace(/\r|\n/g, "").replace(/([\da-fA-F]{2}) ?/g, "0x$1 ").replace(/ +$/, "").split(" ")));
          }

          var data=   hexToBase64("255044462D312E330D0A25E2E3CFD30D0A322030206F626A");// here pass the big hex string

            // it will be open in the web browser like this
            document.location.href = 'data:application/pdf;base64,' +data;

     **//to open up in the new window**
    window.open('data:application/pdf;base64,' +data, "_blank", "directories=no, status=no, menubar=no, scrollbars=yes, resizable=no,width=600, height=280,top=200,left=200");