Let the platform do the work

Adding Dynamic Images to PDF Manager Templates

Overview

This article will cover how to insert images from Sugar image field types into PDF Manager templates. 

Prerequisites

You must be an administrator or have a developer-level role access to make the necessary changes in PDF Manager.

This article contains code level customizations.

Steps To Complete

First, identify or create (See: Creating Fields) an image field for the module you are generating a pdf for. For our example, we will use a custom field name qli_image_c that exists on Quoted Line Items. Next, create an additional custom Smarty Modifiers. In this article, the custom Smarty Modifier will be called:

Next, create a custom Smarty Modifier. This function will allow us to pass the id of the image and generate the necessary prerequisites to render the image in the PDF by creating a temporary file. Please refer to the Temp Image Cleanup For our example, the Smarty function will be named tmp_image_path. The code is shown below:

./custom/include/SugarSmarty/plugins/modifier.tmp_image_path.php

  <?php

if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');

require_once('include/utils/file_utils.php');
require_once('include/upload_file.php');
require_once('include/utils.php');

function smarty_modifier_tmp_image_path($relative_image_path)
{
    if (empty($relative_image_path) || $relative_image_path === 'upload://') {
       return false;
    }

    $mime_types = array(
        "image/png" => "png",
        "image/jpeg" => "jpg"
    );

    // get the actual image id stored in upload
    $filename = explode("/", $relative_image_path);
    $filename = $filename[count($filename)-1];

    // getting mime type
    $mime_type = get_file_mime_type($relative_image_path);
    $ext = $mime_types[$mime_type];

    $upload_path = $GLOBALS['sugar_config']['upload_dir'];
    // building tmp path with filename and extension
    $new_filename = "tmp/".$filename.".".$ext;
    $full_path = $upload_path.$new_filename;

    //checking if the file exist
    if (!SugarAutoloader::fileExists($full_path)) {
        $uploadFile = new UploadFile();
        $result = $uploadFile->duplicate_file($filename, $new_filename);
    }

    //returning the full path
    return $full_path;
}

?>

Once the code is in place, you will need to navigate to Admin > Repairs and run a Quick Repair and Rebuild.

Next, use the custom modifier, tmp_image_path, in your custom PDF Manager template. To do this, click the "Insert Image" button icon.

New-Image-button 

This will launch a property popup. In the popup, set the Image URL property to be:

  {$product.qli_image_c|tmp_image_path}

The resulting popup is shown below:

Image-popup 

Once completed, your template should look similar to the image below:

New-Image-button2

You are now ready to test the generation of your pdf. 

Temp Image Cleanup

As this customization will create temporary files in ./upload/tmp/, it is highly recommended to enable the "Remove temporary files" scheduler in Admin > Schedulers.This scheduler will ensure all temporary files have been removed from the system. You can define the max lifetime of the temporary files by setting tmp_file_max_lifetime.