diff --git a/lib/Controller/TemplateFieldController.php b/lib/Controller/TemplateFieldController.php index be81e9b78f..5071ba43b2 100644 --- a/lib/Controller/TemplateFieldController.php +++ b/lib/Controller/TemplateFieldController.php @@ -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) { diff --git a/lib/Service/PdfService.php b/lib/Service/PdfService.php index 6945f5e051..0e7a1d9cb3 100644 --- a/lib/Service/PdfService.php +++ b/lib/Service/PdfService.php @@ -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++; } @@ -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 + }; + } } diff --git a/lib/Service/TemplateFieldService.php b/lib/Service/TemplateFieldService.php index 1960db65d0..ae0b664f29 100644 --- a/lib/Service/TemplateFieldService.php +++ b/lib/Service/TemplateFieldService.php @@ -31,14 +31,16 @@ public function __construct( private LoggerInterface $logger, private ICacheFactory $cacheFactory, 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 (!$this->capabilitiesService->hasFormFilling()) { return []; } @@ -66,6 +68,10 @@ public function extractFields(Node|int $file) { return $fields; } + if (in_array($file->getMimetype(), Capabilities::MIMETYPES)) { + return []; + } + $collaboraUrl = $this->appConfig->getCollaboraUrlInternal(); $httpClient = $this->clientService->newClient(); @@ -115,10 +121,10 @@ public function extractFields(Node|int $file) { /** * @param Node|int $file - * @param array $fields + * @param array $fields * @return string|resource */ - public function fillFields(Node|int $file, array $fields = []) { + public function fillFields(Node|int $file, array $fields = [], ?string $destination = null) { if (!$this->capabilitiesService->hasFormFilling()) { throw new \RuntimeException('Form filling not supported by the Collabora server'); } @@ -135,8 +141,11 @@ public function fillFields(Node|int $file, array $fields = []) { } 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(); @@ -167,10 +176,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); + } } diff --git a/psalm.xml b/psalm.xml index 8f766ba7a8..e4ecde8a32 100644 --- a/psalm.xml +++ b/psalm.xml @@ -21,6 +21,7 @@ +