Copy link to clipboard
Copied
I have a fillable PDF document with form fields and submit button.
Submit button originally calls a REST api end point:
submitForm({cURL: 'https://api.example.com,cSubmitAs: 'XFDF' });
However, the REST api requires basic authentication and doesn't support SSO.
So, I am looking for other options.
As a workaround, I am trying to export the form fields as a string and include it as a Query parameter and launch the application URL in the browser(see code snippet below). Application URL supports SSO and once user is authenticated successfully, I plan to use the data from the query parameter to invoke the REST api.
Issue: See code snippet below. When URL value with Query parameters stored in "finalAppURL" exceeds 2076 characters, it is getting truncated once the browser is launched. Using the PDF Javascript debugger to do a console.println of the variable "finalAppURL" , I verified that URL is generated correctly and app.launchURL seems to be the issue. I also used the "finalAppURL" value from the debugger ouput directly in the browser and it is not getting truncated in the browser/application.
Could app.launchURL be causing the issue?
This is my exposure to PDF scripting.
Is there any better way to address this use case/issue?
Thanks!
var cXFDF = this.exportAsXFDFStr();
var documentStream = Net.streamFromString(cXFDF);
var encodedStream = Net.streamEncode(documentStream,"base64");
var encodedcXFDF = Net.stringFromStream(encodedStream);
var appURL = 'https://example.com?formdata=';
//get rid of '=','+','/' in the Query paramaters that is base64 encoded
//as URL encode is not handling this
var encodedcXFDF = this.encodeURI(encodedcXFDF);
var regex = /\+/gi;
encodedcXFDF = encodedcXFDF.replace(regex,'.');
regex = /\//gi;
encodedcXFDF = encodedcXFDF.replace(regex,'_');
regex = /=/gi;
encodedcXFDF = encodedcXFDF.replace(regex,'-');
var finalAppURL = this.encodeURI(appURL) + encodedcXFDF;
app.launchURL(finalAppURL);
Copy link to clipboard
Copied
Your approach of encoding the form data as a base64 string and appending it as a query parameter is creative, but as you've discovered, app.launchURL has limitations on the length of the URL it can handle.
Here are a few alternative approaches you can consider to address this issue:
If the URL length is an issue, consider splitting the data into multiple smaller chunks and making multiple requests. This approach may be complex and might require a server-side component to reconstruct the data.
Instead of using app.launchURL with a long URL, use a hidden form to send the data via a POST request. This method doesn't have the same URL length limitations as GET requests. Here's an example of how you might do this using JavaScript for Acrobat:
var cXFDF = this.exportAsXFDFStr();
var documentStream = Net.streamFromString(cXFDF);
var encodedStream = Net.streamEncode(documentStream, "base64");
var encodedcXFDF = Net.stringFromStream(encodedStream);
// Construct an HTML form
var html = `
<html>
<body onload="document.forms[0].submit()">
<form action="https://example.com" method="post">
<input type="hidden" name="formdata" value="${encodedcXFDF}">
</form>
</body>
</html>
`;
// Create a temporary file and write the HTML to it
var tempFile = new File("/c/temp_form.html");
tempFile.open("w");
tempFile.write(html);
tempFile.close();
// Launch the temporary HTML file, which will automatically submit the form
app.launchURL("file:///c/temp_form.html", true);
This method creates a temporary HTML file containing a form with the encoded data and uses app.launchURL to open the file, which automatically submits the form.
You can use the Net.HTTP.request method to make an HTTP POST request directly from the PDF. This way, you can handle the authentication and data transfer directly in the script without relying on URL length.
Here's an example:
var cXFDF = this.exportAsXFDFStr();
var documentStream = Net.streamFromString(cXFDF);
var encodedStream = Net.streamEncode(documentStream, "base64");
var encodedcXFDF = Net.stringFromStream(encodedcXFDF);
var url = "https://api.example.com";
var params = {
cVerb: "POST",
cURL: url,
cUser: "your-username",
cPassword: "your-password",
oRequestBody: encodedcXFDF,
oHeaders: {
"Content-Type": "application/x-www-form-urlencoded"
}
};
var response = Net.HTTP.request(params);
console.println("Response: " + response);
If you have control over the server, you could create a proxy endpoint that accepts the data via a POST request, authenticates the user, and then forwards the data to the REST API. This way, you can avoid dealing with client-side limitations and handle everything securely on the server side.
Copy link to clipboard
Copied
TeMatcha, thank you so much for providing these great options !
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more