Customer Portal

Get Involved. Join the Conversation.

Topic

    Pramod Vasudeva Murthy
    PDF generation in CPMAnswered
    Topic posted February 17, 2018 by Pramod Vasudeva MurthyBlack Diamond: 60,000+ Points 
    377 Views, 25 Comments
    Title:
    PDF generation in CPM
    Content:

    Hi folks,

    I am trying to generated PDF using CPM. I know we can go with Addins which is little easier compared to CPMs, but I cant due to various other reasons.

    I am trying with these two libraries:

    • tcpdf
    • fpdf

    tcpdf has the format I want, which takes in html as a parameter and converts to PDF.

    But the problem is, no file operations are allowed in CPM no file_get_contentst(), file_read, file_write() etc.

    So,

    • How do we get them working?
    • Can I call a custom script and generate them from CPM?

    TIA,

    Pramod V

    Version:
    17D

    Best Comment

    Gursimran Singh Saini

    Ooh that complicates it.

    Here's what I would do:

    1. One Sync CPM to check $obj and $obj->prev. At the end of this CPM, set a field value to something.

    2. One Async CPM which has the cURL call to Custom Script.
    This CPM will be triggered through business rules.
    When Field (Field modified in step 1) Modified this Edit, then Execute CPM
    If this is Incident CPM, then you can even add another condition: If Incident Updated from Public API -> Customised Process.

    Comment

    • Gursimran Singh Saini

      Where is the HTML stored?

    • Pramod Vasudeva Murthy

      HTML is stored in a message base. I have added place holders to replace the values from actual record.

      ~VIP

    • Gursimran Singh Saini

      Please check SimpleHtmlDom.php Library in CP Core Framework folder.

      This Library has functions to load HTML from File Source or Direct String.

    • Pramod Vasudeva Murthy

      The problem is not loading html or reading from message base, it is performing file operations. Those things are disabled in CPM.

      ~VIP

    • Gursimran Singh Saini

      Im sure these file operations work in Custom Scripts.

      Call the Custom Script from CPM using cURL.

    • Pramod Vasudeva Murthy

      Do you have a sample, please?

      ~VIP

    • Gursimran Singh Saini

      Here's a sample for downloading file through Custom Script.

      if (file_exists($file)) {
          header('Content-Description: File Transfer');
          header('Content-Type: application/octet-stream');
          header('Content-Disposition: attachment; filename=' . basename($file));
          header('Content-Transfer-Encoding: binary');
          header('Expires: 0');
          header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
          header('Pragma: public');
          header('Content-Length: ' . filesize($file));
          ob_clean();
          flush();
          readfile($file);
          exit;
        }

      These are the only file operations I have used in Custom Scripts so far.

    • Pramod Vasudeva Murthy

      Thanks for this,

      How do we use this in CPM? Sorry if I am pushy!

      ~VIP

    • Gursimran Singh Saini

      No prob.

      And, not sure about file methods in CPM.
      CPM testing is difficult and time consuming.

      So most of my CPMs work by calling Custom Scripts.

    • Pramod Vasudeva Murthy

      Do you mean you embed CPM logic inside custom script and check them?

      ~VIP

    • Gursimran Singh Saini

      Yeah basically I move all CPM code in a Custom Script.
      Pass the $obj->ID in URL parameter.
      And call the Custom Script from CPM.

      It does the same thing exactly. Just that your CPM has to be Async.

    • Pramod Vasudeva Murthy

      But how do you "call" Custom script from CPM?

      Like $url = <custom_script_url>/obj/<obj_ID>

      and then you do a cURL?

      ~VIP

    • Gursimran Singh Saini

      $i_id = $incident->ID; // This example is Incident CPM

      $siteInterfaceRoql = RNCPHP\ROQL::query("Select * from SiteInterface")->next()->next();
      $interfaceName = str_replace("_", "-", $siteInterfaceRoql["DisplayName"]);
      $siteName = $siteInterfaceRoql["Name"];
      $url = "https://" . $interfaceName . ".custhelp.com/cgi-bin/" . $siteName . ".cfg/php/custom/cpm_incident_create.php?i_id=$i_id";

      // With this ROQL I generate the URL because when you create a Test site or Upgrade site, then the --tst or --upgrade will come automatically in the URL.

      load_curl();
      dl('curl_php5.so');
      $handle = curl_init($url);
      curl_setopt($handle, CURLOPT_RETURNTRANSFER, TRUE);
      curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, FALSE);
      $response = curl_exec($handle);
      $curl_error = curl_error($handle);
      curl_close($handle);

      That's it. This comes in the apply() function.

    • Pramod Vasudeva Murthy

      dl('curl_php5.so');

      what is this file? Is it equivalent to curl_init()?

      ~VIP

    • Gursimran Singh Saini

      dl('curl_php5.so') is a library required to load cURL in CPMs.

      No its not related to curl_init().