Forum


Replies: 1   Views: 392
Inserting image does not fail but there is no image in the document
Topic closed:
Please note this is an old forum thread. Information in this post may be out-to-date and/or erroneous.
Every phpdocx version includes new features and improvements. Previously unsupported features may have been added to newer releases, or past issues may have been corrected.
We encourage you to download the current phpdocx version and check the Documentation available.

Posted by rabol  · 09-10-2023 - 17:58

Hi

I have a API which accepts base 64 encoded data and buld a document

The document is created, all normal variables is replaced except a image / signature

 

        $validated = $request->validate(
            [
                'documentFile' => ['required', 'string'], // document comes as base64 encoded
                'placeholders' => ['sometimes', 'nullable', 'array'],
                'as-link' => ['sometimes', 'nullable', 'bool'],
                'sender_signature' => ['sometimes', 'nullable', 'string'], // signature comes as base64 encoded
            ],
        );

        try {
            $inputValues = [];
            if ($request->get('placeholders')) {
                foreach ($request->get('placeholders') as $key => $value) {
                    if (is_array($value)) {
                        foreach ($value as $k => $v) {
                            $inputValues[$k] = $v;
                        }
                    }
                }
            }

            $filename = Str::uuid()->toString().'.docx';

            // Get the file data from the request
            $encoded_file = $request->input('documentFile');

            // Decode the file
            $decoded_file = base64_decode($encoded_file);

            if (! Storage::disk('users')->put($request->user()->id.'/one-off/'.$filename, $decoded_file)) {
                return $this->sendError('Failed to store document file');
            }

            $path = Storage::disk('users')->path($request->user()->id.'/one-off/'.$filename);
            
            $docx = new CreateDocxFromTemplate($path);
            PhpdocxUtilities::$_phpdocxConfig['license']['code'] = config('authorizedoc.phpdocx.license');

            if ($request->get('sender_signature')) {
                $sender_signature_image = new WordFragment($docx, 'document');
                $sender_signature_image->addImage([
                    'src' => $request->get('sender_signature'),
                    'scaling' => 50, // we scale to 50 because the image is 500px
                ]);

                $docx->replaceVariableByWordFragment(
                    variables: [
                        'sender_signature' => $sender_signature_image,
                    ],
                    options: [
                        'type' => 'inline',
                    ]
                );
            }

            // now… get all vars from the document
            $docx->setTemplateSymbol('${', '}');
            CreateDocxFromTemplate::$regExprVariableSymbols = '\$(?:\{|[^{$]*\>\{)[^}$]*\}';

            $documentVars = $docx->getTemplateVariables();

            // Some predefined placeholders
            $inputValues['document_id'] = Str::orderedUuid();
            $docx->replaceVariableByText($inputValues);

            $options = ['target' => 'header'];
            $docx->replaceVariableByText($inputValues, $options);

            $options = ['target' => 'footer'];
            $docx->replaceVariableByText($inputValues, $options);

            // Save the filled template
            $finalDocPath = Storage::disk('users')->path($request->user()->id.'/one-off/'.Str::orderedUuid().'.docx');
            $docx->createDocx($finalDocPath);

 

I have verified that the image is valid (by saving to a file and open it)

I have verified that the docx is valid by saving it and all 'normal' variables get replaced

I don't get any errors, but the image is not inserted

I'm sure it's something simple, but I cannot see where the issue is