Copy link to clipboard
Copied
Hello all,
I'm lost and have been struggling with this for a couple weeks now. I am a decent beginner level who can understand js, copy it and past, then modify to get it working. I am much more limited with regards to creating new scripts from scratch.
I have a js 'Action' that performs a batch import: locates txt file, imports data for first record, saves the file with one of the field names, then automatically, pulls the next record and repeats.
I'm using Acrobat Pro XI.
The problem is that while it populates the data, it will not save the new file to my folder, and thus, will not proceed to the next file.
I have performed the research and have traced the problem to the security issues associated with saving files on my company's drives. I believe this is a Trust function and Privilege scripting. I don't quite seem to understand
1) how to create a .js file that I save in the javacript folder,
2) the javascript folder location is different from where I have seen in blogs that it is supposed to be (it is here: C:\Program Files (x86)\Adobe\Acrobat 11.0\Acrobat\Javascripts), and
3) the difference between writing the trust script into the Action js and simply creating a js to go and retrieve the trusted script from the js file.
I have tried all sorts of variations to no avail. Any assistance would be really appreciated.
Current script:
/* Autofill OPPE Records */
// specify the filename of the data file
var fileName = "/c/Users/XXXXX/Desktop/Temp Files/NeuroTestDatafile.txt";
var outputDir = "/c/Users/XXXXX/Desktop/Temp Files/OPPEAutoPopTest/";
var err = 0;
var idx = 0;
while (err == 0){
err = this.importTextData(fileName, idx); // imports the next record
if (err == -1)
app.alert("Error: Cannot Open File");
else if (err == -2)
app.alert("Error: Cannot Load Data");
else if (err == 1)
app.alert("Warning: Missing Data");
else if (err == 2)
app.alert("Warning: User Cancelled Row Select");
else if (err == 3)
app.alert("Warning: Missing Data");
else if (err == 0){
this.saveAs(outputDir + this.getField("Provider").value + ".pdf")
}
}
Karl,
this.saveAs(outputDir + this.getField("Text1").value + "_" + this.getField("Text2").value + ".pdf"); // saves the file
idx++;
This is from your blog that I gathered the script from. It looks like the idx++; is missing after the ".pdf"); which it is not but it doesn't show the 'enter' that separates it from the 'saves the file.
This is my new script and it is important to point out a couple things that us novices need to be careful of. 1) that the this.resetForm needs to go after the idx++
...Copy link to clipboard
Copied
You can use an action in the Action Wizard for this.
Copy link to clipboard
Copied
Thank you for responding, but that is what I'm doing. The action requires a script and I am trying to write the script per above.
Copy link to clipboard
Copied
Any error message in the JavaScript console?
Copy link to clipboard
Copied
I appreciate your attempt at assisting, but I'm past all the errors, I know what I need. Can you assist in providing the specific script edits required to get past saving to a highly secured network?
Copy link to clipboard
Copied
Did it work on a local disc?
Copy link to clipboard
Copied
You're not explaining what the issue is... In your code you're not saving to a network drive, but to a local one.
So, is there an error message when you run the code above? If so, what does it say? Also, from where are you running this code?
Copy link to clipboard
Copied
One of your error cases is wrong, this is the correct one:
else if (err == 1)
app.alert("Warning: User Cancelled File Select");
This does however not explain why your save operating never gets called or never finishes. The usual case is that you run into an error when importing the record, and that should display an error message (via the "app.alert()" method). In case something else goes wrong, you should see an error written to the Javascript console. I've never seen a case where no error was produced, but the file was also not saved. Unless you are running into something completely new or different, there should be an error reported somewhere. That's why we are asking for what's going on in the JavaScript console.
If there truly is no error message anywhere, you need to debug the code by either stepping though it line by line, or by adding console.print() statements to see how far your code actually gets.
Copy link to clipboard
Copied
Hey Karl, this is actually your script. I've communicated with you several times on your consulting website, but after reviewing 2-3 years worth of comments, I came up with it being the security issues with saving to a Privilege folder with Trust Functions. I don't quite understand how to do this though.
Copy link to clipboard
Copied
Yes, I recognized the comments
You are probably correct that there is a security restriction that does not allow you to write to that directory, but there is nothing in Acrobat that would help you to get around that restriction. The script works. I am using it (or a variation of it) on a regular basis. So, in order to rule out any other problem, I would try a couple of things: Can you save files to your desktop without problems?
The second test is this: Can you save a PDF file to that folder using File>Save As?
The third thing I would try is to output the filename that is going to be used to see if it contains any special characters, or is too long for your server file system. You can do that by adding the following line just before the one with saveAs():
app.alert(outputDir + this.getField("Provider").value + ".pdf");
To rule out any potential effect of special characters, you could actually replace the file name with a static name for test purposes:
this.saveAs(outputDir + "output.pdf");
Copy link to clipboard
Copied
OMG! I've made some headway! After testing out what Karl suggested, I discovered what the first problem was. 1) My this.getField is the Provider name field. When I looked at the txt file, it surrounds ONLY the Provider name (not the other fields) with quotes, e.g. "Last,First M". This is the File path issue.
So, I was able to get it to start processing through all the files. Next problem. 2) As I watched the save to folder populate, it would create the first file based upon the field name, then I'd see the tmp pdf file churn as if it was opening the txt file again, but then it would only extract the first record and save as the same file over and over again. So, it would just keep saving as the same file name and not the successive records.
I fixed item 1. Now, what is wrong with the script that keeps it from advancing to the next record? How does the script know that it should look at row 2, row 3, etc.?
/* Autofill OPPE Records */
// specify the filename of the data file
var fileName = "/c/Users/XXX/Desktop/Temp Files/NeuroTestDatafile.txt";
var outputDir = "/c/Users/XXX/Desktop/Temp Files/OPPEAutoPopTest/";
var err = 0;
var idx = 0;
while (err == 0){
err = this.importTextData(fileName, idx); // imports the next record
if (err == -1)
app.alert("Error: Cannot Open File");
else if (err == -2)
app.alert("Error: Cannot Load Data");
else if (err == 1)
app.alert("Warning: Missing Data");
else if (err == 2)
app.alert("Warning: User Cancelled Row Select");
else if (err == 3)
app.alert("Warning: Missing Data");
else if (err == 0){
this.saveAs(outputDir + this.getField("Provider").value + ".pdf")
}
}
Copy link to clipboard
Copied
There is a line missing after your call to saveAs() (see my original article with the code here: Batch-Import Excel Data into PDF Forms - KHKonsulting LLC​):
idx++;
This is how the counter gets incremented, and the next time around, the second, third and so on line will be processed.
Copy link to clipboard
Copied
Karl,
this.saveAs(outputDir + this.getField("Text1").value + "_" + this.getField("Text2").value + ".pdf"); // saves the file
idx++;
This is from your blog that I gathered the script from. It looks like the idx++; is missing after the ".pdf"); which it is not but it doesn't show the 'enter' that separates it from the 'saves the file.
This is my new script and it is important to point out a couple things that us novices need to be careful of. 1) that the this.resetForm needs to go after the idx++; in order for it to populate the form first. I tried it in 3 separate locations and it broke the js.
2) when copying and pasting your script, the idx++; actually flows with the "saves the file" unless there is an enter after "file".
This script just saved my office about 150 hrs per year. Thank you so much, Karl. (I'll post this on your consulting blog too.)
Thank you too, Try67; you've been a significant help in the past with my js education.
Here is the full code.
/* Autofill OPPE Records */
// specify the filename of the data file
var fileName = "/c/Users/XXX/Desktop/Temp Files/NeuroTestDatafile.txt";
var outputDir = "/c/Users/XXX/Desktop/Temp Files/OPPEAutoPopTest/";
var err = 0;
var idx = 0;
while (err == 0){
err = this.importTextData(fileName, idx); // imports the next record
if (err == -1)
app.alert("Error: Cannot Open File");
else if (err == -2)
app.alert("Error: Cannot Load Data");
else if (err == 1)
app.alert("Warning: User Cancelled File Select");
else if (err == 2)
app.alert("Warning: User Cancelled Row Select");
else if (err == 3)
app.alert("Warning: Missing Data");
else if (err == 0){
this.saveAs(outputDir + this.getField("Provider").value + ".pdf");idx++; // saves the file idx++;
this.resetForm();
}
}
Copy link to clipboard
Copied
That's odd, I just copied and pasted the code from my site, and I did get the return between the saveAs() and the idx++ - it may depend on what version of Acrobat you use, or even on what operating system. I've tested with Acrobat DC on Mac OS. Glad that I could help to save some time and money.
Copy link to clipboard
Copied
So, although I'm saving to a local drive, it is still heavily securitized, and it does not save to any of my network drives either, as I've tried several different scenarios. The script states that it runs successfully under the action, but does have errors in the Console. The current script is being run through the Action Tab in which I created a new action. This is from the 'FULL REPORT' Link:
[-]TestOPPEForms2.pdf(Succeeded)
Command Name: Execute JavaScript
Command Start Time: 2017-06-19 12:44:02
Command Status: Succeeded
Command Finish Time: 2017-06-19 12:44:03
This is the error from the Debugger Console:
ReferenceError: Percent is not defined
1:Field:Validate
ReferenceError: Percent is not defined
1:Field:Validate
UnsupportedValueError: Value is unsupported. ===> Parameter cPath.
Doc.saveAs:26:Batch undefined:Exec
I know my path is correct.
Copy link to clipboard
Copied
Monteigo99 wrote
I know my path is correct.
Why are you so sure of that? Did you convert it to the platform-independent format used by the PDF standard?
Is the drive you're trying to save to mounted?
Copy link to clipboard
Copied
I did convert the windows format to the PDF required format. The save folder location is in the same location as the txt file, and the path reads the txt file for the first record, populates the form, and then says it was successful. It just won't save. Apparently, I am not asking the correct questions. How about this: I need to batch import records, autopopulate the form, save the form with the 'Provider' field in the file name, and then move to the next record and repeat for my 1000 plus files. Can anyone suggest a script that would allow me to do this?
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Hello Try67,
That looks very useful, however, I am not looking to email, nor create a single file that includes multiple individuals. I need a single file for each record in the txt file. In addition, while I am advocating for consulting resources, your product would have to get approved by our National office, and you would have to be vetted as a vendor for a contract through GSA. There are some workarounds for some adhoc consulting fees, but I've not gained fund approval for that as yet in my Federal Agency.
Copy link to clipboard
Copied
You don't have to email the generated files, and my script can create a separate file for each record, or a single file with all of them. It's up to you. Anyway, if you want to discuss it further you can contact me privately (try6767 at gmail.com).
Find more inspiration, events, and resources on the new Adobe Community
Explore Now