Copy link to clipboard
Copied
Situation: I'm using JavaScript to feed rows from a CSV file into a PDF and save one PDF for each row.
Problem: Subsequent files are larger in size though there is not an increase in the amount of information. For example, the first file is 123KB. The 500th file is 5.03MB. The PDF is still only 3 pages, and there is less information in the 500th PDF than the first.
What I've tried: Nothing. I haven't tried anything else because it is still in the process of creating the files as I write this forum post.
Potential solution: Is there a parameter for the saveAs function that will save the PDF as a compressed PDF?
Possibly important details: The PDF is form IRS 1095-C. It is a three page form. People who don't need the third page (it's an overflow page) can have the form with just two pages. During the process, I'm looking at the row in the CSV file and deleting the third page if it's not needed. After I save the file with two pages, I add that third page back. If three pages were necessary, then the third page is not deleted. I did notice that when the files are going through, there is a notification on the bottom of the page that it is importing/consolidating fonts. That's the only thing that I see that might be different between files. When I look at individual files, they're all 2-3 pages, and there's nothing extra that I can find.
I agree with TSN that it's much better to use the original file in each iteration, especially if otherwise you need to constantly remove and then add pages to the file, which can certainly cause it to grow in size.
So your workflow should be something like this:
- Populate the data
- Remove any unwanted pages.
- Save the file under a new name.
- Close the file
- Open the original file
- Repeat.
Copy link to clipboard
Copied
What save method do you use?
Copy link to clipboard
Copied
On reflection, I always feel there is a small chance of failure when filling a form, and that the raw original should be kept. So I suggest you open master - fill - save a copy - close. Over and over, should also avoid the problem.
Copy link to clipboard
Copied
Here's the important parts of the JS that's running for this form.
var cycleThrough = app.trustedFunction( function(){
//read file data into stream
var fileData = util.readFileIntoStream();//convert data to string
var strData = util.stringFromStream(fileData);var lines = strData.split("\n");
for (var i = 3; i<lines.length; i++){
var line = lines.split(",");//some stuff happens to fill the form...
app.beginPriv();
if (deleteThird) this.deletePages({nStart: 2, nEnd: 2});
this.saveAs("/u/1095-C/"+employeeName+".pdf");
this.insertPages({nPage:1, cPath:"/u/f1095c--2015-unextended.pdf", nStart: 2, nEnd: 2})
app.endPriv();
}
});
I did notice that there is a logic issue with that next to last line where I don't check before adding the page back. The filled and saved forms all have three pages, but that's all they have.
Copy link to clipboard
Copied
I agree with TSN that it's much better to use the original file in each iteration, especially if otherwise you need to constantly remove and then add pages to the file, which can certainly cause it to grow in size.
So your workflow should be something like this:
- Populate the data
- Remove any unwanted pages.
- Save the file under a new name.
- Close the file
- Open the original file
- Repeat.
Copy link to clipboard
Copied
OK, I didn't know I could do that. Is this what we're talking about? Or something like this?
var cycleThrough = app.trustedFunction( function(){
var fileData = util.readFileIntoStream();
var strData = util.stringFromStream(fileData);
var lines = strData.split("\n");
app.beginPriv(); //moved this line to before the loop
for (var i=3; i<lines.length; i++){
var line = lines.split(",");
app.openDoc("/u/filename.pdf"); //added this to open the document
//stuff that happens to fill in form
if (deleteThird) this.deletePages({nStart: 2, nEnd: 2});
this.saveAs("/u/1095-C/"+employeeName+".pdf");
app.closeDoc(); //added this to close the doc after the information was added
}
app.endPriv(); //moved this line outside the loop
});
Copy link to clipboard
Copied
Almost... You should be using the return value of the openDoc method as a reference to your Document object instead of the "this" keyword.
Copy link to clipboard
Copied
With Acrobat's PDF Optimizer feature you can get a report on what's taking up all the space (Audit Space Usage feature), which should give you some clues. Manually do a Save As first, and note if it makes any difference.
Copy link to clipboard
Copied
Pertinent part:
var currentFile = app.openDoc("/u/filename.pdf");
// stuff
if (deleteThird) currentFile.deletePages({nStart: 2, nEnd: 2});
currentFile.saveAs("/u/1095-C/"+employeeName+".pdf");
I'll try it after the lengthy process has completed. I don't want to mess up what we got just to test. I'll report back when I know.
Copy link to clipboard
Copied
I'm having mixed results with this. On the plus side, all of the files are relatively the same size: about 127KB. Unfortunately, Acrobat is not closing soon enough. So about 60-90 rows in, I get an error that I can't escape. I have to wait for the computer to catch up, then I click "OK" on the error and wait for all the copies of the program to close. Perhaps I'm not closing the file correctly.
app.beginPriv();
for (var i = 441; i<lines.length; i++){
var currentFile = app.openDoc("/u/f1095c--2015-unextended.pdf");
//some stuff happens here
currentFile.saveAs("/u/1095-C/"+fileName+".pdf");
currentFile.closeDoc();
}
app.endPriv();
OR maybe I need to begin and end privileges inside the loop. Any thoughts?
(Edit: added thrown error)
NotAllowedError: Security settings prevent access to this property or method. App.openDoc:11:Menu Command:Exec
Copy link to clipboard
Copied
Acrobat is designed for very light duty automation; you are pushing it hard. I recommend quitting Acrobat and restarting every 50 files.
Copy link to clipboard
Copied
Do you see the files still open? If so, you're using a version of Acrobat that is buggy and the closeDoc method doesn't work there. In that case, it will error out after 50 files, which is the maximum allowed number of files open simultaneously.
Copy link to clipboard
Copied
I forgot to set bNoSave to true... [headdesk]
863 items, no issues, everything looks right. I'll post the completed code (minus the field filling lines) for others to use.
Copy link to clipboard
Copied
Here's the complete code other than some string manipulation and inserting into fields.
var cycleThrough = app.trustedFunction( function(){
//read file data into stream
var fileData = util.readFileIntoStream();//convert data to string
var strData = util.stringFromStream(fileData);
var deleteThird = true;
var lines = strData.split("\n");
app.beginPriv();
for (var i = 3; i<lines.length; i++){
try{ var currentFile = app.openDoc({cPath:"/u/f1095c--2015-unextended.pdf",bHidden: true});} catch(e){}
currentFile.resetForm();
var line = lines.split(",");//some stuff happens
try{ if (deleteThird) currentFile.deletePages(2,2); } catch(e) {};
currentFile.saveAs("/u/1095-C/"+fileName+".pdf");
currentFile.closeDoc(true);
}
app.endPriv();
});
The last run was 873 items from a CSV of 1282 lines. It took all of 10 minutes to create the forms. Thank you all for your help.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now