Skip to main content
jasotastic81
Participating Frequently
March 2, 2016
Answered

files increase in size as JScript saveAs loops through

  • March 2, 2016
  • 3 replies
  • 1921 views

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.

This topic has been closed for replies.
Correct answer try67

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.


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.

3 replies

jasotastic81
Participating Frequently
March 2, 2016

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.

jasotastic81
Participating Frequently
March 3, 2016

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

Legend
March 3, 2016

Acrobat is designed for very light duty automation; you are pushing it hard. I recommend quitting Acrobat and restarting every 50 files.

Inspiring
March 2, 2016

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.

Legend
March 2, 2016

What save method do you use?

Legend
March 2, 2016

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.

jasotastic81
Participating Frequently
March 2, 2016

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.