Skip to content

Commit

Permalink
fix: Properly apply field filling with the same structure as for offi…
Browse files Browse the repository at this point in the history
…ce documents

Signed-off-by: Julius Härtl <jus@bitgrid.net>
  • Loading branch information
juliushaertl committed Aug 2, 2024
1 parent 50696d3 commit 38bc17a
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 15 deletions.
4 changes: 2 additions & 2 deletions lib/Controller/TemplateFieldController.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ public function extractFields(int $fileId): DataResponse {
* @return DataResponse
*/
#[NoAdminRequired]
public function fillFields(int $fileId, array $fields): DataResponse {
public function fillFields(int $fileId, array $fields, ?string $destination = null): DataResponse {
try {
$this->templateFieldService->fillFields($fileId, $fields);
$this->templateFieldService->fillFields($fileId, $fields, $destination);

return new DataResponse([], Http::STATUS_OK);
} catch (\Exception $e) {
Expand Down
38 changes: 32 additions & 6 deletions lib/Service/PdfService.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,21 @@ public function extractFields(Node $file): array {

try {
$pdf = new Pdf($filePath);
$fields = $pdf->getDataFields();
$fields = $pdf->getDataFields() ?: [];
$templateFields = [];
$index = 0;
foreach ($fields as $field) {
$fieldType = FieldType::tryFrom($field['FieldType']) ?? null;
$fieldType = self::matchFieldType($field['FieldType']);

if ($fieldType === null) {
continue;
}

$templateFields[] = new Field(
$index,
$field['FieldName'],
(string)$index,
$field['FieldValue'],
$fieldType,
alias: $field['FieldName'],
);
$index++;
}
Expand All @@ -48,17 +49,42 @@ public function extractFields(Node $file): array {
}
}

public function fillFields(Node $file, array $fieldValues): void {
public function fillFields(Node $file, array $fieldValues) {
if (!$file instanceof \OCP\Files\File) {
return;
}

$filePath = $file->getStorage()->getLocalFile($file->getInternalPath());

try {
$pdf = new Pdf($filePath);
$pdf->fillForm($fieldValues);
$fields = $pdf->getDataFields();
$fillData = [];
foreach ($fieldValues as $index => $field) {
if (!isset($fields[$index])) {
continue;
}
$fieldName = $fields[$index]['FieldName'];
$fieldData = $field['content'] ?? $fields[$index]['FieldValue'];
$fillData[$fieldName] = $fieldData;
}
unset($pdf);

$pdf = new Pdf($filePath);
$pdf->fillForm($fillData);
$pdf->flatten();
$pdf->saveAs($filePath);
return file_get_contents($filePath);
} catch (\Exception $e) {
$this->logger->error('Failed to fill fields in PDF: {error}', ['error' => $e->getMessage(), 'exception' => $e]);
throw $e;
}
}

public static function matchFieldType(string $type): ?FieldType {
return match ($type) {
'Text' => FieldType::RichText,
default => null
};
}
}
34 changes: 27 additions & 7 deletions lib/Service/TemplateFieldService.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
namespace OCA\Richdocuments\Service;

use OCA\Richdocuments\AppConfig;
use OCA\Richdocuments\Capabilities;
use OCP\Files\IRootFolder;
use OCP\Files\Node;
use OCP\Files\NotFoundException;
use OCP\Files\Template\Field;
use OCP\Files\Template\FieldType;
use OCP\Http\Client\IClientService;
Expand All @@ -22,14 +24,16 @@ public function __construct(
private IRootFolder $rootFolder,
private LoggerInterface $logger,
private PdfService $pdfService,
private ?string $userId,
) {
}

/**
* @param Node|int $file
* @return array|string
* @return Field[]
* @throws NotFoundException
*/
public function extractFields(Node|int $file) {
public function extractFields(Node|int $file): array {
if (is_int($file)) {
$file = $this->rootFolder->getFirstNodeById($file);
}
Expand All @@ -38,6 +42,10 @@ public function extractFields(Node|int $file) {
return $this->pdfService->extractFields($file);
}

if (in_array($file->getMimetype(), Capabilities::MIMETYPES)) {
return [];
}

$collaboraUrl = $this->appConfig->getCollaboraUrlInternal();
$httpClient = $this->clientService->newClient();

Expand Down Expand Up @@ -85,17 +93,20 @@ public function extractFields(Node|int $file) {

/**
* @param Node|int $file
* @param array $fields
* @param array<string, array{content: string}> $fields
* @return string|resource
*/
public function fillFields(Node|int $file, array $fields = []) {
public function fillFields(Node|int $file, array $fields = [], ?string $destination = null) {
if (is_int($file)) {
$file = $this->rootFolder->getFirstNodeById($file);
}

if ($file->getMimeType() === 'application/pdf') {
$this->pdfService->fillFields($file, $fields);
return '';
$content = $this->pdfService->fillFields($file, $fields);
if ($destination !== null) {
$this->writeToDestination($destination, $content);
}
return $content;
}

$collaboraUrl = $this->appConfig->getCollaboraUrlInternal();
Expand Down Expand Up @@ -126,10 +137,19 @@ public function fillFields(Node|int $file, array $fields = []) {
$form
);

return $response->getBody();
$content = $response->getBody();
if ($destination !== null) {
$this->writeToDestination($destination, $content);
}
return $content;
} catch (\Exception $e) {
$this->logger->error($e->getMessage());
throw $e;
}
}

private function writeToDestination(string $destination, $data): void {
$userFolder = $this->rootFolder->getUserFolder($this->userId);
$userFolder->newFile($destination, $data);
}
}
1 change: 1 addition & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
</ignoreFiles>
</projectFiles>
<extraFiles>
<directory name="composer" />
<directory name="vendor" />
<ignoreFiles>
<directory name="vendor/jakub-onderka" />
Expand Down

0 comments on commit 38bc17a

Please sign in to comment.