Skip to content
This repository has been archived by the owner on Dec 11, 2024. It is now read-only.

Commit

Permalink
dev
Browse files Browse the repository at this point in the history
  • Loading branch information
joanfabregat committed Feb 22, 2024
1 parent a3ef3ca commit 12d2f30
Show file tree
Hide file tree
Showing 13 changed files with 150 additions and 127 deletions.
2 changes: 1 addition & 1 deletion .idea/modules.xml

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

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

21 changes: 10 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@ composer require codeinc/pdf2txt-client

## Usage

This client requires a running instance of the [pdf2text](https://github.com/codeinchq/pdf2text) service. The service can be run locally [using Docker](https://hub.docker.com/r/codeinchq/pdf2text) or deployed to a server.
This client requires a running instance of the [pdf2txt](https://github.com/codeinchq/pdf2txt) service. The service can be run locally [using Docker](https://hub.docker.com/r/codeinchq/pdf2txt) or deployed to a server.

Base example:
### Base example:
```php
use CodeInc\Pdf2TextClient\Pdf2TextClient;
use CodeInc\Pdf2TextClient\Exception;
use CodeInc\Pdf2TxtClient\Pdf2TxtClient;
use CodeInc\Pdf2TxtClient\Exception;

$apiBaseUri = 'http://localhost:3000/';
$localPdfPath = '/path/to/local/file.pdf';

try {
// convert
$client = new Pdf2TextClient($apiBaseUri);
$client = new Pdf2TxtClient($apiBaseUri);
$stream = $client->convertLocalFile($localPdfPath);

// display the text
Expand All @@ -35,12 +35,11 @@ catch (Exception $e) {
}
```

With options:

### With options:
```php
use CodeInc\Pdf2TextClient\Pdf2TextClient;
use CodeInc\Pdf2TextClient\ConvertOptions;
use CodeInc\Pdf2TextClient\Format;
use CodeInc\Pdf2TxtClient\Pdf2TxtClient;
use CodeInc\Pdf2TxtClient\ConvertOptions;
use CodeInc\Pdf2TxtClient\Format;

$apiBaseUri = 'http://localhost:3000/';
$localPdfPath = '/path/to/local/file.pdf';
Expand All @@ -52,7 +51,7 @@ $convertOption = new ConvertOptions(

try {
// convert
$client = new Pdf2TextClient($apiBaseUri);
$client = new Pdf2TxtClient($apiBaseUri);
$jsonResponse = $client->convertLocalFile($localPdfPath, $convertOption);
$decodedJson = $client->processJsonResponse($jsonResponse);

Expand Down
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "codeinc/pdf2text-client",
"name": "codeinc/pdf2txt-client",
"version": "v1.0",
"description": "A PHP client for the pdf2text service",
"homepage": "https://github.com/codeinchq/pdf2text-php-client",
"description": "A PHP client for the pdf2txt service",
"homepage": "https://github.com/codeinchq/pdf2txt-php-client",
"type": "library",
"license": "MIT",
"require": {
Expand All @@ -14,12 +14,12 @@
},
"autoload": {
"psr-4": {
"CodeInc\\Pdf2TextClient\\": "src"
"CodeInc\\Pdf2TxtClient\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"CodeInc\\Pdf2TextClient\\Tests\\": "tests/"
"CodeInc\\Pdf2TxtClient\\Tests\\": "tests/"
}
},
"authors": [
Expand Down
2 changes: 1 addition & 1 deletion composer.lock

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

6 changes: 3 additions & 3 deletions src/ConvertOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@

declare(strict_types=1);

namespace CodeInc\Pdf2TextClient;
namespace CodeInc\Pdf2TxtClient;

/**
* pdf2text convert options.
* pdf2txt convert options.
*
* @see https://github.com/codeinchq/pdf2text?tab=readme-ov-file#usage
* @see https://github.com/codeinchq/pdf2txt?tab=readme-ov-file#usage
*/
final readonly class ConvertOptions
{
Expand Down
2 changes: 1 addition & 1 deletion src/Exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

declare(strict_types=1);

namespace CodeInc\Pdf2TextClient;
namespace CodeInc\Pdf2TxtClient;

use Exception as BaseException;

Expand Down
2 changes: 1 addition & 1 deletion src/Format.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace CodeInc\Pdf2TextClient;
namespace CodeInc\Pdf2TxtClient;

enum Format
{
Expand Down
6 changes: 3 additions & 3 deletions src/Pdf2TextClient.php → src/Pdf2TxtClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

declare(strict_types=1);

namespace CodeInc\Pdf2TextClient;
namespace CodeInc\Pdf2TxtClient;

use Http\Discovery\Psr17FactoryDiscovery;
use Http\Discovery\Psr18ClientDiscovery;
Expand All @@ -21,7 +21,7 @@
use Psr\Http\Message\StreamFactoryInterface;
use Psr\Http\Message\StreamInterface;

class Pdf2TextClient
class Pdf2TxtClient
{
public function __construct(
private readonly string $baseUrl,
Expand Down Expand Up @@ -57,7 +57,7 @@ public function convert(mixed $stream, ConvertOptions $options = new ConvertOpti
)
->addResource('firstPage', (string)$options->firstPage)
->addResource('normalizeWhitespace', (string)$options->normalizeWhitespace)
->addResource('raw', $options->format === Format::text ? 'true' : 'false');
->addResource('format', $options->format->name);

if ($options->lastPage !== null) {
$multipartStreamBuilder->addResource('lastPage', (string)$options->lastPage);
Expand Down
96 changes: 0 additions & 96 deletions tests/Pdf2TextClientTest.php

This file was deleted.

84 changes: 84 additions & 0 deletions tests/Pdf2TxtClientTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php
/*
* Copyright 2024 Code Inc. <https://www.codeinc.co>
*
* Use of this source code is governed by an MIT-style
* license that can be found in the LICENSE file or at
* https://opensource.org/licenses/MIT.
*/

declare(strict_types=1);

namespace CodeInc\Pdf2TxtClient\Tests;

use CodeInc\Pdf2TxtClient\ConvertOptions;
use CodeInc\Pdf2TxtClient\Exception;
use CodeInc\Pdf2TxtClient\Format;
use CodeInc\Pdf2TxtClient\Pdf2TxtClient;
use JsonException;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\StreamInterface;

final class Pdf2TxtClientTest extends TestCase
{
private const string DEFAULT_PDF2TEXT_BASE_URL = 'http://localhost:3000';
private const string TEST_PDF_PATH = __DIR__.'/assets/file.pdf';
private const string TEST_PDF_RESULT_TXT = __DIR__.'/assets/file.txt';
private const string TEST_PDF_RESULT_JSON = __DIR__.'/assets/file.json';

/**
* @throws Exception|JsonException
*/
public function testConvertLocalFileToText(): void
{
$stream = $this->getNewClient()->convertLocalFile(self::TEST_PDF_PATH);
$this->assertInstanceOf(StreamInterface::class, $stream, "The stream is not valid");

$text = (string)$stream;
$this->assertNotEmpty($text, "The stream is empty");
$this->assertStringEqualsFile(self::TEST_PDF_RESULT_TXT, $text, "The text is not valid");
}

/**
* @throws Exception|JsonException
*/
public function testConvertLocalFileToRawJson(): void
{
$client = $this->getNewClient();
$stream = $client->convertLocalFile(self::TEST_PDF_PATH, new ConvertOptions(format: Format::json));
$this->assertInstanceOf(StreamInterface::class, $stream, "The stream is not valid");

$rawJson = (string)$stream;
$this->assertJson($rawJson, "The JSON is not valid");
$this->assertStringEqualsFile(self::TEST_PDF_RESULT_JSON, $rawJson, "The JSON is not valid");
}

/**
* @throws Exception|JsonException
*/
public function testConvertLocalFileToProcessedJson(): void
{
$client = $this->getNewClient();
$stream = $client->convertLocalFile(self::TEST_PDF_PATH, new ConvertOptions(format: Format::json));
$this->assertInstanceOf(StreamInterface::class, $stream, "The stream is not valid");

$json = $client->processJsonResponse($stream);
$this->assertIsArray($json, "The processed JSON is not valid");

$expectedJson = json_decode(file_get_contents(self::TEST_PDF_RESULT_JSON), true);
$this->assertArrayIsEqualToArrayOnlyConsideringListOfKeys(
$json,
$expectedJson,
["meta", "pages"]
);
}

private function getNewClient(): Pdf2TxtClient
{
$apiBaseUrl = self::DEFAULT_PDF2TEXT_BASE_URL;
if (defined('PDF2TEXT_BASE_URL')) {
$apiBaseUrl = constant('PDF2TEXT_BASE_URL');
}
return new Pdf2TxtClient($apiBaseUrl);
}
}
Loading

0 comments on commit 12d2f30

Please sign in to comment.