1. Go to this page and download the library: Download signerphp/pdf-signer 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/ */
use SignerPHP\PdfSigner\Presentation\Signer;
$prepared = Signer::externalSigner()
->withPdfContent(file_get_contents('/tmp/input.pdf'))
->withCertificate(file_get_contents('/tmp/certificate.pem'))
->withPadesBaselineB()
->prepare();
// Persist $prepared->state when the external operation is asynchronous.
// Inspect $prepared->payload for the input, encoding and algorithms
use SignerPHP\PdfSigner\Infrastructure\Native\Service\CurlHttpClient;
use SignerPHP\PdfSigner\Infrastructure\Remote\Vidaas\VidaasAuthorization;
use SignerPHP\PdfSigner\Infrastructure\Remote\Vidaas\VidaasCertificateDiscovery;
use SignerPHP\PdfSigner\Infrastructure\Remote\Vidaas\VidaasClient;
use SignerPHP\PdfSigner\Infrastructure\Remote\Vidaas\VidaasPkce;
use SignerPHP\PdfSigner\Infrastructure\Remote\Vidaas\VidaasSignatureProvider;
use SignerPHP\PdfSigner\Presentation\Signer;
$verifier = VidaasPkce::generateVerifier();
$http = new CurlHttpClient();
$authorization = new VidaasAuthorization($http, $clientId, $clientSecret);
$authorizationUrl = $authorization->authorizationUrl(
codeChallenge: VidaasPkce::challenge($verifier),
redirectUri: $callbackUrl,
state: $state,
);
// After the authorization callback:
$token = $authorization->exchangeAuthorizationCode(
$authorizationCode,
$verifier,
$callbackUrl,
);
$client = new VidaasClient($token->value, $http);
$certificate = (new VidaasCertificateDiscovery($client))->certificates()[0];
$builder = Signer::externalSigner()
->withPdfContent($pdf)
->withCertificate(
$certificate->certificate->certificatePem,
$certificate->certificate->chainPem,
)
->withPadesBaselineB();
$prepared = $builder->prepare();
$signature = (new VidaasSignatureProvider($client))->sign($prepared->payload, $certificate);
$signedPdf = $builder->complete($prepared->state, $signature->bytes);
use SignerPHP\PdfSigner\Application\DTO\SignatureActorDto;
use SignerPHP\PdfSigner\Application\DTO\SignatureMetadataDto;
use SignerPHP\PdfSigner\Presentation\Signer;
$metadata = new SignatureMetadataDto(
reason: 'Contract approval',
location: 'Sao Paulo - BR',
actor: new SignatureActorDto(
name: 'Maria Silva',
contactInfo: '[email protected]'
)
);
$signedPdf = Signer::signer()
->withPdfContent(file_get_contents('/tmp/input.pdf'))
->withCertificatePath('/tmp/certificate.pfx', 'secret-password')
->withMetadata($metadata)
->sign();
use SignerPHP\PdfSigner\Application\DTO\SignatureAppearanceDto;
use SignerPHP\PdfSigner\Presentation\Signer;
$appearance = new SignatureAppearanceDto(
backgroundImagePath: '/tmp/stamp.png',
rect: [350, 770, 500, 830], // [x1, y1, x2, y2]
page: 0 // 0-based index
);
$signedPdf = Signer::signer()
->withPdfContent(file_get_contents('/tmp/input.pdf'))
->withCertificatePath('/tmp/certificate.pfx', 'secret-password')
->withAppearance($appearance)
->sign();
use SignerPHP\PdfSigner\Application\DTO\ProtectionOptionsDto;
use SignerPHP\PdfSigner\Presentation\Signer;
$protectedPdf = Signer::protection()
->withPdfContent(file_get_contents('/tmp/input.pdf'))
->withProtection(ProtectionOptionsDto::preventCopy(
ownerPassword: 'owner-secret',
userPassword: ''
))
->protect();
file_put_contents('/tmp/output-protected.pdf', $protectedPdf);
use SignerPHP\PdfSigner\Application\DTO\ProtectionOptionsDto;
use SignerPHP\PdfSigner\Presentation\Signer;
$signedProtectedPdf = Signer::signer()
->withPdfContent(file_get_contents('/tmp/input.pdf'))
->withProtection(ProtectionOptionsDto::preventCopy(
ownerPassword: 'owner-secret',
userPassword: ''
))
->withCertificatePath('/tmp/certificate.pfx', 'secret-password')
->protectThenSign();
file_put_contents('/tmp/output-protected-signed.pdf', $signedProtectedPdf);
use SignerPHP\PdfSigner\Presentation\Signer;
$validation = Signer::validation()
->withPdfContent(file_get_contents('/tmp/input.pdf'))
->validate();
if (! $validation->hasSignatures) {
// PDF has no signatures
}
if ($validation->allValid) {
// all signatures were verified
}
use SignerPHP\PdfSigner\Presentation\Signer;
$validation = Signer::validation()
->withPdfContent(file_get_contents('/tmp/input.pdf'))
->enableTrustChainValidation('/etc/ssl/certs/ca-certificates.crt') // optional; if omitted, tries the system default bundle
->validate();
foreach ($validation->entries as $entry) {
var_dump($entry->trustValid); // true|false|null
}
use SignerPHP\PdfSigner\Presentation\Signer;
$validation = Signer::validation()
->withPdfContent(file_get_contents('/tmp/input.pdf'))
->withBrazilPolicy('/path/to/icp-brasil-bundle.pem')
->validate();
use SignerPHP\PdfSigner\Application\DTO\BrazilPolicyLpaUrlsDto;
use SignerPHP\PdfSigner\Application\DTO\BrazilTrustAnchorsOptionsDto;
use SignerPHP\PdfSigner\Presentation\Signer;
$validation = Signer::validation()
->withPdfContent(file_get_contents('/tmp/input.pdf'))
->withBrazilPolicy(
'/path/to/icp-brasil-bundle.pem',
new BrazilPolicyLpaUrlsDto(
lpaUrlAsn1Pades: 'https://your-endpoint/LPA_PAdES.der',
lpaUrlAsn1SignaturePades: 'https://your-endpoint/LPA_PAdES.p7s',
),
new BrazilTrustAnchorsOptionsDto(
directory: '/tmp/my-trust-anchors-cache',
urls: [
'http://acraiz.icpbrasil.gov.br/Certificado_AC_Raiz.crt',
'http://acraiz.icpbrasil.gov.br/credenciadas/RAIZ/ICP-Brasilv7.crt',
],
),
)
->validate();