Skip to main content

Using PowerShell with DynamicPDF API

· 5 min read
James A Brannan

Use Microsoft's PowerShell to test DynamicPDF API endpoints from the command line quickly.

Although Postman and cURL are powerful ways to test your REST calls to the DynamicPDF API, did you know you can also use PowerShell? In this post I show you just how easy it is to test the DynamicPDF API endpoints using PowerShell.

Powershell

PowerShell is Microsoft's command-line shell built on .NET and is open-source and cross-platform, meaning you can also use it on MacOS and Linux.

PowerShell performs tasks called cmdlets, specialized .NET classes that perform a specific functionality. You can use these cmdlets directly via the PowerShell command line, via scripts, or packaged into modules that work with the .NET API.

Here, we use the Invoke-RestMethod cmdlet to send HTTPS POST requests to the DynamicPDF API REST endpoints.

info

Do not be misled by the simplicity of the command-line examples presented here. PowerShell is also a powerful scripting language designed to be easily integrated with .NET applications.

Invoke-RestMethod

The Invoke-RestMethod sends HTTP/HTTPS requests to RESTful web services. When used with the -InFile parameter, it sends binary directly as the body of the POST request. When used with the -Form parameter combined with the -ContentType "multipart/form-data" parameter a POST request's form fields are sent as the body.

The DynamicPDF API has two variations for sending POST requests to its endpoints. For the image-info, pdf-info, pdf-text, and pdf-xmp endpoints, the POST requests send the associated binary resource directly as the body. Therefore, we use the -InFile parameter with Invoke-RestMethod. For the dlex-layout and pdf endpoints, the binary resource is sent as form data from a form submission, and therefore, we use the -Form parameter.

danger

The Invoke-RestMethod -Form parameter requires PowerShell version 6.x or greater. Here I used PowerShell version 7.5

-InFile Parameter

For the image-info, pdf-info, pdf-text, and pdf-xmp endpoints we use the Invoke-RestMethod as a POST request with the -InFile parameter. The -InFile parameter sends the body content as the type specified by the -ContentType parameter. For example, the following command sends an HTPS POST request to the pdf-text endpoint.

Invoke-RestMethod -Method Post -Headers @{"Authorization" = "Bearer DP--api-key--"} -Uri "https://api.dpdf.io/v1.0/pdf-text" -Infile "C:\temp\dynamicpdf-api-samples\powershell-example\fw4.pdf" -ContentType "application/pdf"

Recall that these four DynamicPDF API endpoints take binary as the request's body.

EndpointCONTENT-TYPE
image-infoimage/png, image/gif, etc.
pdf-info,pdf-xmp, pdf-textapplication/pdf

-Form Parameter

For the dlex-layout and pdf endpoints, we use the Invoke-RestMethod as a POST request with the -Form parameter. The -Form parameter, combined with the -ContentType "multipart/form-data" parameter, sends the body content as a hashtable of fields and values in a form submission.

The parameter's syntax is an array of name/value pairs separated by a semicolon.

-Form @{<name> = "<value>"; <name> = "<value>"; ...}

Form fields with the same name but multiple values separate the values by a comma.

-Form @{<name> = "<value>", "<value>", ... ; <name> = "<value>"; ...}

To upload a file in a form submission, specify Get-Item and -Path for a form field.

-Form @{<name> = Get-Item -Path "<path>"}

Separate the paths with a comma for multiple files with the same form field name.

-Form @{<name> = Get-Item -Path "<path>", "<path>", ...}

The syntax will become clearer after the following examples.

Examples

Download all PowerShell commands used in this post from Github (powershell-examples.txt). This file contains examples of calling all of the DynamicPDF API REST endpoints using PowerShell.

You can download the resources used in this post from the powershell-examples folder on GitHub, powershell-example.

I assume a path of c:/temp/dynamicpdf-api-samples/powershell-example for all resources used on your local filesystem.

You must also have the following sample loaded in your cloud storage space in the DynamicPDF API Portal to run the example using the dlex-layout endpoint.

pdf-info, pdf-text, pdf-xmp, and image-info

These four DynamicPDF API endpoints all use the -Infile parameter to upload a file directly to the endpoint using an POST request.

endpointCommand
image-infoInvoke-RestMethod -Method Post -Headers @{"Authorization" = "Bearer DP--api-key--"} -Uri "https://api.dpdf.io/v1.0/image-info" -Infile "C:\temp\dynamicpdf-api-samples\powershell-example\dynamicpdfLogo.png" -ContentType "image/png"
pdf-infoInvoke-RestMethod -Method Post -Headers @{"Authorization" = "Bearer DP--api-key--"} -Uri "https://api.dpdf.io/v1.0/pdf-info" -Infile "C:\temp\dynamicpdf-api-samples\powershell-example\fw4.pdf" -ContentType "application/pdf"
pdf-textInvoke-RestMethod -Method Post -Headers @{"Authorization" = "Bearer DP--api-key--"} -Uri "https://api.dpdf.io/v1.0/pdf-text" -Infile "C:\temp\dynamicpdf-api-samples\powershell-example\fw4.pdf" -ContentType "application/pdf" | Format-Table -Wrap -Autosizeformat to display
pdf-textInvoke-RestMethod -Method Post -Headers @{"Authorization" = "Bearer DP.--api-key--"} -Uri "https://api.dpdf.io/v1.0/pdf-text" -Infile "C:\temp\dynamicpdf-api-samples\powershell-example\fw4.pdf" -ContentType "application/pdf" -Outfile "C:\temp\dynamicpdf-api-samples\powershell-example\output-two.json"write to file
pdf-xmlInvoke-RestMethod -Method Post -Headers @{"Authorization" = "Bearer DP.--api-key--"} -Uri "https://api.dpdf.io/v1.0/pdf-xmp" -Infile "C:\temp\dynamicpdf-api-samples\powershell-example\fw4.pdf" -ContentType "application/pdf" -Outfile "C:\temp\dynamicpdf-api-samples\powershell-example\output-three.xml"write to file

pdf and dlex-layout

These two DynamicPDF API endpoints use the -Form parameter to upload a file as a form field submission. Note that they also specify the Content-Type as multipart/form-data.

EndpointCommand
dlex-layoutInvoke-RestMethod -Method Post -Headers @{"Authorization" = "Bearer DP.--api-key--"} -Uri "https://api.dpdf.io/v1.0/dlex-layout" -Form @{LayoutData = Get-Item -Path "C:\temp\dynamicpdf-api-samples\powershell-example\creating-pdf-dlex-layout.json"; DlexPath = "samples/creating-pdf-dlex-layout-endpoint/creating-pdf-dlex-layout.dlex"} -ContentType "multipart/form-data" -Outfile "C:\temp\dynamicpdf-api-samples\powershell-example\output-three.pdf"create PDF
pdfInvoke-RestMethod -Method Post -Headers @{"Authorization" = "Bearer DP.--api-key--"} -Uri "https://api.dpdf.io/v1.0/pdf" -Form @{Instructions = Get-Item -Path "C:\temp\dynamicpdf-api-samples\powershell-example\instructions.json"; Resource = Get-Item -Path "C:\temp\dynamicpdf-api-samples\excel-word-conversion\sample-data.xlsx","C:\temp\dynamicpdf-api-samples\powershell-example\sample-doc.docx"} -ContentType "multipart/form-data" -Outfile "C:\temp\dynamicpdf-api-samples\powershell-example\output-one.pdf"convert Excel & Word to PDF

Numerous online resources exist for using PowerShell. You can create robust solutions using PowerShell, including incorporating them into your .NET applications, or you can use it to easily test your calls to the DynamicPDF API, as we did in this post.

   Follow us on social media for latest news!