Jump to content

Compose pages as separate Output Files


Recommended Posts

I have a 4 page document (2 pages duplex) and I am trying to control the paper type of each. The first two pages print on letterhead, and the second two on plain paper.


I have tried to use the Finish Settings for each, but I can't find a solution that will work for our business. So, what I have been doing is marking pages 3 & 4 as Unused and compose the first sheet, then change the Page Usage and compose the second sheet.


I would like to write a rule to have page numbers 1 & 2 to output as one document and page numbers 3 & 4 as a separate document. Conceptually it seems feasible, but I can't find a script that fits. Hopefully someone in the community could help.


Edit* I just realized I was in the Producer Thread. I intended to post in the Creator Thread

Edited by S_Black
Does idiocy count as a reason?
Link to comment
Share on other sites

As you know, to create two separate files containing the same records, you have to run your data twice because FP does not have the capability to write multiple files at once.


One way you can do this by duplicating your entire data file and adding a field that indicates the stock to each record. Meaning: if your data file contains 6 records, your modified data file would contain 12 records where the 7th record is just the first record again but with a different value in the "stock" field.


But, as that's still a manual step, I think the second option is better. Without altering the data file, we can tell FusionPro to import the data as an external data file which gives us visibility of all of the records at once. Then with a little trickery, we can determine the number of records in the data file, double it, and tell FP to repeat the first record by that amount and don't compose any of the other records. So in the example above, we would tell FP to repeat the first record 12 times. Then based on the iteration in the repeat, we reference the external data file to reassign the field values to give the illusion of producing more than one record. We can enable pages 1 and 2 when the iteration in the repetition is less than or equal to half (1-6) and enable pages 3 and 4 when it is greater than half (6-12).


Having said all that, I think you can get the results you're looking for if you throw this in your OnRecordStart rule:

// Only compose the first record
FusionPro.Composition.composeThisRecord = FusionPro.Composition.inputRecordNumber == 1;

// Link to your data file as an external data file
var data = new ExternalDataFileEx(PrimaryInputFile(), FusionPro.inputFileDelimiter);

// Count the records
var recs = data.recordCount;

// Double the number of records we're composing
FusionPro.Composition.repeatRecordCount = recs * 2;

// Consider the number of times record 1 has been repeated the record number.
var rec = FusionPro.Composition.repeatRecordNumber;

// Redefine the "Field" function so that calling it from other rules will 
// still return the correct field value for the current "record"
Field = function(str) {
   return data.GetFieldValue(rec % recs || recs, str);

// Define how many pages are in each output file.
var pages = 4;

// Enable pages 1 and 2 when 'rec' is < the total number of records
// Enable pages 3 and 4 when 'rec' is >= the total number of records
 FusionPro.Composition.SetBodyPageUsage(pages + 1, 
   Math.floor(pages / 2) ^ rec <= recs);

// Map the field values to the value of the new 'Field' function 
// so that text frames that call a field will have the correct value.
for (var i in FusionPro.Fields)
 FusionPro.Composition.AddVariable(i, Field(i));

// Open a new output file for each stock.
if (rec % recs == 1) {

 // Only get the output file name once (you can hard code this if you prefer)
 this.out = this.out || FusionPro.Composition.outputFileName;

 // Get the appropriate extension.
 var ext = '.' + FusionPro.Composition.outputFormatExtension;

 // Set the stock
 var stock = rec <= recs ? 'letterhead' : 'plain';

 // Open a new output file called "[OUTPUT FILE NAME]-letterhead.pdf"
 FusionPro.Composition.OpenNewOutputFile(this.out.replace(ext, '-' + stock + ext));

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.

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...