How to replace text using PrintUI InDesign server

| | 2 min read

The PrintUI InDesign Server has certain APIs that are specifically designed for variable data applications. They could be used to completely automate creation of a JPEG or PDF with no human intervention.

Replace Text

Is used to replace the text. To replace all the text within a frame, use the replacetext API with just the "with" element. The style (e.g.fonts, point, size, color)will be the same for all the new text.
For example

<with color ="red"> This is the sentence<with>

The code to replace text as shown as below

$auth = 'Authorization code provided by PrintUI administrator';   
$jobid = 'Required job ID for an existing job';   
$hdr4 = post(https://ids.w2p-tools.com/d003/.'replacetext.php', array("auth" => $auth, "id" => $jobid,"data" => '<replace>
<text> frame="title" s="1"> <with>4715 E Launfal Avenue<with>
<text>
<replace>',
"testing" => "yes"));
print $hdr4;

function post($url, $fields, $body = '') {
  $fields_count = count($fields);
  // if there is some data
  $fh = null;
  $temp = '';

  if (strlen($body) > 0)
  {
    $temp = tempnam(TMPDIR, '~~~');
    $fh = fopen($temp, 'w+');
    fwrite($fh, $body);
    fseek($fh, 0);

    $fields = array_merge($fields, array('file' => '@'.$temp.';type=application/octet-stream;filename='.basename($temp)));
    $fields_count++;
  }

  // set the url, number of POST vars, POST data, timeout
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL,$url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_POST, $fields_count);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3600);
  curl_setopt($ch, CURLOPT_TIMEOUT, 3600);
  curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Expect:' ));

  // handle SSL
  if (substr($url, 0, 5) == 'https')
  {
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
    //curl_setopt($ch, CURLOPT_SSLVERSION, 3);
  }

  // execute post
  $result = curl_exec($ch);
  $hdr = curl_getinfo($ch);
  $hdr['result'] = $result;

  curl_close($ch);

  // delete temp file
  if ($fh != null)
  {
    fclose($fh);
    unlink($temp);
  }

  return $hdr;
}
  • https://ids.w2p-tools.com/d003/ is used as the API url.auth, id, testing ,data are the input parameters for this task.
  • 'auth' is Authorization code provided by PrintUI administrator when we have account in Print UI.
  • 'jobid' is a job ID for an existing job.
  • 'data' is an Required XML data indicating what text to replace.
  • 'testing' is Optional testing flag. May be "yes" or "no". Default is "no". Used in analytics logs
    to distinguish real API usage from testing efforts.

We get the response as the number replaced frames.<replacements n=<number of replacements>

For getting 'jobid' we can use 'newjob.php' for example

     $hdr = post('https://ids.w2p-tools.com/d003/'.'newjob.php', array("auth" => $auth, "t" => $t, "order" => time(), "n" => $n, "testing" => "yes"));
     $xml = simplexml_load_string($hdr['result']);
     $newjobid = (string)$xml['id'];
     
  • 't' is created template name
  • 'n' size of template(8.75x11.25)