Jump to content

Need to pull a graphic file based on 3 different variables...


Scott

Recommended Posts

I am creating certificates and I need to pull different background images based on 3 different variables.

 

Basically I need something like this:

 

if field(month) is "August"

AND

if field(year) is "2009"

AND

if field(degree) is "English"

 

then insert a PDF called "aug_09_english.pdf"

 

but obviously I need it to be flexible and be able to pull various other graphics such as "may_08_nursing.pdf" and "may_09_law.pdf", etc.

 

I am guessing I need to use some kind of create resource feature instead of mapping out all possible file names (would be over 100).

 

Any thoughts?

Link to comment
Share on other sites

We may have to work out some kinks, but how about something along the lines of:

// enter the path to where variable graphics are stored
var ImagePath = "server/path/folder/";
// assuming all months are spelled out in data; years are entered as 2- or 4-digit numbers; graphics follow specific naming convention
var ImageName = ToLower(Left(Field("Month",3))) + "_" + Right(Field("Year",2)) + "_" + ToLower(Field("Degree")) + ".pdf";
var CurrImage = CreateResource(ImageName, graphic, true);
return CurrImage;

Link to comment
Share on other sites

I over simplified a bit in my example to get an idea of how to approach this.

 

Thanks for your reply. I will start with that, but will need to modify a bit.

 

The months will actually be in latin, and only Augusti, Maii, and Decembris will be in that field.

 

The year will be 4 digits, so that part will be fine.

 

The Degree field is another story though. There are too many different degrees to use this field, so I will be using the Dean field. In other words, if the Dean field says "John Doe", I know this to be a English degree, if the Dean field says "Jane Smith", then I know this to be a Medical degree.

 

I am the one creating/naming the graphic files, so I can follow any convention needed.

 

Sorry to throw a curveball now, but I needed to get started at least in my head.

Link to comment
Share on other sites

The months will actually be in latin, and only Augusti, Maii, and Decembris will be in that field...I am the one creating/naming the graphic files, so I can follow any convention needed.

As long as graphic names follow same convention (aug, mai, dec) this should not be an issue.

 

I will be using the Dean field. In other words, if the Dean field says "John Doe", I know this to be a English degree, if the Dean field says "Jane Smith", then I know this to be a Medical degree.

You can insert a switch statement before declaring the ImageName variable like this:

//Edit/add case values as necessary
switch (Field("Dean")) {
  case "John Doe": 
     var CurrDegree = "english";
     break;
  case "Jane Smith": 
     var CurrDegree = "medical";
     break;
  default: 
     var CurrDegree = "";
}

Then modify the ImageName variable to:

var ImageName = ToLower(Left(Field("Month",3))) + "_" + Right(Field("Year",2)) + "_" + CurrDegree + ".pdf";

Link to comment
Share on other sites

OK, this is not quite working yet.

 

Here is my rule:

 

//Edit/add case values as necessary
switch (Field("Dean")) {
  case "ANDREW PORTER": 
     var CurrDegree = "EDU";
     break;
  case "EDUARDO D. GLANDT": 
     var CurrDegree = "ENG";
     break;
  case "REBECCA W. BUSHNELL": 
     var CurrDegree = "COL";
     break;
  case "RAYMOND J FONSECA": 
     var CurrDegree = "WHA";
     break;
  default: 
     var CurrDegree = "";
}
// enter the path to where variable graphics are stored
var ImagePath = "\\Tcgfp1\jobs\ InProgress\53373.UofP.Diplomas\PDF\Source\pics";
// assuming all months are spelled out in data; years are entered as roman numerals; graphics follow specific naming convention
var ImageName = CurrDegree + "_" + ToLower(Left(Field("Latin_Month",3))) + "_" + (Field("Latin_Year")) + ".pdf";
var CurrImage = CreateResource(ImageName, graphic, true);
return CurrImage;

I get an error that says "... line 46: ReferenceError: graphic is not defined.

 

1) can you see something wrong in the code? Line 46 is the var CurrImage line.

 

2) Do I need to add these images as resources, or just have them in the correct folder?

Link to comment
Share on other sites

var CurrImage = CreateResource(ImageName, graphic, true);

Should be:

var CurrImage = CreateResource(ImageName, "graphic", true);

Or simply:

var CurrImage = CreateResource(ImageName);

So that you get an error message if the graphic can't be found.

2) Do I need to add these images as resources, or just have them in the correct folder?
With the logic you have, you don't need to add the graphics as resources; you just need to make sure the files are in the same location as the template PDF, or in the Search Path defined on the Advanced tab of the Composition Settings dialog. (Note that even though you go through the trouble to assign a variable containing a path in your rule, you're not using it, so that has no effect. And you would need to double-escape all those bakcslashes too. It's a lot easier to just use the Search Path in the GUI.)
Link to comment
Share on other sites

Got this working guys, thanks!!!

 

 

Of course now the customer throws me a curveball.

 

The field for "Dean", contains more text than just the name. I am not quite sure what case means in the following code:

 

//Edit/add case values as necessary
switch (Field("Dean")) {
  case "John Doe": 
     var CurrDegree = "english";

Can I alter the code to look for some of the type? (with Contains)

so if the value of the field is "John Doe, Dean of blahblah" the code will still work?

Link to comment
Share on other sites

The field for "Dean", contains more text than just the name. I am not quite sure what case means in the following code:
the word case in a switch statement just denotes an if. it is a way of creating nested ifs - so to speak.

 

ex:

switch (Field("Dean")) {

case "John Doe":

var CurrDegree = "english";

could be stated like this in an if statement:

if (Field("Dean") =="John Doe")
{var CurrDegree = "english";
}

Basically, if your field contains more than the name, just change the case statement.

Link to comment
Share on other sites

I just looked at the original thread. If I am reading this right, you will have to create 100 case statements? You may want to look into using an array for the names and having it at the array to grab your data for the output. It will save you alot of coding. I have done this for a number of my jobs and it saves me a ton of time.
Link to comment
Share on other sites

Lester,

 

Thanks for your replies. Since the rule uses the Year and Month, the only thing I need to create accounts for is the Dean field. Which would only be about 20 or so.

 

However, if there is an easier, or better way of setting this up I would be all for it. I am quite an amateur with javascript. If you would be so kind to elaborate on how the array function would apply to my code that would be great. If not, that is fine also, you have already been very helpful.

Link to comment
Share on other sites

(1) How many unique entries are there in your data for the DEAN field?

(2) Do they consistently begin with the dean's name?

(3) Are there instances where one dean may be head of more than one curriculum?

 

If answers are (1)about 20, (2)yes, and (3)no you can just use the LEFT() function to retrieve the first X characters for determining which degree to concatenate into your graphic file name.

 

In this scenario, you would simply change:

switch (Field("Dean")) {

to this:

switch (Left(Field("Dean"),#) { 
//where # is number of characters to create a unique string

Then you would modify the text in quotes for each case statement to match those unique strings (i.e. "JOHN DOE","SUSAN KO","TOM SMIT" if # is 8).

Link to comment
Share on other sites

//Edit/add case values as necessary
switch (Field("Dean")) {
  case "John Doe": 
     var CurrDegree = "english";

Can I alter the code to look for some of the type? (with Contains)

so if the value of the field is "John Doe, Dean of blahblah" the code will still work?

Sure, you can use theString.indexOf function to determine if a string contains another substring, like so:

if (Field("Dean").indexOf("John Doe") != -1)
 return "english";

https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/String/indexOf

 

If you want, you can create a "Contains" function to make the rest of the code simpler, like so:

String.prototype.Contains = function(s)
{
 return this.indexOf(s) != -1;
}

if (Field("Dean").Contains("John Doe"))
 return "english";

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...