draco66 Posted March 31, 2017 Posted March 31, 2017 Good Morning, I have a template that creates variable page count PDF files. The data contains a PDF file name that we have stored in a library. The template gets that PDF file and composes it with some bar codes and other items added to the first and last page of the document. The template has a first page, an overflow page, and a last page. So if the data pulls in a 10 page PDF the composed output is a first page, 8 overflow pages, and a last page. This all works fine unless the PDF being pulled in only has 2 pages. In that case the composed file has a first page that has two copies of page one on top of each other slightly offset, and then the last page looks normal. Here is the code, which I did not create so I am not sure how to fix it: { FusionPro.Composition.SetBodyPageUsage("First", true) FusionPro.Composition.SetBodyPageUsage("Last", true) var path = "c:/SpecBen/staticPDF/"; var fileName = Field("Product Name") + ".pdf"; var file = CreateResource(path + fileName,'graphic'); var pages = file.countPages; var result = ""; for (var i = 1; i<=(pages-1); i++){ result += '<graphic file="' + file.name + '" pagenumber=' + i + ' position="afterline">'; result += '<P>'; } result = result.replace(/.{3}$/,''); FindTextFrame('graphic').content = result; FindTextFrame('graphic_last').content = '<graphic file="' + file.name + '" pagenumber=' + pages + ' position="afterline">'; } Quote
Dan Korn Posted April 3, 2017 Posted April 3, 2017 This is almost impossible to analyze from just the rule. You really should collect up the job, along with one of the PDF resources, and post it here. Quote
draco66 Posted April 4, 2017 Author Posted April 4, 2017 Dan, Collected job posted here. The test17.pdf file shows what I am getting when I run this job. The first page is an address sheet and comes out fine. The second page you will see is a double image of page 1 of a PDF requested from the library. Page three is the second page of the library pdf which is composed correctly. Pages 4-7 are pages of a second pdf requested from the library all of which compose correctly. This only happens when the PDF requested from the library has 2 pages. If the pdf in the library has 4, 10, 20, any number above 2 pages, it composes correctly.SpecBen.zip Quote
step Posted April 4, 2017 Posted April 4, 2017 (edited) Have you tried removing the position="afterline" attribute? I don't think it's necessary. { FusionPro.Composition.SetBodyPageUsage("First", true) FusionPro.Composition.SetBodyPageUsage("Last", true) var path = "c:/SpecBen/staticPDF/"; var fileName = Field("Product Name") + ".pdf"; var file = CreateResource(path + fileName,'graphic'); var pages = file.countPages; var result = ""; for (var i = 1; i<=(pages-1); i++){ result += '<graphic file="' + file.name + '" pagenumber=' + i + ' [color="Red"]position="afterline"[/color]>'; result += '<P>'; } result = result.replace(/.{3}$/,''); FindTextFrame('graphic').content = result; FindTextFrame('graphic_last').content = '<graphic file="' + file.name + '" pagenumber=' + pages + '[color="red"] position="afterline"[/color]>'; } By the way, the code in your template is quite repetitive and could be made more concise if you want: var firstPage = 'First'; var lastPage = 'Last'; var firstGraphic = 'graphic'; var lastGraphic = 'graphic_last'; var isUpload = /Upload/.test(Field("Product Name")); var path = "c:/SpecBen/" + (isUpload ? "uploadPDF" : "staticPDF") + "/"; var fileName = (isUpload ? Field("Order Number") + "-" + Field("SKU") : Field("Product Name")) + ".pdf"; var result = []; if (Field("Product Name") == "Coverage Change Form") { firstPage += 'L'; lastPage += 'L'; firstGraphic += '2'; lastGraphic += '2'; } //Print an address page at beginning of each order if (FieldChanged("Order Number")) FusionPro.Composition.SetBodyPageUsage("address", 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; Edited April 4, 2017 by step Quote
Dan Korn Posted April 4, 2017 Posted April 4, 2017 (edited) EDIT: Ah, Step beat me to it! Anyway, we came up with basically the same solution. Although you really don't need those "Last" pages. Dan, Collected job posted here. The test17.pdf file shows what I am getting when I run this job. The first page is an address sheet and comes out fine. The second page you will see is a double image of page 1 of a PDF requested from the library. Page three is the second page of the library pdf which is composed correctly. Pages 4-7 are pages of a second pdf requested from the library all of which compose correctly. This only happens when the PDF requested from the library has 2 pages. If the pdf in the library has 4, 10, 20, any number above 2 pages, it composes correctly. Okay, thanks for attaching the template. It seems to me that you're making this more complicated than it needs to be. You don't need a separate "Last" page; you can simply use the "First" Body Page and the corresponding Overflow Page for all the pages of each PDF resource. (In order to put a line break between each page, but not an extra one at the end, you can add each graphic tag to an array and then join them with the <p> or <br> tag.) Plus, the code is quite repetitive. I've factored the OnRecordStart rule down to this: //Print an address page at beginning of each order FusionPro.Composition.SetBodyPageUsage("address", FieldChanged("Order Number")) //Print letter size static pdf files in the order //Check to see if item is upload file and if so, image it // 8.5 x 11 static pdf items var pageName = "First"; var frameName = "graphic"; var resourcePath = "staticPDF/" + Field("Product Name") + ".pdf"; if (Field("Product Name").match(/(Upload)/)) { resourcePath = "uploadPDF/" + Field("Order Number") + "-" + Field("SKU") + ".pdf"; } else if (Field("Product Name") == "Coverage Change Form") { // 8.5 x 14 static pdf items pageName = "FirstL"; frameName = "graphic2"; } FusionPro.Composition.SetBodyPageUsage(pageName, true) var file = CreateResource(resourcePath, 'graphic'); var pages = file.countPages; var result = []; for (var i = 1; i <= pages; i++) result.push('<graphic file="' + file.name + '" pagenumber=' + i + '>'); FindTextFrame(frameName).content = result.join("<p>"); var msg = 'File "' + FusionPro.ResolveFile(resourcePath) + '", ' + pages + ' pages'; Print(msg); return msg; Note that I've also removed the hard-coded absolute paths; paths relative to the template or the input file will work fine. I've also added some logging. Finally, in the attached template, I've removed the unnecessary pages, and I've turned on the "Create bookmark for each record" setting, which makes it easier to see which output pages come from which record.Ben_addressSheet_template__Dan-2.pdf Edited April 4, 2017 by Dan Korn Quote
draco66 Posted April 4, 2017 Author Posted April 4, 2017 step, thank you for taking a look at this. I have changed the code to your revised-streamlined version and it works perfectly. Thanks again. Quote
Recommended Posts
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.