Tornado Render using PHP

This example shows how to generate a PDF from a DOC template using PHP.  It calls the Docmosis REST API to merge the data with the template and stream the result back.

The sample code includes the instructions to get started. You will need a Free Trial to install and run Tornado.

<?php

     # This sample PHP code calls the local Tornado server to render the default "WelcomeTemplate.doc" into a PDF file.

     $accessKey = '';
     $templateName = 'WelcomeTemplate.doc';
     $outputName = 'myWelcome.pdf';

     $data = '{"date":"20/Jan/2015","message":"This Tornado Document Engine is working great!"}';

     $request = array(
       "accessKey" => $accessKey,
       "templateName" => "$templateName",
       "outputName" => "$outputName",
       "data" => "$data"
     );


     $requestHeaders = array('Content-Type' => 'multipart/form-data');

     $ch = curl_init('http://localhost:8080/api/render');

     curl_setopt($ch, CURLOPT_HEADER, 0);
     curl_setopt($ch, CURLOPT_POST, 1);
     curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
     curl_setopt($ch, CURLOPT_HTTPHEADER, $requestHeaders);
     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

     $responseData = curl_exec($ch);
     $headers = curl_getinfo($ch);
     curl_close($ch);

     if ($headers['http_code'] == '200')  {

       // success!!
       // write the file out (since we didn't say where to send the
       // result, Docmosis sent us the result)

       $tempDirName = ".";
       $tempFileName = realpath($tempDirName) . "/" . $outputName;

       $renderedFile = file_put_contents($tempFileName, $responseData);

       echo "File saved to $tempFileName\n";

     } else {
       // failed - check error and result message
       echo "Failed:" . $responseData . "\n";
     }

?>

Feedback

Invalid Input

Sorry, this field will only accept letters and numbers, and not special characters, to limit spam.

Sorry, this field will only accept letters and numbers, and not special characters, to limit spam.

Invalid Input

Applies To

Docmosis-Java Tornado Cloud
Version(s) - ALL -

Related items