JavaScript blob encoding as UTF-8 instead of ANSI

橙三吉。 提交于 2020-03-18 06:31:50

问题


I use a JavaScript blob to create an FDF file which opens & fills in a locally stored PDF.

However, the file path to the locally stored PDF contains an accented character (and I am unable to edit the folder name).

This code works when the folder path doesn’t contain an accent and if I open the fdf in Notepad, the default encoding is ANSI. But when the folder path contains an accent, the FDF opens to a message stating the PDF cannot be found. Furthermore, the default encoding in Notepad has changed to UTF-8.

FDF_Text = ''
    + '%FDF-1.2' + "\n"
    + '1 0 obj<</FDF<</F(T:/Échange/MY_PDF.pdf)/Fields 2 0 R>>>>' + "\n"
    + 'endobj' + "\n"
    + '2 0 obj[' + "\n" 
    + '<</T(FIELD_NAME)/V(SOME_TEXT)>>' + "\n"
    + ']' + "\n" 
    + 'endobj' + "\n"
    + 'trailer' + "\n"
    + '<</Root 1 0 R>>' + "\n"
    + '%%EO'


var blobObject = new Blob([FDF_Text], {type: 'text/css;charset=ANSI'}); 
window.navigator.msSaveOrOpenBlob(blobObject, 'MY_FDF.fdf');

I have tried

  • replacing É with E
  • using String.fromCharCode(201) (the chr value for É)
  • changing & removing the "type" of the blob itself to several different examples I've found (sorry I didn't keep track of all the different combinations).

Can anyone suggest a different solution?


回答1:


You can represent the data as binary, just run through the string and fill a binary array

    FDF_Text = ''
        + '%FDF-1.2' + "\n"
        + '1 0 obj<</FDF<</F(T:/Échange/MY_PDF.pdf)/Fields 2 0 R>>>>' + "\n"
        + 'endobj' + "\n"
        + '2 0 obj[' + "\n" 
        + '<</T(FIELD_NAME)/V(SOME_TEXT)>>' + "\n"
        + ']' + "\n" 
        + 'endobj' + "\n"
        + 'trailer' + "\n"
        + '<</Root 1 0 R>>' + "\n"
        + '%%EO'
    
    var uint8 = new Uint8Array(FDF_Text.length);
    for (var i = 0; i <  uint8.length; i++){
        uint8[i] = FDF_Text.charCodeAt(i);
    }
    var blobObject = new Blob([uint8], {type: 'text/fdf'}); 
    window.navigator.msSaveOrOpenBlob(blobObject, 'MY_FDF.fdf');


来源:https://stackoverflow.com/questions/32510273/javascript-blob-encoding-as-utf-8-instead-of-ansi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!