Generate PDF using Rest Call

Hi All ,

i have a custom module named order , i have added a button Print As PDF on details view page like this in

detailviewdefs.php ( /custom/mocules/o_order/metadata/detailviewdefs.php )


          4 => 
          array (
            'customCode' => '<input type="button" class="button" onClick="showPopup(\'pdf\');" value="Print As PDF">',
          ),

add i have added following lines in display function and add some function in view.detail.php



public function display(){

        $this->populateQuoteTemplates();
        $this->displayPopupHtml();
        parent::display();
    }

    function populateQuoteTemplates(){
        global $app_list_strings, $current_user;
        
        $sql = "SELECT id, name FROM aos_pdf_templates WHERE deleted=0 AND type='o_order' AND active = 1";
        
        $res = $this->bean->db->query($sql);

        $app_list_strings['template_ddown_c_list'] = array();
        while($row = $this->bean->db->fetchByAssoc($res)){
            if($row['id']){
                $app_list_strings['template_ddown_c_list'][$row['id']] = $row['name'];
            }
        }
    }

    function displayPopupHtml(){
        global $app_list_strings,$app_strings, $mod_strings;
        $templates = array_keys($app_list_strings['template_ddown_c_list']);
        if($templates){

        echo '  <div id="popupDiv_ara" style="display:none;position:fixed;top: 39%; left: 41%;opacity:1;z-index:9999;background:#FFFFFF;">
                <form id="popupForm" action="index.php?entryPoint=generatePdf" method="post">
                <table style="border: #000 solid 2px;padding-left:40px;padding-right:40px;padding-top:10px;padding-bottom:10px;font-size:110%;" >
                    <tr height="20">
                        <td colspan="2">
                        <b>'.$app_strings['LBL_SELECT_TEMPLATE'].':-</b>
                        </td>
                    </tr>';
            foreach($templates as $template){
                if(!$template){
                    continue;

                }
                $template = str_replace('^','',$template);
                $js = "document.getElementById('popupDivBack_ara').style.display='none';document.getElementById('popupDiv_ara').style.display='none';var form=document.getElementById('popupForm');if(form!=null){form.templateID.value='".$template."';form.submit();}else{alert('Error!');}";
                echo '<tr height="20">
                <td width="17" valign="center"><a href="#" onclick="'.$js.'"><img src="themes/default/images/txt_image_inline.gif" width="16" height="16" /></a></td>
                <td><b><a href="#" onclick="'.$js.'">'.$app_list_strings['template_ddown_c_list'][$template].'</a></b></td></tr>';
            }
        echo '      <input type="hidden" name="templateID" value="" />
                <input type="hidden" name="task" value="pdf" />
                <input type="hidden" name="module" value="'.$_REQUEST['module'].'" />
                <input type="hidden" name="uid" value="'.$this->bean->id.'" />
                </form>
                <tr style="height:10px;"><tr><tr><td colspan="2"><button style=" display: block;margin-left: auto;margin-right: auto" onclick="document.getElementById(\'popupDivBack_ara\').style.display=\'none\';document.getElementById(\'popupDiv_ara\').style.display=\'none\';return false;">Cancel</button></td></tr>
                </table>
                </div>
                <div id="popupDivBack_ara" onclick="this.style.display=\'none\';document.getElementById(\'popupDiv_ara\').style.display=\'none\';" style="top:0px;left:0px;position:fixed;height:100%;width:100%;background:#000000;opacity:0.5;display:none;vertical-align:middle;text-align:center;z-index:9998;">
                </div>
                <script>
                    function showPopup(task){
                        var form=document.getElementById(\'popupForm\');
                        var ppd=document.getElementById(\'popupDivBack_ara\');
                        var ppd2=document.getElementById(\'popupDiv_ara\');
                        if('.count($templates).' == 1){
                            form.task.value=task;
                            form.templateID.value=\''.$template.'\';
                            form.submit();
                        }else if(form!=null && ppd!=null && ppd2!=null){
                            ppd.style.display=\'block\';
                            ppd2.style.display=\'block\';
                            form.task.value=task;
                        }else{
                            alert(\'Error!\');
                        }
                    }
                </script>';
        }
        else{
            echo '<script>
                function showPopup(task){
                alert(\''.$mod_strings['LBL_NO_TEMPLATE'].'\');
                }
            </script>';
        }
    }

do reapir and rebuild and refresh the details view page in order module

and now if i click on Print as PDF its generating the pdf file according to pdf template

now whati want is to generate pdf from rest call

is there any methos to export the pdf from rest services ??

Thanks For your time and support ’

There are no methods that I am aware of that would let you export a pdf from a rest call. It would be possible if you wrote the method yourself.

Hi andy ,

For creating own methods i find out we need to add methods to

suitecrm/service/core/SugarWebServiceImpl.php

i have created some methods which will execute a query but for generating PDFs , if i am not wrong suiteCrm/sugarCrm usees mpdf class

so my code which generate PDFs with company logo is something like this


if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
require_once('modules/AOS_PDF_Templates/PDF_Lib/mpdf.php');

$d_image = explode('?',SugarThemeRegistry::current()->getImageURL('company_logo.png'));
$head =  '<table style="width: 100%; font-family: Arial; text-align: center;" border="0" cellpadding="2" cellspacing="2">
                <tbody style="text-align: left;">
                <tr style="text-align: left;">
                <td style="text-align: left;">
                <p><img src="'.$d_image[0].'" style="float: left;"/>&nbsp;</p>
                </td>
                <tr style="text-align: left;">
                <td style="text-align: left;"></td>
                </tr>
                 <tr style="text-align: left;">
                <td style="text-align: left;">
                </td>
                <tr style="text-align: left;">
                <td style="text-align: left;"></td>
                </tr>
                <tr style="text-align: left;">
                <td style="text-align: left;">
                <b>'.strtoupper($this->bean->name).'</b>
                </td>
                </tr>
                </tbody>
                </table><br />';

// $printable = $this->bean->build_group_report(-1,false);
$stylesheet = file_get_contents('themes/Suite7/css/style.css');
$pdf=new mPDF('en','A4','','DejaVuSansCondensed');
$pdf->setAutoFont();
$pdf->WriteHTML($stylesheet,1);
$pdf->WriteHTML($head,2);
$pdf->WriteHTML('<p><h1>Order Report</h1></p>',2);
$pdf->Output('Order Report', "D");

but i am not getting how to generate a pdf based on query ???

can you guide me on this ???

Thank you for your time and support

Look in modules/AOS_PDF_Templates/generatePdf.php

The mPDF class is called on line 126, that should show you how to generate a pdf by passing in the necessary parameters.

$printable is the main body of the pdf so you need to generate that via the data in your query.

1 Like