PDF reading with python
This commit is contained in:
21
api/common/Controller/Import.php
Normal file
21
api/common/Controller/Import.php
Normal file
@ -0,0 +1,21 @@
|
||||
<?php
|
||||
namespace Contabilidad\Common\Controller;
|
||||
|
||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
use Psr\Http\Message\ResponseInterface as Response;
|
||||
use ProVM\Common\Define\Controller\Json;
|
||||
use ProVM\Common\Factory\Model as Factory;
|
||||
use Contabilidad\Common\Service\PdfHandler;
|
||||
|
||||
class Import {
|
||||
use Json;
|
||||
|
||||
public function __invoke(Request $request, Response $response, Factory $factory): Response {
|
||||
$post = $request->getParsedBody();
|
||||
return $this->withJson($response, $post);
|
||||
}
|
||||
public function uploads(Request $request, Response $response, PdfHandler $handler): Response {
|
||||
$output = $handler->load();
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
}
|
30
api/common/Service/PdfHandler.php
Normal file
30
api/common/Service/PdfHandler.php
Normal file
@ -0,0 +1,30 @@
|
||||
<?php
|
||||
namespace Contabilidad\Common\Service;
|
||||
|
||||
use GuzzleHttp\Client;
|
||||
|
||||
class PdfHandler {
|
||||
protected Client $client;
|
||||
protected string $folder;
|
||||
protected string $url;
|
||||
public function __construct(Client $client, string $pdf_folder, string $url) {
|
||||
$this->client = $client;
|
||||
$this->folder = $pdf_folder;
|
||||
$this->url = $url;
|
||||
}
|
||||
|
||||
public function load(): ?array {
|
||||
$folder = $this->folder;
|
||||
$files = new \DirectoryIterator($folder);
|
||||
$output = [];
|
||||
foreach ($files as $file) {
|
||||
if ($file->isDir() or $file->getExtension() != 'pdf') {
|
||||
continue;
|
||||
}
|
||||
$output []= ['filename' => $file->getBasename()];
|
||||
}
|
||||
$response = $this->client->post($this->url, ['json' => ['files' => $output]]);
|
||||
!d(json_decode($response->getBody()));
|
||||
return $output;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user