How to Switch the Number of Pages (Paging) in Automatic PDF Form Generation

Automatically generate PDF form by switching the number of pages depending on the amount of data.


A little while ago, I received the same question in succession.


Q. Is it possible to switch the number of pages according to the amount of data in automatic PDF form generation of Questetra?
A. It is possible!


I will explain the method with which you can correspond this need, based on cases I actually dealt with.

  • Case 1: Switching the number of pages according to the number of lines on the detail table in Expense claim (Switch according to the number of rows of Table type data item)
  • Case 2: Switching the number of pages according to the volume of the sentences in the report. (Switching according to the number of lines and the amount of characters of String type data item (multiple lines))


* In the first place, regarding how to automatically generate a PDF form in Questetra, please refer to the following.
M228 AUTOMATED STEP Auto Generating PDF File with Business data Inserted


Paging image


As for the method, the scheme is shown below.

  • Preparing the Template PDF as page 1 ・ pages 2, for each pattern (Therefore, it is not suitable for those cases where there are many pages. There is also an upper limit to the number of pages.)
  • As the flow of the Process Model, the Script Task judges the number of pages from the data amount, and switches the Template PDF to be used by a Split gateway.


process model


When using the Table type, it takes a bit of effort to set the Template PDF since there are many field items to configure.
On the other hand, the script for judging the number of pages in a Script Task is very simple.
The following samples are helpful for creating a Template PDF.
“Expense Claim, Monthly Auto-Start, PDF Auto-Generation”
The script sample is the following (The number of page is set as Select type data item).

var table = data.get("1");

if (table) {
  if (table.size() <= 20) { //1 page if it is within 20 lines
    var selects = [ "1" ];
    retVal.put("2", selects);
  } else {
    var selects = [ "2" ];
    retVal.put("2", selects);
} else { //Consideration for the case where the table is empty
  var selects = [ "1" ];
  retVal.put("2", selects);


Compared with the Table type of Case 1, the setting of the Template PDF is easy.
On the other hand, the logic to judge the number of pages in the Script Task is a bit more complicated. It is because it is necessary to judge by counting the number of lines considering the number of characters per line. In addition, it is also necessary to add Data Items to contain split string data.
The script sample is as follows. (I made its logic so it wouldn't be too strict.)

var text = data.get("1") + "";

//Considering only up to 2 pages due to the Template PDF
var cutText = new Array();
cutText[0] = "";
cutText[1] = "";

//Consideration for the case where the string is empty
if (text != "null") {
  var lines = text.split(/\r\n|\r|\n/);

  var textCount = 0;
  var j = 0;

  for (i = 0; i < lines.length; i++) {
    //One line is rounded up to 50 characters then count the number of lines.
    var count = Math.ceil(lines[i].length / 50);

    //Consideration for empty line.
    if (count == 0) {
      count = 1;

    //Up to 30 lines per page and a page break. (Not considering cases where one line is abnormally long.)
    if (textCount + count > 30) {
      textCount = count;
      cutText[j] = lines[i] + "\n";
    } else {
      textCount += count;
      cutText[j] += lines[i] + "\n";

retVal.put("2", cutText[0]);
retVal.put("3", cutText[1]);


About Kusaka Tsuyoshi

View all posts by Kusaka Tsuyoshi

Prev article - 50. Questetra Tips Automate Processing by Accessing its Own API
Next article - 50. Questetra Tips Can Backlog and Cloud BPM Cooperate?
Another article - Kusaka Tsuyoshi Tips When You are in Trouble with kintone’s “Workflow Management” (part 2)