Jump to content

Output to different files

Recommended Posts

I produce various documents in FusionPro with addresses.


In Denmark these can be shipped cheap, if they are sorted to 3 different distributioncenters.

Based on the zipcode I know which distributioncenter the address is for.


So our printdepartment needs to deliver 3 stacks. One for each center.

And I need to deliver 3 different pdf-files to the printdepartment.


I have made a script, which outputs 1 file per record and named for the different centers.


I would like to have a script which only output 3 different files. One containing each center.


Here is my current script:

// Function creates ability to pad number
function pad(n, width, z) {
 z = z || '0';
 n = n + '';
 return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;

var zipcode = Field("zip-code")

var filedata= FusionPro.inputFileName.replace(/^.*[\\\/]/, '')
var filedata= filedata.slice(0, -4)

if (zipcode >= 0 && zipcode <= 4999) {
   var fileName = "KHC-" + filedata + "#" + pad(FusionPro.Composition.inputRecordNumber, 5) + ".pdf";
if ((zipcode >= 5000 && zipcode <= 7999) || (zipcode >= 8700 && zipcode <= 8799)) {
   var fileName = "FAC-" + filedata + "#" + pad(FusionPro.Composition.inputRecordNumber, 5) + ".pdf";
if ((zipcode >= 8000 && zipcode <= 8699) || (zipcode >= 8800 && zipcode <= 9999)) {
   var fileName = "ARC-" + filedata + "#" + pad(FusionPro.Composition.inputRecordNumber, 5) + ".pdf";

FusionPro.Composition.outputFileName = fileName


The centers are:

KHC: Zipcode 0-4999

FAC: Zipcode 5000-7999 and 8700-8799

ARC: Zipcode 8000-8699 and 8800-9999


In compose I compose to one record per file.


Hopefully my script can be changed to accomodate this.

Link to comment
Share on other sites

Yes, you can accomplish what you want, as long as the data file is sorted so that the records for each distribution center are grouped together.


What you're doing now, by checking the “Output to multiple files” box and then specifying the file name in the OnNewOutputFile is called “static chunking,” where each output file (chunk) has to have the same fixed number of records. This is the older chunking method that you had to use prior to FusionPro VDP 8.0.


FusionPro VDP 8.0 introduced a new feature called “dynamic chunking,” where each output file (chunk) can have an arbitrary number of records. Instead of specifying a fixed number of records per output file/chunk in the GUI, you call FusionPro.Composition.OpenNewOutputFile, optionally with the file name, in the OnRecordStart rule, when you want to begin a new output file. There's also a convenience function FieldChanged that you can call to trigger a new output file when a certain field value changes between records (although in your case, you don't want to open a new file every time the field value changes, only when it enters a new range).


So, what you need to do is uncheck the “Output to multiple files” box, delete the OnNewOutputFile rule, and add some logic like this to OnRecordStart:

if (FieldChanged("zip-code"))
   var zip_new = Int(Field("zip-code"));
   var zip_old = Int(FusionPro.PreviousRecordFields["zip-code"]);
   if ((zip_old < 5000 && zip_new >= 5000) || (zip_old < 8000 && zip_new >= 8000))
       var prefix = "KHC";
       if (zip_new >= 5000)
           prefix = "FAC";
       if (zip_new >= 8000)
           prefix = "ARC";

       var filename = prefix + '-' + GetFileName(FusionPro.inputFileName).replace(/\..+$/,'');
       filename += '#' + FormatNumber("00000", FusionPro.Composition.inputRecordNumber);
       filename += '.' + (FusionPro.Composition.outputFormatExtension || "pdf");

Also, if you're also using stacked imposition, there's another feature related to dynamic chunking called "dynamic stacking", where each imposed stack can have an arbitrary number of records. You could invoke that by calling FusionPro.Composition.StartNewStack() above. However, by default, output file breaks are deferred until a new stack is started, but in your case, I think you want to have each output file invoke a new stack. You can do that by adding this line to the OnJobStart (NOT OnRecordStart) rule:

FusionPro.Composition.chunksBreakStacks = true;

The other caveat here, besides the one that your data must be sorted to group the records for each output file (and the one that I'm kind of coding in the blind without being able to test the rules with your actual job), is that, while output file chunking (either the static or dynamic method) works fine in FusionPro VDP, in both Creator/Desktop and Server, it's not supported in MarcomCentral.

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