function saveValues(){ var arr = []; //Gather values into an array for( var i = 0 ; i < this.numFields; i++){ arr[i] = this.getNthFieldName(i); } this.submitForm({ cURL: "http://foo.com/saveValues#FDF", aFields: arr, bEmpty: "true", cSubmitAs: "HTML"}); }Note the '#FDF' on the end of the url. If the PDF is viewed in a browser, this tells the browser that it should expect an FDF file as the response. Without this, the browser will treat the response as a regular web page. In this example, 'submitForm' function sends the values in the array 'arr' to 'http://foo.com/saveValues' as HTML key-value pairs. The function also supports a few other options that you can look up later. Perhaps you are wondering why we want an FDF response instead of some other type of response. The answer, in my case, is that it gives the PDF document in the browser a response without having the browser go to a different page. For example, if you want to save some values that the user has entered in the fields of a PDF and send them to a server, you don't want the browser to go to a different page because the user may still be filling in the fields. An FDF response allows you to save without leaving the page, and you can call a javascript function on the return trip. As an example, here is about the simplest FDF you might need:
%FDF-1.2 1 0 obj << /FDF << /JavaScript << /After (app.alert("Data Imported Successfully");) >> >> >> endobj trailer << /Root 1 0 R >> %%EOFI found that useful snippet here:
http://acrobatusers.com/forum/javascript/submitform-and-server-response-http-post
The important part to note is the 'app.alert'. On the return trip, this will cause the PDF to display 'Data Imported Successfully' in an alert box. However, notice that this is just a javascript call to the app.alert function. Replacing this with a function that exists in the PDF will allow you to call that function (or functions) on the return, allowing custom functionality:
Javascript in the PDF document:
function saveValues(){ var arr = []; //Gather values into an array for( var i = 0 ; i < this.numFields; i++){ arr[i] = this.getNthFieldName(i); } this.submitForm({ cURL: "http://foo.com/saveValues#FDF", aFields: arr, bEmpty: "true", cSubmitAs: "HTML"}); } function valuesSaved(){ app.alert("Values were saved"); } function doSomethingElse(text_1, text_2){ //Do stuff here }
FDF text to be returned:
%FDF-1.2 1 0 obj << /FDF << /JavaScript << /After (valuesSaved(); doSomethingElse('customText','moreText');) >> >> >> endobj trailer << /Root 1 0 R >> %%EOF
Cache Problems:
One thing to be aware of is that browsers like to cache all files they download. So, if you are trying to send back dynamically generated FDF files, you should probably tell the browser not to cache it. One option is to add a random number on the end of the url you are sending the values to. For example, adding '?number=12345' to the url in the example could help, where '12345' is a random number that changes for each call to submitForm. Sending headers similar to the following might also help:
headers["Cache-Control"] = "no-cache, no-store, max-age=0, must-revalidate" headers["Pragma"] = "no-cache" headers["Expires"] = "Fri, 01 Jan 1990 00:00:00 GMT"