Jump to content

Recommended Posts

Posted

I have a template that basically pulls files from a library or used an uploaded file to create a new document. The code below is used to create the new document from a selection of static and uploaded files. It works fine.

 

What I would like to add to the code is a way to limit the number of pages that are added to the new document for any given file. For example, let say that someone uploads a document with 40 pages in it. We only want to allow up to 12 pages of any document to be included in the new composed file. So we want to allow the first 12 pages of any PDF and then disregard any pages after that.

 

The second thing that would be beneficial - if a file we are adding to the composed document has an odd number of pages, add a blank to make it even. That way the next document we add will not start on the back page of the previous document.

 

I hope that makes sense. It was harder describe than I thought.

 

Here is the code:

 

var firstPage = 'First';
var lastPage = 'Last';
var firstGraphic = 'graphic';
var lastGraphic = 'graphic_last';

var isUpload = /UPD-/.test(Field("SKU"));
var path = "c:/library/" + (isUpload ? "uploadPDF" : "staticPDF") + "/";
var fileName = (isUpload ? Field("Order Number") + "_" + Field("SKU") : Field("Product Name")) + ".pdf";
var result = [];

if(Field("SKU").match(/(_14_)/))
{
 firstPage += 'L';
 lastPage += 'L';
 firstGraphic += '2';
 lastGraphic += '2';
}

//Print an address page at beginning of each order
if (FieldChanged("Order Number"))
{
 if (Field("Cost Center") == "102" || Field("Cost Center") == "103" || Field("Cost Center") == "104")
 FusionPro.Composition.SetBodyPageUsage("address1", true);
}
{
 if (Field("Cost Center") == "105")
 FusionPro.Composition.SetBodyPageUsage("address2", true);
}
{
 if (Field("Cost Center") == "106" || Field("Cost Center") == "107" || Field("Cost Center") == "108")
 FusionPro.Composition.SetBodyPageUsage("address3", true);
}
FusionPro.Composition.SetBodyPageUsage(firstPage, true);
FusionPro.Composition.SetBodyPageUsage(lastPage, true);
var file = CreateResource(path + fileName, 'graphic');
var pages = file.countPages;

for (var i = 1; i < pages; i++)
 (file.pagenumber = i) && result.push(file.content);

file.pagenumber = pages;
FindTextFrame(firstGraphic).content = result.join('<p>');
FindTextFrame(lastGraphic).content = file.content;

Posted

What I would like to add to the code is a way to limit the number of pages that are added to the new document for any given file. For example, let say that someone uploads a document with 40 pages in it. We only want to allow up to 12 pages of any document to be included in the new composed file. So we want to allow the first 12 pages of any PDF and then disregard any pages after that.

You can limit the number of pages to 12 like this:

var pages = Math.min(file.countPages, 12);

That will set the 'pages' variable to the lower of the two values.

The second thing that would be beneficial - if a file we are adding to the composed document has an odd number of pages, add a blank to make it even. That way the next document we add will not start on the back page of the previous document.

Assuming you're using overflow pages, you can specify that you want the last added page to be even. But judging by your code, it looks like you may want the last page in the PDF to print in a different text frame (lastGraphic)? So, I'm also going to assume that if you have an odd number of pages, you want the content of 'lastGraphic' to be blank rather than containing the last page?

var file = CreateResource(path + fileName, 'graphic');
var pages = [color="red"]Math.min(file.countPages, 12)[/color];

for (var i = 1; i [color="Red"]<=[/color] pages; i++)
 (file.pagenumber = i) && result.push(file.content);

[color="red"]var lastPage = result.length % 2 ? '' : result.pop();[/color]
FindTextFrame(firstGraphic).content = result.join('<p>');
FindTextFrame(lastGraphic).content = [color="red"]lastPage[/color];

Posted

Thank you for your input on this one. I will look this over and give it a try in the next couple of days.

 

I'll let you know how it works out.

Posted

Today I tested just the odd page part of the code as we are ready to implement that but not the limited page count just yet.

 

I updated my code as you had suggested and it does make the page count even, but it does so by eliminating the odd page from the file.

 

For my test I used a 5 page document. The composed file contained only 4 pages of that document. It dropped page 5 to make it even rather than adding a blank to the end.

Posted
Can you share your updated code? It sounds like you didn't change the < to a <= in the for loop. But it would be easier to determine what's going on if I could see what you're using.
Posted

You are 100% correct. I missed <=. I changed it and it is working fine.

 

I shouldn't work on code before my third cup of coffee.

 

Sorry about that.

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