Skip to content

Commit

Permalink
#120 ZugferdDocumentPdfReader now contains additional methods for ext…
Browse files Browse the repository at this point in the history
…racting the XML content
  • Loading branch information
HorstOeko committed Sep 24, 2024
1 parent 1422fc1 commit 10fc7d3
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 26 deletions.
4 changes: 1 addition & 3 deletions build/phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@
<file>../tests/testcases/BuilderExtendedTest.php</file>
</testsuite>
<testsuite name="PDFReader">
<file>../tests/testcases/PdfReaderGeneralTest.php</file>
<file>../tests/testcases/PdfReaderMinimumSimpleTest.php</file>
<file>../tests/testcases/PdfReaderEn16931AllowanceChargeTest.php</file>
<file>../tests/testcases/PdfReaderEn16931SimpleTest.php</file>
<file>../tests/testcases/PdfReaderExtendedTest.php</file>
<file>../tests/testcases/PdfReaderExtended2Test.php</file>
<file>../tests/testcases/PdfReaderXRechnungSimpleTest.php</file>
<file>../tests/testcases/PdfReaderInvalidTest.php</file>
<file>../tests/testcases/PdfReaderMultipleAttachmentsTest.php</file>
</testsuite>
<testsuite name="PDFBuilder">
Expand All @@ -45,11 +45,9 @@
<file>../tests/testcases/ValidatorValidTest.php</file>
<file>../tests/testcases/ValidatorInvalidTest.php</file>
</testsuite>
<!--
<testsuite name="Exporter">
<file>../tests/testcases/JsonExporterTest.php</file>
</testsuite>
-->
<testsuite name="issues">
<file>../tests/testcases/issues/Issue10Test.php</file>
<file>../tests/testcases/issues/Issue18Test.php</file>
Expand Down
61 changes: 59 additions & 2 deletions src/ZugferdDocumentPdfReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,70 @@ public static function readAndGuessFromFile(string $pdfFilename): ?ZugferdDocume
}

/**
* Load a PDF content (ZUGFeRD/Factur-X)
* Tries to load an attachment content from PDF and return a ZugferdDocumentReader
* If any erros occured or no attachments were found null is returned
*
* @param string $pdfContent
* String Containing the binary pdf data
* @return ZugferdDocumentReader|null
*/
public static function readAndGuessFromContent(string $pdfContent): ?ZugferdDocumentReader
{
$xmlContent = static::extractXMLFromContent($pdfContent);

if (is_null($xmlContent)) {
return null;
}

try {
return ZugferdDocumentReader::readAndGuessFromContent($xmlContent);
} catch (\Exception $e) {
return null;
}
}

/**
* Returns a XML content from a PDF file
*
* @param string $pdfFilename
* Contains a full-qualified filename which must exist and must be readable
* @return string|null
*/
public static function getXmlFromFile(string $pdfFilename) : ?string
{
if (!file_exists($pdfFilename)) {
throw new ZugferdFileNotFoundException($pdfFilename);
}

$pdfContent = file_get_contents($pdfFilename);

if ($pdfContent === false) {
throw new ZugferdFileNotReadableException($pdfFilename);
}

return static::getXmlFromContent($pdfContent);
}

/**
* Returns a XML content from a PDF binary stream (string)
*
* @param string $pdfContent
* String Containing the binary pdf data
* @return string|null
*/
public static function getXmlFromContent(string $pdfContent) : ?string
{
return static::extractXMLFromContent($pdfContent);
}

/**
* Get the attachment content from XML.
* See the allowed filenames which are supported
*
* @param string $pdfContent
* @return string|null
*/
private static function extractXMLFromContent(string $pdfContent): ?string
{
$pdfParser = new PdfParser();
$pdfParsed = $pdfParser->parseContent($pdfContent);
Expand Down Expand Up @@ -87,7 +144,7 @@ public static function readAndGuessFromContent(string $pdfContent): ?ZugferdDocu
$embeddedFiles = $pdfParsed->getObjectsByType('EmbeddedFile');
foreach ($embeddedFiles as $embeddedFile) {
if ($attachmentIndex == $embeddedFileIndex) {
$returnValue = ZugferdDocumentReader::readAndGuessFromContent($embeddedFile->getContent());
$returnValue = $embeddedFile->getContent();
break;
}
$embeddedFileIndex++;
Expand Down
52 changes: 52 additions & 0 deletions tests/testcases/PdfReaderGeneralTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace horstoeko\zugferd\tests\testcases;

use horstoeko\zugferd\exception\ZugferdFileNotFoundException;
use \horstoeko\zugferd\tests\TestCase;
use horstoeko\zugferd\ZugferdDocument;
use \horstoeko\zugferd\ZugferdDocumentPdfReader;

class PdfReaderGeneralTest extends TestCase
{
public function testCanReadPdf(): void
{
$document = ZugferdDocumentPdfReader::readAndGuessFromFile(dirname(__FILE__) . "/../assets/InvalidPDF.pdf");
$this->assertNull($document);
}

public function testFileNotFound(): void
{
$this->expectException(ZugferdFileNotFoundException::class);
$document = ZugferdDocumentPdfReader::readAndGuessFromFile(dirname(__FILE__) . "/../assets/unknown.pdf");
}

public function testCanReadPdf2(): void
{
$document = ZugferdDocumentPdfReader::getXmlFromFile(dirname(__FILE__) . "/../assets/InvalidPDF.pdf");
$this->assertNull($document);
}

public function testFileNotFound2(): void
{
$this->expectException(ZugferdFileNotFoundException::class);
$document = ZugferdDocumentPdfReader::getXmlFromFile(dirname(__FILE__) . "/../assets/unknown.pdf");
}

public function testCanReadPdf3(): void
{
$document = ZugferdDocumentPdfReader::readAndGuessFromFile(dirname(__FILE__) . "/../assets/zugferd_2p1_EN16931_Einfach.pdf");
$this->assertNotNull($document);
$this->assertInstanceOf(ZugferdDocument::class, $document);
}

public function testCanReadPdf4(): void
{
$xmlString = ZugferdDocumentPdfReader::getXmlFromFile(dirname(__FILE__) . "/../assets/zugferd_2p1_EN16931_Einfach.pdf");
$this->assertNotNull($xmlString);
$this->assertIsString($xmlString);
$this->assertStringContainsString("<?xml version='1.0'", $xmlString);
$this->assertStringContainsString("<rsm:CrossIndustryInvoice", $xmlString);
$this->assertStringContainsString("</rsm:CrossIndustryInvoice>", $xmlString);
}
}
21 changes: 0 additions & 21 deletions tests/testcases/PdfReaderInvalidTest.php

This file was deleted.

0 comments on commit 10fc7d3

Please sign in to comment.