Cloud Code Samples

This page contains a collection of code samples that use the Docmosis Cloud service.

You will also need to create a Cloud account and then run the samples to connect to it.

 

C# Render

cURL List Templates using the command line

cURL Render using the command line

cURL Upload Template using the command line

Elixir Render to PDF



This example shows how to generate a PDF from a DOC template using Elixir.  It calls the Docomsis 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.

Java Get Template Sample Data

Java Get Template Structure

Java Render using JSON

Java Render using URL Encoding

Java Render using XML

Java Render via Proxy using JSON

Java Render via Proxy using XML

Java Upload Template Example

Node.js Render Example

PHP Render using JSON



<?php

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

     $accessKey = 'XXXX';


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

     $templateName = 'samples/WelcomeTemplate.doc';
     $outputName = 'output.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('https://dws2.docmosis.com/services/rs/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);
     // 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)";

     }


?>
 

 

PHP Render using XML



<?php

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

     $accessKey = 'XXXX';


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

     $templateName = 'samples/WelcomeTemplate.doc';
     $outputName = 'output.pdf';

     $data = '<data>
                <title>Welcome to Docmosis in the Cloud</title>
                <messages><msg>This cloud experience is better than I thought.</msg></messages>
                <messages><msg>The sun is shining.</msg></messages>
                <messages><msg>Right, now back to work</msg></messages>
              </data>';

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


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

     $ch = curl_init('https://dws2.docmosis.com/services/rs/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);
     // 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)";

     }


?>
 

 

PowerShell Render Example



# This PowerShell example calls the Docmosis web service to create a PDF document
# from the default template (samples/WelcomeTemplate.docx).
# To run:
#  1) plug your Docmosis access key into the $accessKey variable
#
# Copyright Docmosis 2019

# Set access key for Docmosis Cloud
$accesskey = 'XXX'

if ($accessKey -eq 'XXX') {
  Write-Error 'Please set your accessKey'
  Exit
}

# Setting compulsory request parameters
$templateName = 'samples/WelcomeTemplate.docx'
$outputName = 'result.pdf'
$outputFormat = 'pdf'

$data = ConvertTo-Json @{
  "title"    = "Hello from PowerShell";
  "messages" = @(
    @{
      "msg" = "This cloud experience is better than I thought.";
    },
    @{
      "msg" = "The sun is shining.";
    },
    @{
      "msg" = "Right, now back to work.";
    }
  )
}

$params = @{
  "templateName" = $templateName;
  "outputName"   = $outputName;
  "outputFormat" = $outputFormat;
  "accessKey"    = $accesskey;
  "data"         = $data;
}

try {
  # Use API to post request
  Invoke-RestMethod `
    -Uri https://dws2.docmosis.com/services/rs/render `
    -Method POST `
    -Body $params `
    -OutFile $outputName

  "Hello from PowerShell, Docmosis has rendered your document $outputName"
}
catch {
  # Get response body if error
  $result = $_.Exception.Response.GetResponseStream()
  $reader = New-Object System.IO.StreamReader($result)
  $reader.BaseStream.Position = 0
  $reader.DiscardBufferedData()
  $responseBody = $reader.ReadToEnd();
  $formatError = $responseBody | ConvertFrom-Json | ConvertTo-Json
  Write-Error $formatError
}

Python Delete Template Example

Python Render Example

Python Upload Template Example