Creating Documents with DirectSmile Crossmedia totally dynamical

11. März 2014

Today I would like to blog about creating dynamic documents with the new DirectSmile Crossmedia V6.

When talking about a ‚dynamic document‘ I mean a document whose content will be constructed on a dynamic way. For this blog I will allow the user to pick several chapters from a list and will get a PDF in return. So the final document will need to be build based on the users selection.

To achieve my goal I will first need a PURL which allows my user to pick several available chapters. Thisfor I will need the ‚MultiSelect‘ item from the extension items library.

So my basic PURL will require a firstname, a lastname and a selection of several chapters:

Simple setup for our PURL

Simple setup for our PURL

Creating a dynamic document which will be rendered as PDF requires a XML which we call DocML. This is a language which allows you to create documents just with a XML syntax. Please see the manual (Document and Composition XML Commands) for more details about DocML.

The multiple selection will be stored as a stringlist with pipes as delimeter. Next we will need to create a new ’scripted data relation‘ to generate the DocML which will generate our document based on the users selection.

So just add a new scripted datarelation and edit it to read from the current table row and split the string by ‚|‘:

A first setup for our data relation.

A first setup for our data relation.

As we now have all informations needed to set up our document we will start creating our XML. We will simply create new pages for every valid entry in our splitted string which contains only a single image referencing to a PDF file containing the selected chapter which I’ve uploaded to my template images before.

In case your chapters are longer than single pages you will need another database containing individual informations about each chapter so you can regard these in your JavaScript. But for our small example we simply assume that each chapter only consists of one single page.

Final DataRelation with full DocML result.

Final DataRelation with full DocML result.

Also we need a ButtonAction defined inside our DataRelation so that we can write the DocML to our database. Please recognize that I’ve also added a second condition (Mode ===‘ ButtonAction‘):

In the head section of the DataRelation we define a ButtonAction to trigger the write-2-db from outside.

In the head section of the DataRelation we define a ButtonAction to trigger the write-2-db from outside.

This ButtonAction needs to be assigned to our submit button on the PURL:

Assign the ButtonAction from the DataRelation to the submit button.

Assign the ButtonAction from the DataRelation to the submit button.

So after that we will only need to create a dummy document which needs to be web editable – this will allow us to render this document on our page but with our DocML as payload to overwrite the content with our dynamic one.

Configure the dummy document to pick the DocML from the correct database column

Configure the dummy document to pick the DocML from the correct database column

And finally we have all we want. We can now use this document to be rendered on a PURL, sent to the workflow or attached to an e-mail.

If you want to try this on your own please use the attached ZIP file to import this example campaign.

Finally to say: Many thanks to my dear colleague Wolfgang – without his work this article would not have been possible!

Advertisements

Creating random strings for a dummy table

23. Januar 2014

Today I worked on a JavaScript placed in a DirectSmile Crossmedia campaign, which is currently used for supporting auto-completion based on a list of entries coming from a DataRelation. The customer who uses this script asked if we could allow prefixes which will not being recognized by the auto-completion.

Well, sounded like a challenge – perfect for me as I like such things, challenge accepted! Before I could begin I had to get some dummy data… but how?

So I could just create an empty campaign, fill the database with dummy data I have to write on my own and then link it with a DataRelation to the real campaign.

This approach sounds like a plan – but on the other hand it sounds like a lot of manual work to do. And as software engineers are lazy by nature I had the idea for a slightly different approach:

With the latest release of DirectSmile CrossMedia a new feature has been introduced: Scripted DataRelations! This special DataRelation uses JavaScript to produce data. So all I had to do was create some JavaScript which will create my dummy string which fulfills my needs.

To create a random string in JavaScript all you need is a string with all wanted chars – in my case I separated the capital letters from the normal ones and placed them in two strings:

var possibleCapitals = „ABCDEFGHIJKLMNOPQRSTUVWXYZ“;

var possible = „abcdefghijklmnopqrstuvwxyz“;

Now you only need a random number between 0 and the length of each source-string:

for (var i = 0; i < 10; i++) {

randomString += possible.charAt(Math.floor(Math.random() * possible.length));

}

This will create you a random string with 10 characters. Could be enough for my needs but it could even be „fancier“ – using capitals in front of each cityname and different string lengths:

So I’ve added a random length variable with at least four characters and a maximum of 16:

var length = Math.floor(4 + Math.random() * 12);

And added this to my for-loop:

var possibleCapitals = „ABCDEFGHIJKLMNOPQRSTUVWXYZ“;

var possible = „abcdefghijklmnopqrstuvwxyz“;

var length = Math.floor(4 + Math.random() * 12);

var randomString = possibleCapital.charAt(Math.floor(Math.random() * possibleCapital.length));

for (var i = 0; i < length; i++) {

randomString += possible.charAt(Math.floor(Math.random() * possible.length));

}

Now I’ve got a nice function which will render me one single random string with a capital letter at the beginning. So all I needed to do now is to call this snippet of code a couple of times and add each string to my DataRelation output.

After adding a Scripting Relation…

Add a Scripting Relation

… in my campaign I’ve just copy & pasted the code from above to it’s editor…

Edit the data source

… and modified the section where the data is added to the „newRow“ variable:

if(Mode == „GetCount“)
{
    MyTable.ResultCount = 100;
}
else if(Mode == „LoadData“)
{
    var possibleCapitals = „ABCDEFGHIJKLMNOPQRSTUVWXYZ“;
    var possible = „abcdefghijklmnopqrstuvwxyz“;
    var tResult = “;
    for (var n = 0; n < 101; n++) {
        tResult = ‚NAV ‚ + possibleCapitals.charAt(Math.floor(Math.random() * possibleCapitals.length));
        var length = Math.floor(4 + Math.random() * 12);
        for (var j = 0; j < length; j++) {
            tResult += possible.charAt(Math.floor(Math.random() * possible.length));
        }
    var newRow = MyTable.AddRow();
    newRow.SetValue(„ID“, n);
    newRow.SetValue(1, tResult);
}
}
As you can see I’ve already placed my specific prefix to the random string variable („NAV „). And finally I had my data presented:
See the result
And finally I could begin my work on the auto-completion with the nearly correct values 🙂 Challenge won!!