How to customize the Sugar PDF Manager

Here is another guest post from Cédric Mourizard from the Elite SugarCRM Partner Synolia.  Cedric is a well recognized expert on the Sugar PDF Manager.

In regard to some recent questions from Sugar Community stalwart, Francesca Shiekh, we will address some questions about how to deal with the PDF Manager that was originally introduced in Sugar 6.

A Common Use Case

In this article we want to resolve this common use case for customizing the PDF manager. When following up on a meeting, you want to be able to generate an attractive summary document that lists all the meeting details included lists of those who were invited or attended.

We will customize the PDF manager so that we can print a meeting summary for any Meeting record that will have all contacts and invited users listed.

Understanding PDF Manager and extending it

Sugar uses two important 3rd party libraries as part of the PDF Manager implementation. Note that Sugar 7.6.2.1 uses TinyMCE 3.5.8 and TCPdf 4.6.013.  These are not the latest available versions of these libraries.

The architecture of the SugarPDF API is described in the Developer Guide.  It can be customized in the usual way by creating your custom code at a special location under Sugar's custom directory. We need to create a file at custom/modules/Meetings/sugarpdf/sugarpdf.pdfmanager.php where we can implement our logic to add related record information for the meeting:

<?php

require_once 'include/Sugarpdf/sugarpdf/sugarpdf.pdfmanager.php';

class MeetingsSugarpdfPdfmanager extends SugarpdfPdfmanager

{

    public function preDisplay()

    {

        parent::preDisplay();

        $previewMode = false;

        if (!empty($_REQUEST['pdf_preview']) && $_REQUEST['pdf_preview'] == 1) {

            $previewMode = true;

        }

        if ($previewMode === false) {

            // Select links to add

            $linksToLoad = array('contacts', 'users');

            foreach ($linksToLoad as $linkName) {

                /** @var Meeting $this->bean */

                $this->bean->load_relationship($linkName);

                $linkedBeans = $this->bean->$linkName->getBeans();

                // Prepare data

                $linkedData = array();

                foreach ($linkedBeans as $linkedBean) {

                    $linkedData[] = PdfManagerHelper::parseBeanFields($linkedBean, true);

                }

                // Assign data to template

                $this->ss->assign($linkName, $linkedData);

            }

        }

    }

}

Now we can create our PDF template based on the Meeting module with the PDF Manager which can be found in the Administration panel:

<h1>Meeting Summary</h1>

Topic: {$fields.name}

Description: {$fields.description}

{if isset($contacts) and $contacts|@count gt 0}

<h2>Contact Guests</h2>

<table style="width: 100%;" border="0">

<tbody>

<tr>

<td><strong>Name</strong></td>

<td>Email</td>

<td>Phone</td>

</tr>

<!-- {foreach from=$contacts item="contact"} -->

<tr>

<td>{$contact.full_name}</td>

<td>Email {$contact.email1}</td>

<td>{$contact.phone_work}</td>

</tr>

<!--  {/foreach} --></tbody>

</table>

{/if} {if isset($users) and $users|@count gt 0}

<h2>User Guests</h2>

<table style="width: 100%;" border="0">

<tbody>

<tr>

<td><strong>Name</strong></td>

<td>Email</td>

<td>Title</td>

</tr>

<!-- {foreach from=$users item="user"} -->

<tr>

<td>{$user.full_name}</td>

<td>{$user.email1}</td>

<td>{$user.title}</td>

</tr>

<!-- {/foreach} --></tbody>

</table>

{/if}

Hey! When I copy and paste this code into the WYSIWYG editor it doesn't appear the same! What is going on here?

You are right! By default TinyMCE will add an HTML p tag on every line. To avoid that you can adapt the configuration by creating a file at custom/include/tinyMCEDefaultConfig.php

<?php

$defaultConfig = array(

    'forced_root_block' => '',

);

Want to learn more about TinyMCE configuration in Sugar? Do not hesitate to read the world famous Sugar Developer Guide section on Modifying TinyMCE Editor!

Tips for your own Templates

You can follow some of these tips when you are thinking about creating a new template:

  • Use HTML comment tag to add Smarty operators where you want
  • Use inline styles instead of a CSS declaration
  • Remember that PDF generation is performed using the language selected by the current user

Do not forget to perform a Quick Repair and Rebuild to make your customizations available for the application.

Enjoy your new PDF summary for your meetings!

Anonymous
  • It is not working. 

    <?php

    require_once 'include/Sugarpdf/sugarpdf/sugarpdf.pdfmanager.php';

    class <Module>SugarpdfPdfmanager extends SugarpdfPdfmanager

    {

    public function preDisplay()

    {

    parent::preDisplay();

    $previewMode = false;

    if (!empty($_REQUEST['pdf_preview']) && $_REQUEST['pdf_preview'] == 1) {

    $previewMode = true;

    }

    if ($previewMode === false) {

    // Select links to add

    $linksToLoad = array('products');

    foreach ($linksToLoad as $linkName) {

    /** @var Meeting $this->bean */

    $this->bean->load_relationship($linkName);

    $linkedBeans = $this->bean->$linkName->getBeans();

    // Prepare data

    $linkedData = array();

    foreach ($linkedBeans as $linkedBean) {

    $linkedData[] = PdfManagerHelper::parseBeanFields($linkedBean, true);

    }

    // Assign data to template

    $this->ss->assign($linkName, $linkedData);

    }

    }

    }

    }

    Stucked here from 2 days .help me to solve this ..

    Created a Custom module I want link quoted line items in pdf manager. it has one to many relationship

    one=> custom module 

    Many=>Quoted line items

    Francesca ShiekhhatsCédric Mourizard 

  • Wow that's really a strong relationship, what a lucky guy

    By the way francesca can you help me with this issue cause im really out of ideas on how to solve this and no one replied on it. link here

  • thanks for the enlightenment francesca hope you find a good relationship partner xD

  • Those are the names of the relationships (links) between the modules Meetings and Contacts (contacts) and Meetings and Users (users). Cédric Mourizard is using those to get all the Contacts (the customers) and Users (the employees) that are in the Meeting and retrieving details about those contacts and users to put in the PDF

    See if my explanation of relationships and relationship names here helps:

    How to avoid chasing red herrings in logic hook relations? 

    FrancescaS

  • Nice tutorial very educational. Just one question 

    in the $linkToLoad variable.

                $linksToLoad = array('contacts', 'users');

    Where does the users comes from ? it's a module or field ? sorry im new in sugarcrm.