diff --git a/src/Bundle/ChillDocStoreBundle/Service/Signature/PDFPage.php b/src/Bundle/ChillDocStoreBundle/Service/Signature/PDFPage.php new file mode 100644 index 000000000..138c4e1c8 --- /dev/null +++ b/src/Bundle/ChillDocStoreBundle/Service/Signature/PDFPage.php @@ -0,0 +1,28 @@ +index === $this->index + && round($page->width, 2) === round($this->width, 2) + && round($page->height, 2) === round($this->height, 2); + } +} diff --git a/src/Bundle/ChillDocStoreBundle/Service/Signature/PDFSignatureZone.php b/src/Bundle/ChillDocStoreBundle/Service/Signature/PDFSignatureZone.php new file mode 100644 index 000000000..515356e52 --- /dev/null +++ b/src/Bundle/ChillDocStoreBundle/Service/Signature/PDFSignatureZone.php @@ -0,0 +1,33 @@ +x == $other->x + && $this->y == $other->y + && $this->height == $other->height + && $this->width == $other->width + && $this->PDFPage->equals($other->PDFPage); + } +} diff --git a/src/Bundle/ChillDocStoreBundle/Service/Signature/PDFSignatureZoneParser.php b/src/Bundle/ChillDocStoreBundle/Service/Signature/PDFSignatureZoneParser.php new file mode 100644 index 000000000..3aade2fcc --- /dev/null +++ b/src/Bundle/ChillDocStoreBundle/Service/Signature/PDFSignatureZoneParser.php @@ -0,0 +1,57 @@ +parser = new Parser(); + } + + /** + * @return list + */ + public function findSignatureZones(string $fileContent): array + { + $pdf = $this->parser->parseContent($fileContent); + $zones = []; + + $defaults = $pdf->getObjectsByType('Pages'); + $defaultPage = reset($defaults); + $defaultPageDetails = $defaultPage->getDetails(); + + foreach ($pdf->getPages() as $index => $page) { + $pdfPage = new PDFPage( + $index, + $page['MediaBox'][2] ?? $defaultPageDetails['MediaBox'][2], + $page['MediaBox'][3] ?? $defaultPageDetails['MediaBox'][3], + ); + + foreach ($page->getDataTm() as $dataTm) { + if (str_starts_with($dataTm[1], self::ZONE_SIGNATURE_START)) { + $zones[] = new PDFSignatureZone($dataTm[0][4], $dataTm[0][5], $this->defaultHeight, $this->defaultWidth, $pdfPage); + } + } + } + + return $zones; + } +} diff --git a/src/Bundle/ChillDocStoreBundle/Tests/Service/Signature/PDFSignatureZoneParserTest.php b/src/Bundle/ChillDocStoreBundle/Tests/Service/Signature/PDFSignatureZoneParserTest.php new file mode 100644 index 000000000..5a78c8bbf --- /dev/null +++ b/src/Bundle/ChillDocStoreBundle/Tests/Service/Signature/PDFSignatureZoneParserTest.php @@ -0,0 +1,77 @@ + $expected + */ + public function testFindSignatureZones(string $filePath, array $expected): void + { + $content = file_get_contents($filePath); + + if (false === $content) { + throw new \LogicException("Unable to read file {$filePath}"); + } + + $actual = self::$parser->findSignatureZones($content); + + self::assertEquals(count($expected), count($actual)); + + foreach ($actual as $index => $signatureZone) { + self::assertObjectEquals($expected[$index], $signatureZone); + } + } + + public static function provideFiles(): iterable + { + yield [ + __DIR__.'/data/signature_2_signature_page_1.pdf', + [ + new PDFSignatureZone( + 127.7, + 95.289, + 180.0, + 180.0, + $page = new PDFPage(0, 595.30393, 841.8897) + ), + new PDFSignatureZone( + 269.5, + 95.289, + 180.0, + 180.0, + $page, + ), + ], + ]; + } +} diff --git a/src/Bundle/ChillDocStoreBundle/Tests/Service/Signature/data/signature_2_signature_page_1.pdf b/src/Bundle/ChillDocStoreBundle/Tests/Service/Signature/data/signature_2_signature_page_1.pdf new file mode 100644 index 000000000..89d91e67c Binary files /dev/null and b/src/Bundle/ChillDocStoreBundle/Tests/Service/Signature/data/signature_2_signature_page_1.pdf differ