Adobe Acrobat Javascript Error: Value is unsupported - javascript

I'm trying to troubleshoot an issue with a PDF (Adobe Acrobat) using custom trusted function. It works on my machine (go figure), possibly some permissions issue?
When the user hits a button the PDF it is supposed to save it as a JPEG to a known folder location.
The trusted function exists in the users folder: C:\Program Files (x86)\Adobe\Acrobat DC\Acrobat\Javascripts
var CustSaveAsJpeg = app.trustedFunction(
function(d,p,f) //D=Document, p=path, f=filename
{
app.beginPriv();
p= p.replace(/([^/])$/, "$1/");
try{
d.saveAs(p + f, "com.adobe.acrobat.jpeg");
}catch(e){
app.alert("Error During Save:" + e.toString());
}
app.endPriv();
}
);
It is called like so:
var m = this.getField("ID").value;
var d = new Date();
var n = d.getSeconds();
var s = "/nas1/dfs/Shared/Departments/Saves/";
var f = m + "-" + n + ".jpeg";
if(typeof(CustSaveAsJpeg) == "function"){CustSaveAsJpeg(this,s,f);};
When i run it, I'm getting either obsolete function call, or value is unsupported Param: p.
I don't think it is an issue with the folder\file path, it works on my machine
I don't think it is a permissions issue (but it could be); the user can create/delete/modify files in that folder (acrobat executing this function under the local user?)
If it matters, i have adobe acrobat Pro DC, whereas the user has the Acrobat DC (Free version)

Related

How to call a trusted function from acroform

I have been looking for an example of how to call a trusted function in acroforms and havent found any. I am trying to build a form for my company that will allow users to click a button and automatically have the form save to a folder on our server (eg: //SERVER1/Forms/). I found this code to test with and placed it in C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\Javascripts
//SaveAs Function1
var date = new Date();
var day = date.getDate();
var month = date.getMonth()+1;
var year = date.getFullYear();
var dateSigned = String(month) + String(day) + String(year);
var mySaveDoc = app.trustedFunction(function(doc,fileNam­e){
app.beginPriv();
var myPath = "C/test/" + fileName + "Agreement " + dateSigned + ".pdf";
//saveAs is the only privileged code that needs to be enclosed
doc.saveAs({cPath: myPath, bCopy: true, bPromptToOverwrite: false});
//doc.close();
app.endPriv();
});
Any help on making this work is greatly appreciated!!
I think the location of the application level script is correct; check whether you have other files in that folder; one of them would be a precompiled one, coming from Adobe.
Now, for calling the trusted function, well…, call it as you would call any other function:
mySaveDoc(this, fileName) ;
and that should do it.
However, there are a few issues I don't like that much in the application-level script:
The dateSigned variable and its bits and pieces will be defined and initialized when the application starts, and then keep their value. In other words, if you keep Reader running all the time, the date will not be updated. To get the current date all the time, you'd have to initialize the dateSigned variable within the function. AND, as you are in Acrobat JavaScript, you can use the util object for assembling the string.
Your script would then look like this:
var mySaveDoc = app.trustedFunction(function(doc, fileName){
app.beginPriv() ;
var dateSigned = util.printd("MMDDYYYY", new Date() ;
var myPath = "/C/test" + filename + "Agreement " + dateSigned + ".pdf" ;
doc.saveAs({cPath: myPath, bCopy: true, bPromptToOverwrite: false}) ;
app.endPriv() ;
}) ;
Note that there is als a slash at the beginning of the path (although I may be wrong on that; as I don't have access to a Windows machine, I can not verify it; if someone else would use Acrobat, open any file, and then run this.path() from the Console, then he could confirm the slash (or not)).

Reading a file on the local machine with javascript

I am new in javascript. What I am trying to do is:
write a script which will read an php or txt file, take the info (a number), and replace it on the page like a banner. It will be something like a rating, and the number of this rating will be taking on the local machine.
I need some script which will work with most of browsers.
Thank you for your help!!!
Only Internet Explorer supports this via ActiveXObject. You can access the file system using ActiveXObject and then read the file.
See http://msdn.microsoft.com/en-us/library/2z9ffy99(v=vs.84).aspx
Sample Code:
function ReadFiles()
{
var fso, f1, ts, s;
var ForReading = 1;
fso = new ActiveXObject("Scripting.FileSystemObject");
f1 = fso.CreateTextFile("c:\\testfile.txt", true);
// Write a line.
Response.Write("Writing file <br>");
f1.WriteLine("Hello World");
f1.WriteBlankLines(1);
f1.Close();
// Read the contents of the file.
Response.Write("Reading file <br>");
ts = fso.OpenTextFile("c:\\testfile.txt", ForReading);
s = ts.ReadLine();
Response.Write("File contents = '" + s + "'");
ts.Close();
}

Javascript and HTML5 File API - problems getting file correctly routed to function

I am wondering how to define a file reference for an HTML5 'file' object, to be used as input to a JavaScript function that will convert the file into Base64 encoding, WITHOUT using an interactive file input element. I have seen the HTML5Rocks examples, and numerous others, but they all use an <input type=file> element to read and gather inputs about the file to operate on. I am intending to take image files (i.e. binary) as input, and output Base64 strings. (Perhaps it is obvious, but I am new to the HTML5 and JavaScript worlds)
Some of my reading seems to indicate that this isn't possible for security reasons: JS would then be able to run arbitrary files. I wanted to double check.
What is the output of the 'file' input type? Can I manually mimic it in some way? (I found one reference here about just directly including the file itself inside the JS, but can you do that with a binary file? Frankly, not sure how I would do that on the FileMaker side, though, either. My plan, up to this point, was the export the file from FileMaker to a known location, then use that location as the input to the JavaScript)
The whole picture: I am trying to create a self contained web-viewer element in FileMaker 12. In FileMaker, I can dynamically define my HTML and JavaScript BEFORE running it. I want to dynamically hardcode the JavaScript to ALREADY contain the file reference based on the information from the database (i.e. path and filename). This is all running on a local machine, no server involved.
I am trying to minimize the interaction the end user has to make to get the file encoded: I don't want them to have to put the image into the database, and then also have to drop (or file-chooser) the image again in the web viewer. I want to keep all of this code inside the FileMaker database to make it much more portable and robust, i.e. not have to rely on an internet connection. So, the user puts their file into the database, which automatically detects that event, it calculates the JavaScript (including the path to the file), and the JavaScript runs the Base64 function on it, returning the encoded string to the database.
Perhaps I should do it the other way around: have the user drop the file into a JavaScript area that then copies it into the database. Not sure that the JavaScript would have any handles for interacting with the database, though.
------------------------ EDIT (in addition to some tagging and flagging of original question)
Let's ignore the whole FileMaker side of things for now. I decided to go the route of just using HTML5 and JavaScript as the file input portion (instead of trying to read it in FM and then output to JS). Just to see if I can get the JavaScript part to work.
Here is some code that I am playing with right now. It is mostly from the HTML5 Rocks demo, as well as the base64 encoding routine I found. However, I am running into problems with exactly how to define and call my reader, the onload event, and the encoding function. Any suggestions would be appreciated:
<script>
// From: http://www.html5rocks.com/en/tutorials/file/dndfiles/
// JC update: changing the handleFileSelect() function to do the base64 Processing
function base64Encode(aFile) {
/*
* base64.js - Base64 encoding and decoding functions
* See: http://developer.mozilla.org/en/docs/DOM:window.btoa
* http://developer.mozilla.org/en/docs/DOM:window.atob
* Copyright (c) 2007, David Lindquist <david.lindquist#gmail.com>
* Released under the MIT license
*
* JC, update: Removed the 'atob' section of code; only need ENcoding, not DEcoding.
*/
if (typeof btoa == 'undefined') {
function btoa(str) {
var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
var encoded = [];
var c = 0;
while (c < str.length) {
var b0 = str.charCodeAt(c++);
var b1 = str.charCodeAt(c++);
var b2 = str.charCodeAt(c++);
var buf = (b0 << 16) + ((b1 || 0) << 8) + (b2 || 0);
var i0 = (buf & (63 << 18)) >> 18;
var i1 = (buf & (63 << 12)) >> 12;
var i2 = isNaN(b1) ? 64 : (buf & (63 << 6)) >> 6;
var i3 = isNaN(b2) ? 64 : (buf & 63);
encoded[encoded.length] = chars.charAt(i0);
encoded[encoded.length] = chars.charAt(i1);
encoded[encoded.length] = chars.charAt(i2);
encoded[encoded.length] = chars.charAt(i3);
}
return encoded.join('');
}
}
}
function handleFileSelect(evt) {
evt.stopPropagation();
evt.preventDefault();
var files = evt.dataTransfer.files; // FileList object - a FileList of File objects.
var fReader = new FileReader () ;
var output = [];
for (var i = 0, f; f = files[i]; i++) {
if ( !f.type.match('image.*')) { continue; } //To skip non-image files
fReader.onLoad = (function (aFile) { return base64Encode(aFile); } ) (f);
output.push('<li><strong>', escape(f.name), '</strong> (', f.type || 'n/a', ') - ',
f.size, ' bytes, last modified: ',
f.lastModifiedDate ? f.lastModifiedDate.toLocaleDateString() : 'n/a',
'<br><br>' , fReader.readAsBinaryString(f) , '<br><br>', '</li>');
//This defines the 'onLoad' behavior/function...I think.
}
document.getElementById('list').innerHTML = '<ul>' + output.join('') + '</ul>';
}
function handleDragOver(evt) {
evt.stopPropagation();
evt.preventDefault();
evt.dataTransfer.dropEffect = 'copy'; // Explicitly show this is a copy.
}
// Setup the dnd listeners. [Slightly modified by JC]
var dropZone = document.getElementById('drop_zone');
dropZone.addEventListener('dragover', handleDragOver, false);
dropZone.addEventListener('drop', handleFileSelect, false);
</script>
No, in web browsers you can't access arbitrary files on user's computer. Never ever.
You can get files from the network (but read contents only if they are from the same origin or allowed via CORS, and file:// protocol doesn't allow it), IndexedDB database and Chrome's sandboxed filesystem (which is not user's filesystem — it only contains files you've put there yourself).
And you can create "files" yourself:
var file = new Blob(["file content"], {type:"text/plain"})
Blob is the base class of File and it's generally usable everywhere where input.files[] is.
You may have privileges to access local files from other JS-based environments like Widgets or browser extensions.

Is there a way to automatically detect the excel version from a html file or active x?

I have a workflow process that runs in weird combination of html and excel, this was already done when I came to work here so I'm no able to restart the process, well here is the deal:
After a certain step on the flow, a html file is sent through email, this file contains an active x that will automatically open excel with some given parameters, however even though everything seems to build with parameters the path for excel is hardcoded, we never had an issue with this as everyone was using Excel 2003 and pretty much everyone had the same image so the path was the same, but now some users (top management) will be migrating to office 2010. Therefore I need a way to automatically detect the excel version through the active x so the html file will open the correct excel version.
Any ideas?
Thanks!
EDIT
this is what I use:
<script language="JavaScript">
function LoadVendor()
{
var vendor = document.getElementById("vendor").value;
document.getElementById("vend").innerHTML=vendor;
var BIDnum = document.getElementById("BIDnumber").value;
document.getElementById("BIDnum").innerHTML=BIDnum;
}
function openExcelDocPath()
{
var shell = new ActiveXObject("WScript.Shell");
var excelexe = document.getElementById("excelexe").value;
var excelprg = document.getElementById("excelprg").value;
var vendor = document.getElementById("vendor").value;
document.getElementById("vend").innerHTML=vendor;
shell.run( "\"" + excelexe + "\"" + excelprg , 1, false );
try{objExcel.comaddins("SAS.OIBootStrap.Connect").connect = "False"; }
catch(err)
{ }
}
</script>
I'm not 100% on what your after but you could drop the physical path entirely;
var Excel = new ActiveXObject("excel.application");
Excel.Visible = true;
Excel.Workbooks.Add(); //to keep resident if you want interactivity
//can open something here ...
//or just get the path/version
alert( Excel.version );
alert( Excel.path );
//to kill
//Excel.Quit();
//Excel = null;

WIX: Where and how should my CustomAction create and read a temporary file?

I have a script CustomAction (Yes, I know all about the opinions that say don't use script CustomActions. I have a different opinion.)
I'd like to run a command, and capture the output. I can do this using the WScript.Shell COM object, then invoking shell.Exec(). But, this flashes a visible console window for the executed command.
To avoid that, I understand I can use the shell.Run() call, and specify "hidden" for the window appearance. But .Run() doesn't give me access to the StdOut of the executed process, so that means I'd need to create a temporary file and redirect the exe output to the temp file, then later read that temp file in script.
Some questions:
is this gonna work?
How do I generate a name for the temporary file? In .NET I could use a static method in the System.IO namespace, but I am using script here. I need to insure that the use has RW access, and also that no anti-virus program is going to puke on this.
Better ideas? I am trying very hard to avoid C/C++.
I could avoid all this if there were a way to query websites in IIS7 from script, without resorting to the IIS6 Compatibility pack, without using .NET (Microsoft.Web.Administration.ServerManager), and without execing a process (appcmd list sites).
I already asked a separate question on that topic; any suggestions on that would also be appreciated.
Answering my own question...
yes, this is going to work.
Use the Scripting.FileSystemObject thing within Javascript. There's a GetTempName() method that produces a file name suitable for temporary use, and a GetSpecialFolder() method that gets the location of the temp folder. There's even a BuildPath() method to combine them.
so far I don't have any better ideas.
Here's the code I used:
function GetWebSites_IIS7_B()
{
var ParseOneLine = function(oneLine) {
...regex parsing of output...
};
LogMessage("GetWebSites_IIS7_B() ENTER");
var shell = new ActiveXObject("WScript.Shell");
var fso = new ActiveXObject("Scripting.FileSystemObject");
var tmpdir = fso.GetSpecialFolder(SpecialFolders.TemporaryFolder);
var tmpFileName = fso.BuildPath(tmpdir, fso.GetTempName());
var windir = fso.GetSpecialFolder(SpecialFolders.WindowsFolder);
var appcmd = fso.BuildPath(windir,"system32\\inetsrv\\appcmd.exe") + " list sites";
// use cmd.exe to redirect the output
var rc = shell.Run("%comspec% /c " + appcmd + "> " + tmpFileName, WindowStyle.Hidden, true);
// WindowStyle.Hidden == 0
var ts = fso.OpenTextFile(tmpFileName, OpenMode.ForReading);
var sites = [];
// Read from the file and parse the results.
while (!ts.AtEndOfStream) {
var oneLine = ts.ReadLine();
var line = ParseOneLine(oneLine);
LogMessage(" site: " + line.name);
sites.push(line);
}
ts.Close();
fso.DeleteFile(tmpFileName);
return sites;
}

Categories

Resources