Skip to content

Instantly share code, notes, and snippets.

@randycasburn
Created August 1, 2017 16:20
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save randycasburn/802f278c3cfef0873dd086c34bbb9fee to your computer and use it in GitHub Desktop.
Save randycasburn/802f278c3cfef0873dd086c34bbb9fee to your computer and use it in GitHub Desktop.
This makes a HTTP GET request from within Adobe Acrobat
/*
This file must be placed with the user JavaScript file location or the Acrobat JavaScript file location.
This provides FOLDER level privilege for the script.
This script WILL NOT WORK if created within the document using the script editor. This is due to DOCUMENT priviledge restrictions.
Once installed,
1. Open any PDF file in Acrobat
2. Pull down [File] and click [Get Mock Data]
3. Open the JavaScript Debug Console to view output.
/--------------/
1. Adds new menu item to File menu of Acrobat - Get Mock Data
2. Calls jsonplaceholder mock data service and requests post id # 1 to be returned
3. Upon return, prints any errors to the console, converts the stream to a string, convers the string to a JS object and prints
the object properties and values to the console.
*/
// Make HTTP GET request
ajax = app.trustedFunction(function(fURL) {
app.beginPriv();
Net.HTTP.request({ cVerb:"GET", cURL:fURL, oHandler: ajaxCallback});
app.endPriv();
});
// process the response
ajaxCallback = {
response:function(msg,uri,e){
var stream = msg;
var string = "";
var error = e == undefined? 'No HTTP errors' : "ERROR: " + e;
string = SOAP.stringFromStream( stream );
oResult = JSON.parse(string);
console.println(error);
console.println( "id: " + oResult.id);
console.println( "userId: " + oResult.userId);
console.println( "title: " + oResult.title);
console.println( "body: " + oResult.body);
}
};
// Add menu item to kick it all off
app.addMenuItem({
cName: "Get Mock Data", cParent: "File",
cExec: 'ajax("https://jsonplaceholder.typicode.com/posts/1");',
nPos: 0});
@zachary-kaelan
Copy link

Hey, how do I send one request, get a value from the response, then use that value in a second request?

@zachary-kaelan
Copy link

Nevermind, I just had to create another trusted function for the second request, and then call that function from the callback for the first request, feeding the value into it.

@randycasburn
Copy link
Author

randycasburn commented Nov 3, 2021 via email

@fclcodan
Copy link

It works for me. But how do I get the oResult.id for instance into a field (of the PDF file) instead of the console? Any help appriciated.

@randycasburn
Copy link
Author

It works for me. But how do I get the oResult.id for instance into a field (of the PDF file) instead of the console? Any help appriciated.

Hi fclcodan - replace the console.log() with .getField(name). Assign the field object to a variable and then simply assign the variable a value. That should do it. Something like this:

var fieldObj = this.getField('myFieldName');
fieldObj.value = oResult.id;

The docs can be found here: https://opensource.adobe.com/dc-acrobat-sdk-docs/library/jsapiref/JS_API_AcroJS.html#field

@xator91
Copy link

xator91 commented Mar 1, 2024

If I want to run a specific GET or POST request in a Adobe Javascript Button can I call your script to run it?

Also, after update of Adobe will be deleted?

I want to send a post request similar to this:

function leerenboard() {
try {
var campoVuoto = false; // Variabile di controllo
var campoVuotoNome = null; // Memorizza il nome del campo vuoto
var selectboard = 1;

    var dati = {
        selectboard: selectboard

    };

    var datiJSON = JSON.stringify(dati);

    // Invia app.launchURL con i dati della singola riga
    var url = "https://localhost/script/scriptleeren.php?dati=" + encodeURIComponent(datiJSON);

    app.launchURL(url, true);

    if (!campoVuoto) {
        console.println("Tutti i dati sono stati inviati con successo.");
    } else {
        console.println("Nessun dato da inviare o campo vuoto trovato.");
        console.println("Campo vuoto individuato: " + campoVuotoNome);
    }
} catch (e) {
    console.println("Errore durante l'invio dei dati: " + e);
}

}

String.prototype.capitalize = function() {
return this.charAt(0).toUpperCase() + this.slice(1);
};

@randycasburn
Copy link
Author

@xator91 - Mirko - What have you tried? What didn't work?

@xator91
Copy link

xator91 commented Mar 20, 2024

@xator91 - Mirko - What have you tried? What didn't work?

I have a PHP script that wait for a get request through URL, and i want that work without opening an external browser, is that possible?

  • Now it works. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment