Skip to main content
Eric.promattex
Participant
December 10, 2023
Answered

Export data form in text file with encoding UTF-8

  • December 10, 2023
  • 1 reply
  • 1949 views

I have a pdf form with javascript to export some data in fichier text with the following script :

var Data = 
	this.getField("Texte3").value	
var doc =
	this.createDataObject({cName:"Data.txt",cValue: Data});
	this.exportDataObject({cName:"Data.txt",nLaunch:2});

If there are no special characters in the field, the text file is in UTF-8, but as soon as there is a special character, the text file is in ANSI.

Would there be a solution for the text file to remain in utf-8 but if there are special characters in the field?

This topic has been closed for replies.
Correct answer try67

the JS reference is this: https://www.t10.org/ftp/js_api_reference.pdf?
yes on page 338 but I don't understand their example


Try this code:

 

var myData = this.getField("Texte3").valueAsString;
var myDataStream = util.streamFromString(myData, "utf-8");
this.createDataObject({cName:"Data.txt",cValue: ""});
this.setDataObjectContents("Data.txt", myDataStream);
this.exportDataObject({cName:"Data.txt", nLaunch:2});

 

1 reply

try67
Community Expert
Community Expert
December 10, 2023

Use the setDataObjectContents method to populate the file. That way you would be able to specify the encoding directly. There's an example of that in the JS reference for that method.

Eric.promattex
Participant
December 10, 2023

thank you for your reply
Could you give me an example with the above script?

var Data = 
	this.getField("Texte3").value	
var doc =
	this.createDataObject({cName:"Data.txt",cValue: Data});
	this.exportDataObject({cName:"Data.txt",nLaunch:2});
try67
Community Expert
Community Expert
December 10, 2023

Did you look in the JS Reference?