Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
315 views
in Technique[技术] by (71.8m points)

pdf generation - How to convert Hexadecimal Byte stream in the form of string to actual bytestream in javascript?

I have a pdf file that is available in the form of a Hexa string bytestream like:

"255044462D312E330D0A25E2E3CFD30D0A322030206F626A......"

(its a very long string, so I have just given the format) which is in a string format available in the browser.

I need to convert this into its hexadecimal format so that I can render the pdf in the web browser by writing this output stream to the pdf file. I need to know if there is any inbuilt function or any way this can be achieved in Javascript.

I know implementing this functionality is simple with Java but I have an ABAP backend that only fetches me this string and SAPUI5 front end that is a framework based on Javascript.

I checked the validity this bytestream by writing a simple java program that generated the PDF just for testing purpose if the data is coming right:

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

This generates the pdf. But I know it is very inefficient and I am not sure all this can be done in javascript. I have done a lot of search and it was not fruitful. I will be thankful any and every help.

Regards, Riswan

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
   //  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(/
|
/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");

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...