PHP code example of pdfgate / pdfgate-sdk-php

1. Go to this page and download the library: Download pdfgate/pdfgate-sdk-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

pdfgate / pdfgate-sdk-php example snippets




use PdfGate\PdfGateClient;

$client = new PdfGateClient('live_your_api_key');

$generated = $client->generatePdf([
    'url' => 'https://example.com',
    'pageSizeType' => 'a4',
    'preSignedUrlExpiresIn' => 1200
]);

echo $generated->getFileUrl();

$client->generatePdf([
    'html' => '<h1>Hello</h1>',
    'pageSizeType' => 'a4',
    'metadata' => ['source' => 'sdk'],
]);

$client->uploadFile([
    'file' => new \CURLFile('/absolute/path/source.pdf', 'application/pdf', 'source.pdf'),
    'preSignedUrlExpiresIn' => 1200,
]);

use PdfGate\Enum\EnvelopeStatus;

$envelope = $client->createEnvelope([
    'requesterName' => 'John Doe',
    'documents' => [
        [
            'sourceDocumentId' => '6642381c5c61',
            'name' => 'Employment Agreement',
            'recipients' => [
                [
                    'email' => '[email protected]',
                    'name' => 'Anna Smith',
                    'signingOrder' => 1,
                ],
                [
                    'email' => '[email protected]',
                    'name' => 'Bob Jones',
                    'signingOrder' => 2,
                ],
            ],
        ],
    ],
    'metadata' => ['customerId' => 'cus_123'],
]);

if ($envelope->getStatus() === EnvelopeStatus::CREATED) {
    echo $envelope->getId();
}

use PdfGate\Enum\EnvelopeStatus;

$sentEnvelope = $client->sendEnvelope('69c0fa44f83ca6a7015f1c8c');

if ($sentEnvelope->getStatus() === EnvelopeStatus::IN_PROGRESS) {
    echo 'Envelope emails have been sent.';
}

use PdfGate\Enum\EnvelopeStatus;

$envelope = $client->getEnvelope('69c0fa44f83ca6a7015f1c8c');

if ($envelope->getStatus() === EnvelopeStatus::IN_PROGRESS) {
    echo 'Envelope is still awaiting signatures.';
}

use PdfGate\Enum\EnvelopeStatus;

$voided = $client->voidEnvelope('69c0fa44f83ca6a7015f1c8c', 'Contract terms changed');

if ($voided->getStatus() === EnvelopeStatus::VOIDED) {
    echo 'Envelope has been voided.';
}

$client->deleteEnvelope('69c0fa44f83ca6a7015f1c8c');

$envelope = $client->createEnvelope([
    'requesterName' => 'John Doe',
    'documents' => [
        [
            'sourceDocumentId' => '6642381c5c61',
            'name' => 'Employment Agreement',
            'recipients' => [
                [
                    'email' => '[email protected]',
                    'name' => 'Anna Smith',
                    'embedded' => true,
                ],
            ],
        ],
    ],
]);

$client->sendEnvelope($envelope->getId());

$embedLink = $client->createEmbedLink($envelope->getId(), [
    'documentId' => '6642381c5c61',
    'recipientId' => 'rcp_1a2b3c4d5e6f',
    'returnUrl' => 'https://example.com/signed',
]);

echo '<iframe src="' . htmlspecialchars($embedLink->getUrl()) . '"></iframe>';

// Email is stored lowercased and cannot be changed later.
$recipient = $client->createRecipient([
    'email' => '[email protected]',
    'name' => 'Anna Smith',
    'metadata' => ['customerId' => 'cus_123'],
]);

// Case-insensitive lookup, oldest first.
$recipients = $client->listRecipients('[email protected]');

$fetched = $client->getRecipient($recipient->getId());

// Updates do not affect existing envelopes; they keep the recipient
// name they were created with.
$updated = $client->updateRecipient($recipient->getId(), [
    'name' => 'Anna Jones',
]);

// Removes the recipient's personal data from the directory; envelopes
// already sent are not affected. A recipient with an active stored
// signature or a pending enrolment cannot be deleted until the signature
// is removed (see Automatic Signing).
$client->deleteRecipient($recipient->getId());

use PdfGate\Enum\StoredSignatureStatus;

$pending = $client->requestRecipientSignature($recipient->getId());
echo $pending->getSignature()->getStatus(); // "pending"

// Once the recipient completes the enrolment (webhook event
// `recipient.signature.enrolled`), the signature is active.
$enrolled = $client->getRecipient($recipient->getId());
$signature = $enrolled->getSignature();
if ($signature !== null && $signature->getStatus() === StoredSignatureStatus::ACTIVE) {
    echo $signature->getImage(); // PNG data URL
}

// Sign automatically: the recipient is signed at send, or when their turn
// in the signing order comes. No signing email is sent.
$envelope = $client->createEnvelope([
    'requesterName' => 'HR',
    'documents' => [
        [
            'sourceDocumentId' => '6642381c5c61',
            'name' => 'Employment Agreement',
            'recipients' => [
                [
                    'email' => '[email protected]',
                    'name' => 'Anna Smith',
                    'role' => 'employee',
                    'signingOrder' => 1,
                ],
                [
                    'recipientId' => $recipient->getId(),
                    'role' => 'ceo',
                    'signingOrder' => 2,
                    'autoSign' => true,
                ],
            ],
        ],
    ],
]);

// Remove the stored signature or cancel a pending enrolment.
$client->removeRecipientSignature($recipient->getId());

$stream = $client->getFile($documentId);
$output = fopen('output.pdf', 'wb');
stream_copy_to_stream($stream, $output);
fclose($output);
fclose($stream);

use PdfGate\Enum\DocumentFieldType;

$doc = $client->addFormFields([
    'documentId' => $documentId,
    // Customize placeholder fields detected in the PDF, keyed by field name.
    'fieldOverrides' => [
        'signature' => ['role' => 'signer', 'optional' => false],
    ],
    // Or place fields at explicit positions on a given page.
    'fields' => [
        [
            'name' => 'signed_on',
            'type' => DocumentFieldType::DATE,
            'page' => 1,
            'x' => 100,
            'y' => 650,
            'width' => 160,
            'height' => 24,
        ],
    ],
]);

$flattened = $client->flattenPdf([
    'documentId' => $documentId,
    // Omit fieldNames to flatten the whole document.
    'fieldNames' => ['signature', 'date'],
]);

$client->deleteDocument($documentId);

use PdfGate\Enum\WebhookEventType;

// The returned secret is shown only once — store it to verify payloads.
$webhook = $client->createWebhook([
    'url' => 'https://example.com/pdfgate-callback',
    'eventTypes' => [
        WebhookEventType::ENVELOPE_COMPLETED,
        WebhookEventType::ENVELOPE_SENT,
    ],
    'description' => 'Production signing events',
]);

$fetched = $client->getWebhook($webhook->getId());
$client->deleteWebhook($webhook->getId());

use PdfGate\Exception\SignatureVerificationException;
use PdfGate\Webhook\WebhookSignatureVerifier;

$secret = 'whsecret_...';
$signatureHeader = $_SERVER['HTTP_X_PDFGATE_SIGNATURE'] ?? null;
$rawBody = file_get_contents('php://input');

try {
    WebhookSignatureVerifier::verify($secret, $signatureHeader, $rawBody === false ? '' : $rawBody);
    http_response_code(200);
} catch (SignatureVerificationException $e) {
    error_log($e->getMessage());
    http_response_code(400);
}
bash
composer 
bash
RELEASE_VERSION=1.2.3 php scripts/prepare-release.php
bash
DRY_RUN=1 RELEASE_VERSION=1.2.3 php scripts/prepare-release.php