cubemage/paging-seal-bundle
Symfony 6+ PDF 盖章组件:支持骑缝章与单页盖章,将印章图片按可配置规则叠加到 PDF。提供 PagingSealGenerator 服务,支持默认印章路径与临时文件目录配置,依赖 PHP 8.1+ 与 gd 扩展。
Installation:
composer require cubemage/paging-seal-bundle
Ensure gd PHP extension is enabled for image processing.
Enable Bundle:
Add to config/bundles.php if not auto-loaded by Flex:
CubeMage\PagingSealBundle\CubeMagePagingSealBundle::class => ['all' => true],
First Use Case:
Inject PagingSealGenerator into a controller/service and call generate() with:
default_seal_path if configured)'right', 'left', 'top', 'bottom')Example:
$this->sealGenerator->generate(
$pdfPath,
$sealPath,
'right',
30, // size
5 // margin
);
File Handling:
UploadedFile.getPathname() to get temporary paths for processing.deleteFileAfterSend(true).Service Integration:
PagingSealGenerator and call generate() in action methods.Configuration:
default_seal_path in config/packages/cube_mage_paging_seal.yaml to avoid passing seal paths repeatedly.pdf_path for temporary file storage (default: %kernel.project_dir%/var/cubemage/paging-seal).Response Handling:
BinaryFileResponse for downloads:
return $this->file($generatedPdfPath, 'sealed-document.pdf')->deleteFileAfterSend(true);
Dynamic Seal Selection: Use a database-backed service to fetch seal paths based on user/role (e.g., company-specific seals). Example:
$sealPath = $this->sealRepository->getSealPathForUser($userId);
Validation: Validate PDF/seal files before processing:
if (!$documentFile->isValid() || !$documentFile->getClientOriginalExtension() === 'pdf') {
throw $this->createNotFoundException('Invalid PDF file.');
}
Asynchronous Processing: Dispatch a Symfony Messenger message to process PDFs in the background:
$this->messageBus->dispatch(new ProcessPdfSealMessage($pdfPath, $sealPath, 'right'));
File Permissions:
pdf_path directory is writable by the web server (e.g., chmod -R 775 var/cubemage).GD Extension:
gd is enabled (php -m | grep gd). Common issue on shared hosting.Memory Limits:
memory_limit. Increase if needed:
# config/packages/framework.yaml
framework:
php:
memory_limit: 512M
Seal Image Requirements:
Log Temporary Files: Add logging to track file paths:
$this->logger->debug('Generated PDF:', ['path' => $generatedPdfPath]);
Check Config Overrides:
Verify cube_mage_paging_seal.yaml is loaded (Symfony Flex may auto-configure it).
Custom Seal Logic:
Extend PagingSealGenerator to support:
Event Listeners: Trigger events before/after sealing (e.g., log processing time):
// src/EventListener/PdfSealListener.php
public function onPdfSealed(PdfSealedEvent $event) {
$this->logger->info('PDF sealed', ['path' => $event->getPath()]);
}
Seal Templates: Use Symfony’s templating to generate seals dynamically (e.g., add timestamps):
{# templates/seal/seal.html.twig #}
<img src="{{ asset('images/seal.png') }}" style="opacity: 0.8; filter: drop-shadow(0 0 2px #000);">
Performance:
Cache frequently used seals in memory (e.g., with Symfony’s CacheInterface).
Testing:
Mock PagingSealGenerator in PHPUnit:
$mockGenerator = $this->createMock(PagingSealGenerator::class);
$mockGenerator->method('generate')->willReturn('/path/to/mocked-pdf.pdf');
Localization:
Support multi-language seals by storing them in public/seals/{locale}.png and routing dynamically.
How can I help you explore Laravel packages today?