Announcement

Collapse
No announcement yet.

Get Image From Current User In PDF Template

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Get Image From Current User In PDF Template

    Hi All!

    I need to get the "Current Users" image within a PDF template. I was curious how I can do this as when using a general PDF template there's no related record like if I were to build the template for a specific module.

    Normally I would do something like this {{imageTag currentUser.UserImageId width=95 }}

    Any help is appreciated!

  • #2
    Hi dodom2

    This won't work as there is no currentUser.userImage. You will need to create your own pdf helper to do it, follow steps below and it should allow you get the current user avatar (you change the avatarId to any other image field name you are using for your user, i just assumed that this will be for the avatar).

    1 - Create a file custom/Espo/Custom/Resources/metadata/app/templateHelpers.json
    PHP Code:
    {
        
    "currentUserAvatar""Espo\\Custom\\TemplateHelpers\\CurrentUserAvatar"
    }​ 


    2 - Create a file custom/Espo/Custom/TemplateHelpers/CurrentUserAvatar.php
    PHP Code:
    <?php
    namespace Espo\Custom\TemplateHelpers;

    use 
    Espo\Core\Htmlizer\Helper;
    use 
    Espo\Core\Htmlizer\Helper\Data;
    use 
    Espo\Core\Htmlizer\Helper\Result;
    use 
    Espo\Entities\User;

    class 
    CurrentUserAvatar implements Helper
    {
        public function 
    __construct(
            Private 
    User $user
        
    ) {}

        public function 
    render(Data $data): Result
        
    {
            
    $width $data->getOption('width') ?? null;        
            
    $height $data->getOption('height') ?? null;        

            
    $attributesPart "";

            if (
    $width) {
                
    $attributesPart .= " width="" .strval($width) . """;
            }

            if (
    $height) {
                
    $attributesPart .= " height="" .strval($height) . """;
            }

            
    $id $this->user->get('avatarId');

            if (!
    $id) {
                return 
    Result::createEmpty();
            }

            
    $html "<img src="?entryPoint=attachment&id={$id}"{$attributesPart}>";


            return 
    Result::createSafeString($html);
        }
    }

    And here is how you can use it (width and high are optional but give you control if you want to)

    PHP Code:
    {{currentUserAvatar width=50 height=50}} 

    This should do the job for you
    Last edited by rabii; 03-08-2024, 01:00 PM. Reason: Update

    Comment

    Working...
    X