PHP code example of cubemage / paging-seal-bundle
1. Go to this page and download the library: Download cubemage/paging-seal-bundle 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/ */
cubemage / paging-seal-bundle example snippets
// config/bundles.php
return [
// ... 其他的 bundle
CubeMage\PagingSealBundle\CubeMagePagingSealBundle::class => ['all' => true],
];
namespace App\Controller;
// 1. 引入核心服务类
use CubeMage\PagingSealBundle\Service\PagingSealGenerator;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\Routing\Annotation\Route;
class MyPdfController extends AbstractController
{
// 2. 通过构造函数自动注入服务
public function __construct(
private PagingSealGenerator $sealGenerator
) {}
#[Route('/api/contracts/process-seal', name: 'api_process_seal', methods: ['POST'])]
public function processPdfAction(Request $request): BinaryFileResponse
{
// 3. 从请求中获取上传的文件
/** @var UploadedFile|null $documentFile */
$documentFile = $request->files->get('pdf');
/** @var UploadedFile|null $sealFile */
$sealFile = $request->files->get('seal_image'); // 这是可选的公章图片
// 进行必要的檔案验证...
if (!$documentFile || !$documentFile->isValid()) {
throw new \InvalidArgumentException("必须上传有效的PDF文件。");
}
$sealPath = null;
if ($sealFile && $sealFile->isValid()) {
// 如果用户上传了自定义公章,使用它的临时路径
$sealPath = $sealFile->getPathname();
}
try {
// 4. 调用组件服务的核心方法来生成骑缝章PDF
// 如果 $sealPath 为 null,服务会自动使用您在配置文件中配置的 default_seal_path
$generatedPdfPath = $this->sealGenerator->generate(
$documentFile->getPathname(),
$sealPath,
'right', // 盖章位置: 'right', 'left', 'top', 'bottom'
30, // 印章尺寸 (mm)
5 // 页边距 (mm)
);
// 5. 将生成的文件以二进制响应的形式,回传给用户下载
// ->deleteFileAfterSend(true) 会在下载完成后自动删除服务器上的临时文件,非常方便
return $this->file($generatedPdfPath, 'processed-document.pdf')->deleteFileAfterSend(true);
} catch (\Exception $e) {
// 在真实应用中,应该记录日志并返回一个更友好的错误响应
throw $this->createNotFoundException('PDF处理失败: ' . $e->getMessage());
}
}
}