Skip to content

Instantly share code, notes, and snippets.

@randycasburn
Created August 1, 2017 16:20
Show Gist options
  • Select an option

  • Save randycasburn/802f278c3cfef0873dd086c34bbb9fee to your computer and use it in GitHub Desktop.

Select an option

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});
@cabrams007

Copy link
Copy Markdown

I see that this post is over two years old but hopefully you're still available. Have you created any examples of the Net.HTTP request for the "post" method?

@randycasburn

Copy link
Copy Markdown
Author

I'm still here. It is as simple as changing the cVerb to 'Post' and adding the body. But I would refer to the latest API for the version of Acrobat reader you are targeting.

@randycasburn

Copy link
Copy Markdown
Author

@cabrams007 - This would be one example of how to accomplish what my previous post said:
Something like this should work:

// Create some Stream object:
strObj = this.createDataObject("myData", "This is the data");
// Make HTTP POST request
ajax = app.trustedFunction(function(fURL) {
    app.beginPriv();
    Net.HTTP.request({ cVerb:"POST", cURL:fURL,  oRequest: strObj, oHandler: ajaxCallback});
    app.endPriv();
});

Go here and search for Net.HTTP methods:
https://www.adobe.com/content/dam/acom/en/devnet/acrobat/pdfs/js_api_reference.pdf

@cabrams007

cabrams007 commented Sep 6, 2019 via email

Copy link
Copy Markdown

@cabrams007

Copy link
Copy Markdown

Randy,

I have asked this question to a couple of forum and no one has answered. I'm trying to upload a PDF file to URL using a PDF form. The response from the HTTP server should read ("resultCode=SUCCESS&fileAuthToken=65922027422002491920600182984091022955832") The server only accept post commands.
My goal is to emulate this simple form I currently use for testing.

`

Choose a file to upload:
`

So is it as simple as using the file location as the strObj variable in your example?

strObj = this.createDataObject("D:\Wellpath\LDD_DON_NEO_Guide.pdf","");

@nishant0013

Copy link
Copy Markdown

@randycasburn I also used the same script above to make a get request but instead, I am getting an [ object aggregate ] error. I don't know which kind of error is this. Can you help with this?

@randycasburn

randycasburn commented May 29, 2020

Copy link
Copy Markdown
Author

@randycasburn I also used the same script above to make a get request but instead, I am getting an [ object aggregate ] error. I don't know which kind of error is this. Can you help with this?

Hi @nishant0013,

I don' think that is truly the error ([ object aggregate ]). That is simply the output of printing a JS object to the console. I'll need to see the actual error text and probably any modifications you've made before I'll be able to assist you.

@randycasburn

randycasburn commented May 31, 2020 via email

Copy link
Copy Markdown
Author

@zachary-kaelan

Copy link
Copy Markdown

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
Copy Markdown

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

randycasburn commented Nov 3, 2021 via email

Copy link
Copy Markdown
Author

@fclcodan

Copy link
Copy Markdown

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
Copy Markdown
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

xator91 commented Mar 1, 2024

Copy link
Copy Markdown

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
Copy Markdown
Author

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

@xator91

xator91 commented Mar 20, 2024

Copy link
Copy Markdown

@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