# 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 }