From 1355220f794965a67a224a80a14ba4c553184e2f Mon Sep 17 00:00:00 2001 From: HorstOeko Date: Fri, 6 Sep 2024 12:13:26 +0200 Subject: [PATCH] #107 Added readAndGuessFromContent to ZugferdDocumentPdfReader --- src/ZugferdDocumentPdfReader.php | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/ZugferdDocumentPdfReader.php b/src/ZugferdDocumentPdfReader.php index 7a01eb1..0c4e19e 100644 --- a/src/ZugferdDocumentPdfReader.php +++ b/src/ZugferdDocumentPdfReader.php @@ -11,6 +11,7 @@ use Smalot\PdfParser\Parser as PdfParser; use horstoeko\zugferd\exception\ZugferdFileNotFoundException; +use horstoeko\zugferd\exception\ZugferdFileNotReadableException; /** * Class representing the document reader for incoming PDF/A-Documents with @@ -27,7 +28,7 @@ class ZugferdDocumentPdfReader /** * List of filenames which are possible in PDF */ - const ATTACHMENT_FILENAMES = ['ZUGFeRD-invoice.xml'/*1.0*/, 'zugferd-invoice.xml'/*2.0*/, 'factur-x.xml'/*2.1*/, 'xrechnung.xml']; + public const ATTACHMENT_FILENAMES = ['ZUGFeRD-invoice.xml'/*1.0*/, 'zugferd-invoice.xml'/*2.0*/, 'factur-x.xml'/*2.1*/, 'xrechnung.xml']; /** * Load a PDF file (ZUGFeRD/Factur-X) @@ -42,8 +43,26 @@ public static function readAndGuessFromFile(string $pdfFilename): ?ZugferdDocume throw new ZugferdFileNotFoundException($pdfFilename); } + $pdfContent = file_get_contents($pdfFilename); + + if ($pdfContent === false) { + throw new ZugferdFileNotReadableException($pdfFilename); + } + + return static::readAndGuessFromContent($pdfContent); + } + + /** + * Load a PDF content (ZUGFeRD/Factur-X) + * + * @param string $pdfContent + * String Containing the binary pdf data + * @return ZugferdDocumentReader|null + */ + public static function readAndGuessFromContent(string $pdfContent): ?ZugferdDocumentReader + { $pdfParser = new PdfParser(); - $pdfParsed = $pdfParser->parseFile($pdfFilename); + $pdfParsed = $pdfParser->parseContent($pdfContent); $filespecs = $pdfParsed->getObjectsByType('Filespec'); $attachmentFound = false;