Jump to content

Multi-page based on the page and quantity field


ricky10ss

Recommended Posts

Hello,

 

I found a post that almost completed a project I am trying to compose. I have a two page document. The first page is the job ticket "ticket" the second page is the cover "p1". I am wanting to output two PDF's for the whole job. I attached a data file sample. You can see all records will always be the same WO#.

 

I want one PDF that is page one "ticket" duplicated for as many records are in the file. So this example would have an output file name of 847_ticket.pdf with 9 pages.

 

The second PDF would be all the covers "p1" combined to print based on the Books field value. So this would be 47 pages of "p1" named 847_Cover.pdf.

 

Below is the current code I have that is outputting page 1 "ticket" then page 2 "p1" times the Books value and so on for each record. Do you have any recommendations to help?

 

// enter number of copies needed per record

var copies = Field("Books");

 

// add one for the trailing job ticket

FusionPro.Composition.repeatRecordCount = copies;

 

//generate duplicate pages per record

if (FusionPro.Composition.repeatRecordNumber > copies) {

FusionPro.Composition.SetBodyPageUsage("p1", false);

} else if (FusionPro.Composition.repeatRecordNumber > 1) {

FusionPro.Composition.SetBodyPageUsage("ticket", false);

}

Input.txt

Link to comment
Share on other sites

I want one PDF that is page one "ticket" duplicated for as many records are in the file. So this example would have an output file name of 847_ticket.pdf with 9 pages.

 

The second PDF would be all the covers "p1" combined to print based on the Books field value. So this would be 47 pages of "p1" named 847_Cover.pdf.

That sounds doable but it's not pretty. What you essentially want to do is run through your data file 1 time to create the "tickets" PDF and then run through it again to create the "covers" PDF. The cleanest approach to this would be to have two separate templates that you run your data file against. With FP Server you could even run them concurrently. That being said, there is another way – I'll get to that in a minute.

Below is the current code I have that is outputting page 1 "ticket" then page 2 "p1" times the Books value and so on for each record. Do you have any recommendations to help?

// enter number of copies needed per record
var copies = Field("Books");

// add one for the trailing job ticket
FusionPro.Composition.repeatRecordCount = copies;

//generate duplicate pages per record
if (FusionPro.Composition.repeatRecordNumber > copies) {
   FusionPro.Composition.SetBodyPageUsage("p1", false);
} else if (FusionPro.Composition.repeatRecordNumber > 1) {
   FusionPro.Composition.SetBodyPageUsage("ticket", false);
}

Your code is basically repeating each record by the number in the "Books" field, that part I understood from your original post (though, it didn't sound like you wanted to repeat the records for the "ticket" page) but your if statement confuses me.

 

Anyway, the only way I can think to accomplish this is by unlinking your data file from your template and then reading from it in OnJobStart as an external data file. Doing so will allow you to specify to FP how many records you want to have (since you want to run it two times, you want to double the amount of records).

data = new ExternalDataFileEx('/path/to/file/Input.txt', '\t');
recs = data.recordCount;

FusionPro.Composition.composeAllRecords = false;
FusionPro.Composition.endRecordNumber = (recs*2);

Make sure both of your body pages ('p1' and 'ticket') are set to Unused. Those will be turned on in the OnRecordStart Callback rule:

if (FusionPro.inValidation) { Rule("OnJobStart") }
var curr = FusionPro.Composition.inputRecordNumber

//===================================================
// Function to return external data field 
// values as they relate to this job
//===================================================
   function ExField(s){
       return data.GetFieldValue((curr%recs || recs), s);
   }

//===================================================
// For loop populates variable values in use
// elsewhere in the template
// *Note: Field() syntax will not work. Use ExField()*
//===================================================
   for (var i=0; i<data.fieldCount; i++){
       var fieldName = data.GetFieldValue(0, i);
       FusionPro.Composition.AddVariable(fieldName, ExField(fieldName));
   }

//===================================================
// Settings for ticket & cover pages
//===================================================
   var isCover = (curr>recs);
   var template = {
       outputName: ExField("WO#") + ["_ticket","_Cover"][+isCover],
       page: ["ticket","p1"][+isCover],
       copies: [1,Int(ExField("Books"))][+isCover]
   }

//===================================================
// Page control & output
//===================================================
   FusionPro.Composition.SetBodyPageUsage(template.page, true);
   FusionPro.Composition.repeatRecordCount = template.copies;

   if (!((curr%recs)-1)){
       FusionPro.Composition.OpenNewOutputFile(template.outputName + "." + FusionPro.Composition.outputFormatExtension)
   }

As I mentioned in the code, if you have rules in your templates that call fields using the 'Field' function (ex: 'Field("Books")' ), you might want to move that 'ExField' function into the JavaScript globals so that you can use that instead. If you have text frames that reference variables, they won't need to be altered since I added all of them back in that 'for' loop.

 

Hopefully I understood your problem well enough to help you get started.

Link to comment
Share on other sites

You'd have to collect your template (FusionPro > Collect...) in order for me to see what's going on in your template. Just supplying the PDF only shows me the PDF and FP frames – no rules.

 

That being said, I noticed a mistake in the code I posted yesterday. The code was set to open a new output file new when the record number is equal to 1 or 10 (the first records in the files: 1-9, 10-18) but since record 10 is set to repeat 3 times, It created the "_Covers" PDF 3 times – overwriting it's content each time before moving on to the next record.

 

One way around that is to add an additional condition to the 'if' statement that checks to make sure the records are on their first repeat:

if (!((curr%recs)-1) [color="Red"]&& FusionPro.Composition.repeatRecordNumber == 1[/color]){
       FusionPro.Composition.OpenNewOutputFile(template.outputName + "." + FusionPro.Composition.outputFormatExtension)
   }

Link to comment
Share on other sites

Question for you Ste. So this works now with the adjustment. But if there is only one record then the rule is not outputting a separate cover and ticket pdf. I just get a file named whatever I call the output file with the cover and ticket all in one pdf. Any idea of a fix for when there is just one record?
Link to comment
Share on other sites

I suppose only one record is boggling the modulus operation in the if statement for the output file.

 

I just moved everything into a small output control function and modified that if statement with this:

//===================================================
// Output Control
//===================================================
   function newOutput(recordNumber) {
       if (FusionPro.Composition.repeatRecordNumber == 1){
           return ([1,(recs+1)].indexOf(recordNumber) > -1);
       }
       return false;
   }

   if (newOutput(curr)){
       FusionPro.Composition.OpenNewOutputFile(template.outputName + "." + FusionPro.Composition.outputFormatExtension)
   }

And that should handle files with only 1 record.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...