Quanda Quanda - 19 days ago 6
JSON Question

"The validation of the PDF file failed." when trying to send signing request via email - docusign

****EDIT: Thanks for those links, @Hackerman. I was able to get it working in Postman and then in Powershell using the Docusign postman sample. However I cannot get it working with my own PDF documents. I suspect this is because I am not properly converting my PDFs to Base64. Anyone know how this can be done via Powershell?****

EDIT2: Was able to encode my PDFs to Base64 via powershell with this simple 1-liner
$docEncodedBase64 = [Convert]::ToBase64String((Get-Content $PDFPath -Encoding Byte))


We are trying to use Powershell 5.0 (and the

invoke-restmethod
cmdlet) to make requests to the docusign REST API in order to send a signing request via email.

In my tests I have been following this guide: https://docs.docusign.com/esign/guide/usage/request_a_signature.html#prepare-the-document-to-send-through-docusign but am getting an error when I send my POST request

We are going the route of a normal request (i.e., not multi-part request), and thus supplying the PDF document as bytes in base64-encoded format as the value of the documentBase64 property.

Here is my code to convert the PDF to base64 bytes:

# PDF document
$docContent = Get-Content 'Mutual_NDA.pdf'

# PDF as bytes
$docBytes = [System.Text.Encoding]::Unicode.GetBytes($docContent)

# PDF as Base-64 Encoded bytes
$docEncoded = [System.Convert]::ToBase64String($docBytes)


Then I define my JSON payload, which will be sent as the body in the POST request. In here I set the 'documentBase64' property to the base64 encoded string that I just converted above.

# JSON payload
$jsonPayload = @"
{
"documents": [
{
**"documentBase64": "$docEncoded"**,
"documentId": "1",
"fileExtension": "pdf",
"name": "Mutual_NDA.pdf"
}
],
"emailSubject": "Please sign the NDA",
"recipients": {
"signers": [
{
"email": "the_nda_signer@mailinator.com",
"name": "Darlene Petersen",
"recipientId": "1",
"routingOrder": "1",
"tabs": {
"dateSignedTabs": [
{
"anchorString": "signer1date",
"anchorYOffset": "-6",
"fontSize": "Size12",
"name": "Date Signed",
"recipientId": "1",
"tabLabel": "date_signed"
}
],
"fullNameTabs": [
{
"anchorString": "signer1name",
"anchorYOffset": "-6",
"fontSize": "Size12",
"name": "Full Name",
"recipientId": "1",
"tabLabel": "Full Name"
}
],
"signHereTabs": [
{
"anchorString": "signer1sig",
"anchorUnits": "mms",
"anchorXOffset": "0",
"anchorYOffset": "0",
"name": "Please sign here",
"optional": "false",
"recipientId": "1",
"scaleValue": 1,
"tabLabel": "signer1sig"
}
]
}
}
]
},
"status": "sent"
}
"@


And lastly, the HTTP request:

$Envelope = Invoke-RestMethod -uri ($BaseURL + '/envelopes') -Method Post -Body $jsonPayload -ContentType 'application/json' -Headers @{"X-Docusign-Authentication" = $XMLHeader}


Anyone have experience with this? Perhaps I am encoding the PDF as base64 incorrectly? I am truly stuck. Any help appreciated!
Thanks,

Eric

Answer

My PDF was not properly converting to Base64. Using this line of code in Powershell I was able to successfully create and send an envelope

$docEncodedBase64 = [Convert]::ToBase64String((Get-Content $PDFPath -Encoding Byte))

Thanks to @Hackerman for referencing Postman!

Comments