Sample code for 30+ languages & platforms
Pascal (Lazarus/Delphi)

DocuSign Download Envelope Document (PDF)

See more DocuSign Examples

Retrieves the specified document from the envelope. The response body of this method is the PDF file as a byte stream. You can get the file name and document ID from the response's Content-Disposition header.

Chilkat Pascal (Lazarus/Delphi) Downloads

Pascal (Lazarus/Delphi)
program ChilkatDemo;

// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.

{$IFDEF FPC}
  {$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}

uses
  {$IFDEF UNIX}
  cthreads,
  {$ENDIF}
  SysUtils,
  CkDllLoader,
  Chilkat.Http,
  Chilkat.BinData,
  Chilkat.StringBuilder,
  Chilkat.Mime,
  Chilkat.JsonObject;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  http: THttp;
  jsonToken: TJsonObject;
  url: string;
  bd: TBinData;
  respStatusCode: Integer;
  mime: TMime;
  filename: string;
  sbPath: TStringBuilder;

begin
  success := False;

  //  This example assumes the Chilkat API to have been previously unlocked.
  //  See Global Unlock Sample for sample code.

  http := THttp.Create;

  //  Implements the following HTTP request:
  //  GET /restapi/v2.1/accounts/{accountId}/envelopes/{envelopeId}/documents/1

  //  Adds the "Authorization: Bearer eyJ0eXAi.....UE8Kl_V8KroQ" header.
  jsonToken := TJsonObject.Create;
  //  Load a previously obtained OAuth2 access token.
  success := jsonToken.LoadFile('qa_data/tokens/docusign.json');
  if (success = False) then
    begin
      WriteLn(jsonToken.LastErrorText);
      Exit;
    end;

  http.AuthToken := jsonToken.StringOf('access_token');

  //  Use your account ID and a valid envelopeId here:
  http.SetUrlVar('accountId','7f3f65ed-5e87-418d-94c1-92499ddc8252');
  http.SetUrlVar('envelopeId','90d7e40a-b4bd-4ccd-bf38-c80e37954a13');

  url := 'https://demo.docusign.net/restapi/v2.1/accounts/{$accountId}/envelopes/{$envelopeId}/documents/1';
  bd := TBinData.Create;

  success := http.DownloadBd(url,bd);
  if (success = False) then
    begin
      WriteLn(http.LastErrorText);
      Exit;
    end;

  respStatusCode := http.LastStatus;
  WriteLn('Response Status Code = ' + respStatusCode);
  if (respStatusCode <> 200) then
    begin
      WriteLn('Response Header:');
      WriteLn(http.LastResponseHeader);
      //  The response body contains an error message.
      WriteLn(bd.GetString('utf-8'));
      WriteLn('Failed.');
      Exit;
    end;

  //  The response indicated success.
  //  Get the filename from the Content-Disposition header and save to a file.
  mime := TMime.Create;
  mime.LoadMime(http.LastResponseHeader);

  filename := mime.GetHeaderFieldAttribute('Content-Disposition','filename');
  WriteLn('filename = ' + filename);

  sbPath := TStringBuilder.Create;
  sbPath.Append('C:/aaworkarea/');
  sbPath.Append(filename);
  success := bd.WriteFile(sbPath.GetAsString());
  if (success = False) then
    begin
      WriteLn('Failed to save to output file.');
    end
  else
    begin
      WriteLn('Wrote ' + sbPath.GetAsString());
    end;


  http.Free;
  jsonToken.Free;
  bd.Free;
  mime.Free;
  sbPath.Free;

end;

// ---------------------------------------------------------------------------

begin

  try
    RunDemo;
  except
    on E: Exception do
      WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
  end;

  WriteLn;
  {$IFDEF MSWINDOWS}
  WriteLn('Press Enter to exit...');
  ReadLn;
  {$ENDIF}
end.