JavaScript blob 编码为 UTF-8 而不是 ANSI [英] JavaScript blob encoding as UTF-8 instead of ANSI
问题描述
我使用 JavaScript blob 创建一个 FDF 文件,该文件打开 &填写本地存储的 PDF.
I use a JavaScript blob to create an FDF file which opens & fills in a locally stored PDF.
但是,本地存储的 PDF 的文件路径包含重音字符(我无法编辑文件夹名称).
However, the file path to the locally stored PDF contains an accented character (and I am unable to edit the folder name).
当文件夹路径不包含重音并且如果我在记事本中打开 fdf 时,此代码有效,默认编码为 ANSI.但是,当文件夹路径包含重音符号时,FDF 将打开一条消息,指出无法找到 PDF.此外,记事本中的默认编码已更改为 UTF-8.
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' + "
"
+ '1 0 obj<</FDF<</F(T:/Échange/MY_PDF.pdf)/Fields 2 0 R>>>>' + "
"
+ 'endobj' + "
"
+ '2 0 obj[' + "
"
+ '<</T(FIELD_NAME)/V(SOME_TEXT)>>' + "
"
+ ']' + "
"
+ 'endobj' + "
"
+ 'trailer' + "
"
+ '<</Root 1 0 R>>' + "
"
+ '%%EO'
var blobObject = new Blob([FDF_Text], {type: 'text/css;charset=ANSI'});
window.navigator.msSaveOrOpenBlob(blobObject, 'MY_FDF.fdf');
我试过了
- 用 E 替换 É
- 使用 String.fromCharCode(201)(É 的 chr 值)
- 改变&将 blob 本身的类型"删除到我发现的几个不同示例中(抱歉,我没有跟踪所有不同的组合).
谁能提出不同的解决方案?
Can anyone suggest a different solution?
推荐答案
你可以将数据表示为二进制,只需遍历字符串并填充一个二进制数组
You can represent the data as binary, just run through the string and fill a binary array
FDF_Text = ''
+ '%FDF-1.2' + "
"
+ '1 0 obj<</FDF<</F(T:/Échange/MY_PDF.pdf)/Fields 2 0 R>>>>' + "
"
+ 'endobj' + "
"
+ '2 0 obj[' + "
"
+ '<</T(FIELD_NAME)/V(SOME_TEXT)>>' + "
"
+ ']' + "
"
+ 'endobj' + "
"
+ 'trailer' + "
"
+ '<</Root 1 0 R>>' + "
"
+ '%%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');
这篇关于JavaScript blob 编码为 UTF-8 而不是 ANSI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!