问题
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
回答1:
// 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");
来源:https://stackoverflow.com/questions/11131789/how-to-convert-hexadecimal-byte-stream-in-the-form-of-string-to-actual-bytestrea