string replace inside of a pdf template?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jamie
    Senior Member
    • Aug 2025
    • 274

    #1

    string replace inside of a pdf template?

    Is it possible to use something like
    string\replace('Hello {test}', '{test}', 'world')

    inside of a pdf template?
  • victor
    Active Community Member
    • Aug 2022
    • 1172

    #2
    This topic has been discussed before in many threads. Directly in the PDF template - no.
    - You need to calculate your formula in Administration > Entity Manager > Entity_name > Formula > Before Save Custom Script.
    - Put the result in some physical created field.
    - And only then insert the placeholder of this field into your PDF template.

    Comment

    • jamie
      Senior Member
      • Aug 2025
      • 274

      #3
      Originally posted by victor
      This topic has been discussed before in many threads. Directly in the PDF template - no.
      - You need to calculate your formula in Administration > Entity Manager > Entity_name > Formula > Before Save Custom Script.
      - Put the result in some physical created field.
      - And only then insert the placeholder of this field into your PDF template.
      sounds a lot like a missing feature.. but ok so its complicated, i'll just rearrange the data

      Comment

      • rabii
        Active Community Member
        • Jun 2016
        • 1392

        #4
        You can easily build something like this using templateHelpers.json metadata - below a working example i have used in a project in the past

        Step 1 — Register the Helper

        Create this file:

        custom/Espo/Custom/Resources/metadata/app/templateHelpers.json

        PHP Code:
        {
            "replace": "Espo\\Custom\\Classes\\TemplateHelpers\\ReplaceHelper"
        } 
        

        Step 2 — Write the Helper Class

        Create:

        custom/Espo/Custom/TemplateHelpers/ReplaceHelper.php

        PHP Code:
        <?php
        
        namespace Espo\Custom\Classes\TemplateHelpers;
        
        use Espo\Core\Htmlizer\Helper;
        use Espo\Core\Htmlizer\Helper\Data;
        use Espo\Core\Htmlizer\Helper\Result;
        
        class ReplaceHelper implements Helper
        {
        
            public function render(Data $data): Result
            {
                $args = $data->getArgumentList();
        
                if (empty($args)) {
                    return Result::create('');
                }
        
                $text = $args[0] ?? null;
        
                if ($text === '' || !is_string($text)) {
                    return Result::createEmpty();
                }
        
                $search  = (string) $data->getOption('search');
                $replace = (string) $data->getOption('replace');
                $caseInsensitive = (bool) $data->getOption('insensitive');
        
                if ($caseInsensitive) {
                    $output = str_ireplace($search, $replace, $text);
                } else {
                    $output = str_replace($search, $replace, $text);
                }
        
                return Result::create($output);
            }
        }

        Then clear cache and rebuild and you should be able to use this in the pdf templates - usage examples below


        Example 1 - Simple Replacement (case insensitive false)

        PHP Code:
        {{replace 'Hello WORLD' search='WORLD' replace='EspoCRM'}} 
        

        → Outputs: Hello EspoCRM


        Example 2 - Simple Replacement (case insensitive false)

        PHP Code:
        {{replace 'Hello world' search='WORLD' replace='Mate' insensitive=true}} 
        

        → Outputs: Hello Mate


        Example 3 - Using Field Values e.g pdf template for account entity

        PHP Code:
        {{replace name search='Inc.' replace='Ltd.'}} 
        

        → Outputs account name with text replaced. If company name is (Espocrm Inc.) then the output will be (Espocrm Ltd.)

        Hope this help

        Cheers
        Rabii
        EspoCRM Custom Development

        🔗 Portfolio & Builds

        Comment


        • jamie
          jamie commented
          Editing a comment
          That's a wonderful, easy-to-follow instruction. The only change i will do is change the name to "textReplace."

        • rabii
          rabii commented
          Editing a comment
          textReplace is better than replace

          cheers
          Last edited by rabii; 02-06-2026, 12:37 PM.
      Working...