Cloud - Create a PDF using PHP with JSON Data via API

This example shows how to generate a PDF from a DOCX 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 and then plug your Docmosis Cloud access key into the code below, then run.

Note: This code sample is written to specifically work with DWS4.

<?php

     # This sample PHP code calls the Docmosis cloud to render the default "samples/WelcomeTemplate.docx" into a PDF file.

     # Set your access key here. This is visible in your cloud account in the Docmosis Web Site.
     # It is your key to accessing your service - keep it private and safe.
     $accessKey = 'XXXX';

     # RESTful service host. Set your region here.
     # End-point in the USA
     $DWSRenderURL = 'https://us1.dws4.docmosis.com/api/render';
     # End-point in the EU
     #$DWSRenderURL = 'https://eu1.dws4.docmosis.com/api/render';
     # End-point in AUS
     #$DWSRenderURL = 'https://au1.dws4.docmosis.com/api/render';

     if ($accessKey == 'XXXX') {
        echo "Please set your access key\n";
        exit(1);
     }

     $templateName = 'samples/WelcomeTemplate.docx';
     $outputName = 'myWelcome.pdf';

     $data = '{"title":"Welcome to Docmosis in the Cloud",
               "messages":
               [{"msg":"This cloud experience is better than I thought."},
                {"msg":"The sun is shining."},
                {"msg":"Right, now back to work."}]
              }';

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


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

     $ch = curl_init($DWSRenderURL);

     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);
     // curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

     $responseData = curl_exec($ch);

     if ( $responseData != false) {

       $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";
       }

     } else {

         echo "curlexec failed.\n\nDocmosis Cloud must be used via HTTPS.\n\nCheck your CA certificates, or try un-commenting CURLOPT_SSL_VERIFYPEER line (for troubleshooting only)";

     }


?>
 

Feedback

Invalid Input

Sorry, this field will only accept letters and numbers, and not special characters, to limit spam. Please also consider contacting support@docmosis.com if you need help with this article.

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

Invalid Input

API Overview

Docmosis offers a high-performance, template-based PDF generation API. Suitable for use with custom software applications built using Java, C#/.Net, Python, Ruby and more. See a side-by-side comparison of the API for our SaaS and self-hosted products.