diff --git a/examples/appearance-demo.php b/examples/appearance-demo.php new file mode 100644 index 0000000..35c8368 --- /dev/null +++ b/examples/appearance-demo.php @@ -0,0 +1,88 @@ + new CurlHandler(), + // note: guzzle requires this parameter to fully support PSR-18 + 'http_errors' => false, + 'verify' => $caBundle, + // timeout by api after ~300 seconds + 'timeout' => 360, +]); +// only required if you are using guzzle < 7 +$httpClient = new Psr18Wrapper($httpClient); +$requestFactory = new RequestFactory(); +$streamFactory = new StreamFactory(); + +$client = new Client( + $httpClient, + $requestFactory, + $streamFactory, + $settings['apiUrl'], + $settings['username'], + $settings['activationPin'] +); +//var_dump($client->getCertificates());die(); + +$module = new Module($client, $requestId, $certificateSerialNumber); +$module->setDigest(SetaPDF_Signer_Digest::SHA_512); + +$certificate = $client->getCertificateBySerialNumber($certificateSerialNumber); +$module->setCertificate($certificate['certificate']); +$module->setExtraCertificates($certificate['chain']); + +$reader = new SetaPDF_Core_Reader_File($file); +$writer = new SetaPDF_Core_Writer_File(__DIR__ . '/signed.pdf'); +// let's get the document +$document = SetaPDF_Core_Document::load($reader, $writer); + +// now let's create a signer instance +$signer = new SetaPDF_Signer($document); +$signer->setAllowSignatureContentLengthChange(false); +//$signer->setSignatureContentLength(36000); + +//// set some signature properties +$signer->setReason('Testing TRUST2GO'); + +$field = $signer->addSignatureField( + 'Signature', + 1, + SetaPDF_Signer_SignatureField::POSITION_RIGHT_TOP, + ['x' => -160, 'y' => -100], + 180, + 60 +); + +$signer->setSignatureFieldName($field->getQualifiedName()); + +$appearance = new SetaPDF_Signer_Signature_Appearance_Dynamic($module); +$signer->setAppearance($appearance); + +$signer->sign($module); diff --git a/src/Client.php b/src/Client.php index 29404f5..e767cb7 100644 --- a/src/Client.php +++ b/src/Client.php @@ -100,13 +100,25 @@ public function getCertificates(bool $activeonly = true, bool $useronly = false) return \json_decode($responseBody, true); } + /** + * @param string $certificateSerialNumber + * @return array + * @throws ClientExceptionInterface + * @deprecated Use getCertificateBySerialNumber() instead. + * @see getCertificateBySerialNumber() + */ + public function getCertificatesBySerialNumber(string $certificateSerialNumber): array + { + return $this->getCertificateBySerialNumber($certificateSerialNumber); + } + /** * @param string $certificateSerialNumber * @return array{certificate: string, chain: array} * @throws ClientExceptionInterface * @throws Exception */ - public function getCertificatesBySerialNumber(string $certificateSerialNumber): array + public function getCertificateBySerialNumber(string $certificateSerialNumber): array { $certificates = $this->getCertificates(); // search for the correct certificate chain diff --git a/src/Module.php b/src/Module.php index c745cbc..ab60412 100644 --- a/src/Module.php +++ b/src/Module.php @@ -85,6 +85,14 @@ public function setCertificate($certificate) $this->padesModule->setCertificate($certificate); } + /** + * @return SetaPDF_Signer_X509_Certificate|string + */ + public function getCertificate() + { + return $this->padesModule->getCertificate(); + } + /** * Add additional certificates which are placed into the CMS structure. * @@ -128,10 +136,10 @@ public function addCrl($crl) */ public function createSignature(SetaPDF_Core_Reader_FilePath $tmpPath): string { - if ($this->padesModule->getCertificate() === null) { - $certificates = $this->client->getCertificatesBySerialNumber($this->certificateSerialNumber); - $this->padesModule->setCertificate($certificates['certificate']); - $this->padesModule->setExtraCertificates($certificates['chain']); + if ($this->getCertificate() === null) { + $certificate = $this->client->getCertificateBySerialNumber($this->certificateSerialNumber); + $this->setCertificate($certificate['certificate']); + $this->setExtraCertificates($certificate['chain']); } $digest = $this->padesModule->getDigest();