"Print as PDF" function via API

The CRM allows us to Print as PDF on, say a Lead. Is it possible to print PDF of a lead via the API?

SuiteCRM v7.11.19

@ohtwadi

The CRM allows us to Print as PDF on, say a Lead. Is it possible to print PDF of a lead via the API?

I that it’s possible in 2 steps using API 4.1

  • using set_entry method for creating pdf file and save as a note for the Lead.
  • using get_note_attachment for download the file.

Thanks @p.konetskiy.

Docs state that v4.1 API is for CRM version up to v.7.9.x. https://docs.suitecrm.com/developer/api/api-4_1/.
I am using v7.11.19, can I still use v4.1 API ?

@ohtwadi

API v4.1 is supported in all versions of SuiteCRM 7.x.

@p.konetskiy
Thank you. I have been successful in adding a entry to the Notes table via the API. I must be missing a step here as calling get_note_attachment endpoint gives me an empty file
{
“note_attachment”: {
“id”: “5fc1064f-739c-f104-aeb8-60dc93950e59”,
“filename”: “postman.pdf”,
“file”: “”,
“related_module_id”: “f1785a05-1365-bd96-7b22-60a93e90190e”,
“related_module_name”: “Leads”
}
}

Do I also have to use the set_note_attachment endpoint to upload the file after the set_entry call?

@ohtwadi

Do you see the file into SuiteCRM interface?

Yes, the entry does show in the UI. However, when I click to download, it says Invalid File Reference.
I noticed that Print as PDF from the UI creates a file under the uploads directory which does not happen with set_entry API call. This explains why the API response response has an empty file.

Pasting the API calls here for complete information.

curl --location -g --request POST ‘http://localhost/suitecrm/service/v4_1/rest.php?method=set_entry&input_type=JSON&response_type=JSON&rest_data={“session”:“fth1600uv0vqq8d0h1ir4t2mpf”,“module”:“Notes”,“name_value_list”:[{“name”:“date_entered”,“value”:“2021-06-29 04:20:14”},{“name”:“date_modified”,“value”:“2021-06-29 04:20:14”},{“name”:“modified_user_id”,“value”:1},{“name”:“created_by”,“value”:1},{“name”:“name”,“value”:“postman.pdf”},{“name”:“file_mime_type”,“value”:“application/pdf”},{“name”:“filename”,“value”:“postman.pdf”},{“name”:“parent_type”,“value”:“Leads”},{“name”:“parent_id”,“value”:“f1785a05-1365-bd96-7b22-60a93e90190e”}]}’

curl --location -g --request GET ‘http://localhost/suitecrm/service/v4_1/rest.php?method=get_note_attachment&input_type=JSON&response_type=JSON&rest_data={“session”:“fth1600uv0vqq8d0h1ir4t2mpf”,“id”:“5fc1064f-739c-f104-aeb8-60dc93950e59”}

Clicking Print as PDF executes index.php?entryPoint=formLetter. This ends up in modules\AOS_PDF_Templates\formLetterPdf.php which adds to Notes table + generates the PDF itself. set_entry API call never gets to this area.

Hi,

Adding a note does not generate the PDF
You need to extend the REST API and add a new API to create the PDF.
This new API will have to use a custom version of formLetterPdf.php so that it can return the contents of the PDF.

Please note that, if you are a customer of QuickCRM, it already contains that REST API extension.

@ohtwadi

I don’t recommend to do addition API.
It’s more detail about algorithm:

  1. Add to module Leads addition boolean field which default is false.
  2. Write logichook ( after_save for module Leads) which will work if the added field is true and call into the function which will be generate PDF-file. The function should be create new record in module Notes which has relationship with the Lead record. You can use the file modules/AOS_PDF_Templates/formLetterPdf.php as an example.
  3. Call API set_entry and set the value of added field true.
  4. Call API get_note_attachment for getting file.

The API can be extended in custom/service.
I didn’t mean modifying any core API file.

@blqt

OK. Yes, you can use your own API. I wrote only my opinion.

So this will generate the PDF every time a Lead is saved? That seems like bit of an overkill.

Thanks.

The documentation is doing a poor job of explaining how to extend the API. https://docs.suitecrm.com/developer/api/developer-setup-guide/customization/.
I haven’t been able to find anything on Google either. Can you shed some light on how to do this?

Edit: v4.1 docs are more helpful and I have been able to get a custom endpoint working. https://docs.suitecrm.com/developer/api/api-4_1/#_adding_custom_api_methods

@ohtwadi

I was sure that you would figure out all the details yourself. You can analyze:

  • value of the fields which was before.
if ($bean->control_field !== $bean->fetched_row['control_field']){
...
}
  • when the value was changed if you switch on audit for the field.

@ohtwadi

You can find examples for using SOAP and REST in the post: