7

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?

ThanosLovesYuna
  • 87
  • 1
  • 1
  • 8
  • How does a working FDF for that file path look like (regardless how you constructed/downloaded it)? – Bergi Sep 10 '15 at 20:56
  • It looks exactly the same. I just tested 2 identical FDF files saved to a folder on my computer, both with "É" in the file path. When open side by side in Notepad, they look identical and there is no corruption of the "É" character. The only difference is that Save As for one shows encoding of UTF-8 and the other ANSI. Running the UTF-8 version give message stating the file can't be found. Running the ANSI version works perfectly. – ThanosLovesYuna Sep 10 '15 at 21:17
  • What is `charset=ANSI`, is that valid? [Which one](http://stackoverflow.com/a/702023/1048572) do you mean? – Bergi Sep 12 '15 at 20:30
  • I’m not sure if it is valid. I can’t find a list of valid blob second parameters anywhere. I ran some tests using ASCII, ISO/IEC 8859, and Windows-1252, no luck. I don’t know what that parameter does exactly because I also tried removing it as well as entering gibberish. No difference. The solution I went with is sad but works: the New Blob creates a vbs (user has to click an extra security warning when opened), the vbs writes the FDF on the local drive and then executes it. When the FDF is written locally, it is saved to Notepad’s default encoding. In this case, that is ANSI. – ThanosLovesYuna Sep 14 '15 at 15:59
  • It seems that the Blob type matches the HTTP `content-type` header ([as documented](https://msdn.microsoft.com/de-de/library/hh772332(v=vs.85).aspx)) so you should try valid values of that (`ISO-8859-1` seems to be common as well). Regardless, I guess your problem is actually that you're constructing the Blob from a "DOM String", which is inherently encoded as UTF-16, and written as such into the blob's buffer. – Bergi Sep 14 '15 at 20:37

1 Answers1

10

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');
Musa
  • 96,336
  • 17
  • 118
  • 137