Cloud Render using PowerShell

This example shows how to generate a PDF from a DOCX template using Powershell.  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.

# 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
#  2) Make sure your you have the correct region specified in the $DWSRenderURL variable
#
# Copyright Docmosis 2019

# 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 = 'XXX'

# 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 -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 $DWSRenderURL `
    -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
}

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

Applies To

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