Skip to content

Commit

Permalink
#107 Added readAndGuessFromContent to ZugferdDocumentPdfReader
Browse files Browse the repository at this point in the history
  • Loading branch information
HorstOeko committed Sep 6, 2024
1 parent 458ceb0 commit 1355220
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions src/ZugferdDocumentPdfReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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;
Expand Down

0 comments on commit 1355220

Please sign in to comment.