dbp/relay-verity-connector-verapdf-bundle
Installation:
composer require dbp/relay-verity-connector-verapdf-bundle
Ensure DbpRelayVerityConnectorVerapdfBundle is listed before DbpRelayCoreBundle in config/bundles.php.
Configuration:
Create config/packages/dbp_relay_verity-connector-verapdf.yaml:
dbp_relay_verity_connector_verapdf:
url: '%env(VERAPDF_API_URL)%' # Required
maxsize: 10485760 # 10MB default (optional)
First Use Case:
Inject the VerapdfClient service into a controller or command:
use Dbp\Relay\VerityConnectorVerapdfBundle\Client\VerapdfClient;
public function validatePdf(VerapdfClient $client, UploadedFile $file)
{
$result = $client->validate($file->getPathname());
return new JsonResponse($result);
}
PDF Validation Endpoint:
VerapdfClient to validate PDFs in a Symfony controller:
public function validate(Request $request, VerapdfClient $client)
{
$file = $request->file('pdf');
$result = $client->validate($file->getPathname());
return $this->json($result);
}
annotations or YAML (e.g., validate_pdf route).Batch Processing:
use Dbp\Relay\VerityConnectorVerapdfBundle\Message\ValidatePdfMessage;
$bus->dispatch(new ValidatePdfMessage($filePath));
public function __invoke(ValidatePdfMessage $message, VerapdfClient $client)
{
return $client->validate($message->getFilePath());
}
Integration with Relay API:
RelayApiController to expose validation as an API endpoint:
#[Route('/api/validate-pdf', name: 'api_validate_pdf', methods: ['POST'])]
public function validatePdfApi(Request $request, VerapdfClient $client)
{
return $this->handleValidation($request, $client);
}
%env(VERAPDF_API_URL)% for the API endpoint to avoid hardcoding.if ($file->getSize() > $this->container->getParameter('verapdf.maxsize')) {
throw new \RuntimeException('File too large');
}
VerapdfClient calls in try-catch blocks to handle API failures gracefully:
try {
$result = $client->validate($filePath);
} catch (\Exception $e) {
return $this->json(['error' => $e->getMessage()], 500);
}
Bundle Order Dependency:
DbpRelayVerityConnectorVerapdfBundle is listed after DbpRelayCoreBundle in bundles.php, the service may not register.Missing Configuration:
ParameterNotFoundException if url or maxsize is missing in the YAML config.url and maxsize in config/packages/dbp_relay_verity-connector-verapdf.yaml.File Size Limits:
maxsize.API Rate Limits:
use Symfony\Component\Retry\Retry;
$client = new VerapdfClient($url, $maxsize);
$retry = Retry::create(3)->withDelay(1000);
$result = $retry->retry(fn() => $client->validate($filePath));
APP_DEBUG=true in .env to log detailed errors from the VerapdfClient.VerapdfClient to log raw API responses:
public function validate(string $filePath): array
{
$response = $this->httpClient->request('POST', $this->url, [
'body' => file_get_contents($filePath),
]);
$this->logger->debug('VeraPDF Response', ['response' => $response->getContent()]);
return json_decode($response->getContent(), true);
}
Custom Validation Rules:
VerapdfClient to add custom validation logic:
class CustomVerapdfClient extends VerapdfClient
{
public function validateWithCustomRules(string $filePath): array
{
$result = parent::validate($filePath);
if ($result['isValid'] && $this->hasCustomTag($filePath)) {
$result['customCheck'] = true;
}
return $result;
}
}
services:
Dbp\Relay\VerityConnectorVerapdfBundle\Client\CustomVerapdfClient:
arguments:
$url: '%env(VERAPDF_API_URL)%'
$maxsize: '%verapdf.maxsize%'
tags: ['verapdf.client']
Event Listeners:
#[AsEventListener(event: 'verapdf.validation.success')]
public function onValidationSuccess(ValidationSuccessEvent $event)
{
$this->mailer->send(new PdfValidatedEmail($event->getFilePath()));
}
Testing:
VerapdfClient in tests:
$mockClient = $this->createMock(VerapdfClient::class);
$mockClient->method('validate')->willReturn(['isValid' => true]);
$this->container->set(VerapdfClient::class, $mockClient);
How can I help you explore Laravel packages today?