Print pdf in custom module

Hi, i have used this https://suitecrm.com/suitecrm/media/kunena/attachments/50755/AddingPrintPDFtocustomModule.pdf to create a pdf print menu to my custom module. the menu “print PDF” appears, but it still not working. when i click on nothing happens. any idea ?
thanks.

@Lahlou
Welcome to community!

Look at the discussion: Print button in subpanel
Please write if isn’t enough information there.

Hi,
this AddingPrintPDFtocustomModule.pdf was usefull .
my problem was solved after adding
require_once(‘modules/AOS_PDF_Templates/formLetter.php’);
formLetter::DVPopupHtml(‘my_custom_module’);
in ** function display()**
thanks !

I’m stuck on the same issue I added your suggestion to public function but doesn’t seem to work, even after repair and rebuild, in SuiteCRM 8 is modules/… ok or do I need public/legacy/modules/…

Ok I’m getting somewhere…

 public function display()
    {
        $this->populateQuoteTemplates();
        $this->displayPopupHtml();
		require_once('modules/AOS_PDF_Templates/formLetter.php'); formLetter::DVPopupHtml('PLNE_Planes');
        parent::display();
    }

Gets me the template popup. The PDF Parser starts, but now I get…

Warning: copy(upload//9bb5fd2b-8a83-4351-7157-627e92f74bd6_photo): failed to open stream: No such file or directory in /home/XXXXXXXXXXX/public_html/lancair/public/legacy/modules/AOS_PDF_Templates/templateParser.php on line 103
TCPDF ERROR: Some data has already been output, can't send PDF file

I have an image field in the module Line 103 in the parser refers to the URL. I’m thinking maybe the upload// with two slashes is the problem.

I tested with a image field in Contacts in SuiteCRM 7 and works with an image. Hmmm.

Ok, I’ve got this totally working if I comment out the …elseif type = Image part of the code so the image is the issue…

There’s something wrong with it. I managed to remove the first slash, so that works, however it’s not getting the file ID right either and the _photo is not the fieldname. It should be _image1 it seems to be inserting the field “type” when it should be outputting the field name. If I change it to $focus->$fieldName I get the contents of the field (the filename). Trying to get the field name.

Anyone see what’s going on here?

elseif ($field_def['type'] == 'image') {
                   $secureLink = $sugar_config['site_url'] . '/' . "public/" . $focus->id . '_' . $fieldName;
                 //   $file_location = $sugar_config['upload_dir'] . '/' . $focus->id . '_' . $fieldName;
				 $file_location = $sugar_config['upload_dir'] . '' . $focus->id . '_' . $fieldName;
                    // create a copy with correct extension by mime type
                    if (!file_exists('public')) {
                        sugar_mkdir('public', 0777);
                    }
                    if (!copy($file_location, "public/{$focus->id}".  '_' . (string)$fieldName)) {
                        $secureLink = $sugar_config['site_url'] . '/'. $file_location;
                    }

                    if (empty($focus->{$fieldName})) {
                        $repl_arr[$key . "_" . $fieldName] = "";
                    } else {
                        $link = $secureLink;
                        $repl_arr[$key . "_" . $fieldName] = '<img src="' . $link . '" width="' . $field_def['width'] . '" height="' . $field_def['height'] . '"/>';
                    }

The problem line is actual the $file_location I think.

It’s outputting:
copy(upload//9bb5fd2b-8a83-4351-7157-627e92f74bd6_photo)

It’s outputing upload//(created by userid)_(field type)

when it should be outputting:
upload/(file id)_(field name)

Strangely, in my SuiteCRM 7 installation, this works fine for a non custom module. In SuiteCRM 8 I tested with Quotes (had to find a legacy module that supports images) and images don’t work if you add to the quote header either in 8.

What do you have in site_url? Check both config.php and config_override.php.

Do you have a trailing slash there perhaps?

Thanks @pgr I checked that and is OK. The extra slash isn’t the major issue here. For some reason it’s not constructing the URL correctly. It should be upload/fieldID_fieldname and for some reason it’s constructing: upload/userid_filetype

I think it’s the $filelocation definition but just can’t seem to figure it out. Little above my PHP skills I’m not understanding completely how $focus works. $fieldName gives me the field type (photo)of the field, when I think it should be outputing the field name. If I try subbing in $focus ->$fieldName again, I’m thinking I should get the field name but get the contents of the field (the filename).

Other than image fields, I’ve pretty much got the PDF printing working for custom post types. I’d love to make a video for everyone on this one, seems pretty common, but I’m stuck on the image field not working.

It totally works with an image in SuiteCRM. 7

OK thanks for you help @pgr.

After more testing (I tried a million things to get it to work). It only worked because I copied the files into the /public/ directory. When I tried a second one, it didn’t work. Therefore, the file location is the problem. Found a bug and solved it.

in /legacy/modules/AOS_PDF_Templetes/templateParser.php

Changed:

$secureLink = $sugar_config['site_url'] . '/' . "public/" . $focus->id . '_' . $fieldName;

TO:

$secureLink = $sugar_config['site_url'] . '/' . "public/legacy/public/" . $focus->id . '_' . $fieldName;

PDF’s now show images!!!

Stay tuned for instructional video and documentation. Some of the past threads have a few errors in them I’ve cleaned up and the documentation needs to be slightly changed for SuiteCRM 8 (file locations).

Here’s the issue on Github I just created:

#96

1 Like

Hello I tried to add Print to Pdf to Tasks Module but it did not work.

I tried it on Projects and was able to get it to work but not Tasks.

OS: Linux Mint 21.3
Browser: Chrome and Firefox
Suitecrm 8.5.1

These are the steps I did for Projects

  1. Add to /modules/Project/metadata/detailviewdefs.php
          'AOS_GENLET' =>
          array(
            'customCode' => '<input type="button" class="button" onClick="showPopup();" value="{$APP.LBL_PRINT_AS_PDF}">',
          ),
  1. Add to /modules/Project/views/view.detail.php under

public function display()

        require_once('modules/AOS_PDF_Templates/formLetter.php');
        formLetter::DVPopupHtml('Project');
  1. Add to /modules/Project/language/en_us.lang.php at the end
    'LBL_PRINT_AS_PDF' => 'print PDF',

  2. Add Project under

Admin → pdf_template_type_dom

  1. Create a pdf template for Project

  2. Quick repair and rebuild

It worked.

However, when I tried doing for Tasks it didn’t work.
It’s still only showing Edit, Delete and Duplicate.
Tasks did not have view.detail.php
So I created one and added

<?php
if (!defined('sugarEntry') || !sugarEntry) {
    die('Not A Valid Entry Point');
}

class TasksViewDetail extends ViewDetail
{
    public function display()
    {
        require_once('modules/AOS_PDF_Templates/formLetter.php');
        formLetter::DVPopupHtml('Tasks');

        parent::display();
    }
}

Do I have to do additional steps since Tasks doesn’t have the file view.detail.php?