Skip to content

Commit

Permalink
Switch from fpdi/fpdf to pdftk to be able to use more recent/secure p…
Browse files Browse the repository at this point in the history
…df protection algorithm
  • Loading branch information
Matthias Devlamynck committed Jan 30, 2024
1 parent dab4651 commit adad3af
Show file tree
Hide file tree
Showing 5 changed files with 226 additions and 205 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
"doctrine/migrations": "^3.0",
"doctrine/orm": "^2.7",
"knplabs/knp-gaufrette-bundle": "^0.7.2",
"mikehaertl/php-pdftk": "^0.13.1",
"richcongress/bundle-toolbox": "^1.1",
"sensio/framework-extra-bundle": "^5.4|^6.0",
"setasign/fpdi-protection": "^2.0",
"symfony/translation": "^5.4",
"symfony/twig-bundle": "^5.4"
},
Expand Down
314 changes: 140 additions & 174 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 16 additions & 17 deletions src/Domain/Pdf/Trait/PdfMergerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,30 @@

namespace RichId\PdfTemplateBundle\Domain\Pdf\Trait;

use setasign\Fpdi\Fpdi;
use setasign\Fpdi\PdfParser\StreamReader;
use mikehaertl\pdftk\Pdf;

trait PdfMergerTrait
{
/** @param string[] $pdfs */
private function mergePdfs(array $pdfs): string
{
$encoder = new Fpdi();
use PdfTempFileTrait;

foreach ($pdfs as $pdf) {
$pageCount = $encoder->setSourceFile(StreamReader::createByString($pdf));
/** @param string[] $sources */
private function mergePdfs(array $sources): string
{
return $this->withTempDir(function (string $tempDir) use ($sources) {
$pdf = new Pdf();
$pdf->ignoreWarnings = true;

for ($i = 1; $i <= $pageCount; $i++) {
$tplidx = $encoder->importPage($i);
$specs = $encoder->getTemplateSize($tplidx);
foreach ($sources as $index => $source) {
$pdf->addFile($this->copySource($source, $tempDir, $index + 1));
}

if (\is_array($specs)) {
$encoder->addPage($specs['orientation'], [$specs['width'], $specs['height']]);
}
$result = $pdf->toString();

$encoder->useTemplate($tplidx);
if (\is_bool($result)) {
throw new \Exception('Failed to generate pdf file');
}
}

return $encoder->Output('S');
return $result;
});
}
}
Loading

0 comments on commit adad3af

Please sign in to comment.