0

I am using PHP SDK for docusign integration in laravel. I am trying to download signed document envelop in my public folder. I am getting the PDF content after following the steps. But when i tried to save it in PDF file using file_put_contents then its save a PDF file but after opening it gives me PDF document is damaged.

$ch = curl_init();
$headers = array(
    'Content-Type: application/pdf',
    'Authorization: Bearer '.$_SESSION['docusign_auth_code']
);
curl_setopt($ch, CURLOPT_URL, 'https://demo.docusign.net/restapi/v2/accounts/{account_id}/envelopes/{envelop_id}/documents/1/');
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HEADER, 0);


curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET"); 

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// Timeout in seconds
curl_setopt($ch, CURLOPT_TIMEOUT, 30);

$file_content = curl_exec($ch);


file_put_contents("my_document.pdf", ($file_content));
return $file_content;

I have tried above code and this code is working but PDF file is giving error on opening.

rnegi
  • 1
  • 1
  • 2

1 Answers1

0

See Send file to the user for more details, here is PHP code that we have in https://github.com/docusign/code-examples-php/blob/master/src/Controllers/Examples/eSignature/EG007EnvelopeGetDoc.php for this:

public function createController(): void
    {
        $this->checkDsToken();
        $envelope_id = $this->args['envelope_id'];
        $envelope_documents = $_SESSION['envelope_documents'] ?? false;
        if ($envelope_id && $envelope_documents) {
            # Call the worker method
            # More data validation would be a good idea here
            # Strip anything other than characters listed
            $docNameAndData = EnvelopeGetDocService::envelopeGetDoc($this->args, $this->clientService);

            if ($docNameAndData) {
                # See https://stackoverflow.com/a/27805443/64904
                header("Content-Type: {$docNameAndData['mimetype']}");
                header("Content-Disposition: attachment; filename=\"{$docNameAndData['doc_name']}\"");
                ob_clean();
                flush();
                $file_path = $docNameAndData['data']->getPathname();
                readfile($file_path);
                exit();
            }
        } else {
            $this->clientService->envelopeNotCreated(
                basename(__FILE__),
                $this->routerService->getTemplate($this::EG),
                $this->routerService->getTitle($this::EG),
                $this::EG,
                ['envelope_ok' => false, 'documents_ok' => false]
            );
        }
}
Inbar Gazit
  • 12,566
  • 1
  • 16
  • 23