How to customize the Sugar PDF Manager

cedricmourizard —  April 25, 2016 — 3 Comments

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:&nbsp;{$fields.name}
Description:&nbsp;{$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!

3 responses to How to customize the Sugar PDF Manager

  1. 

    Thank you for the tips Cédric Mourizard. I was able to leverage the customizable sugarpdf.pdfmanager.php to include module specific css files in the document and use wkhtmltopdf instead of tcpdf for a cleaner PDF output.

    • 
      cedricmourizard April 25, 2016 at 1:18 pm

      You are very welcome, links to gist are missed on the article and matt will update to re-add them.

      • 

        Not sure why the Gist integration isn’t working. I’ve included the code in the post directly now. Apologies!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s