47 lines
1.1 KiB
PHP
47 lines
1.1 KiB
PHP
<?php
|
|
|
|
namespace App\Providers;
|
|
|
|
use InvalidArgumentException;
|
|
use RuntimeException;
|
|
use setasign\Fpdi\Fpdi;
|
|
|
|
class PdfMergeProvider {
|
|
protected array $files = [];
|
|
|
|
/**
|
|
* Fügt eine PDF-Datei zur Merge-Liste hinzu.
|
|
*/
|
|
public function add(string $filePath): self
|
|
{
|
|
$this->files[] = $filePath;
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Führt alle hinzugefügten PDFs zusammen und speichert sie.
|
|
*/
|
|
public function merge(string $outputPath): void
|
|
{
|
|
if (empty($this->files)) {
|
|
throw new RuntimeException("No PDF files added for merging.");
|
|
}
|
|
|
|
$pdf = new FPDI();
|
|
|
|
foreach ($this->files as $file) {
|
|
$pageCount = $pdf->setSourceFile($file);
|
|
|
|
for ($page = 1; $page <= $pageCount; $page++) {
|
|
$tpl = $pdf->importPage($page);
|
|
$size = $pdf->getTemplateSize($tpl);
|
|
|
|
$pdf->AddPage($size['orientation'], [$size['width'], $size['height']]);
|
|
$pdf->useTemplate($tpl);
|
|
}
|
|
}
|
|
|
|
$pdf->Output('F', $outputPath);
|
|
}
|
|
}
|