print to pdf with 2 click

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • item
    Active Community Member
    • Mar 2017
    • 1476

    print to pdf with 2 click

    Hello team,

    just for win "1 click" .. it's possible to have print-to-pdf (see attachment) ?

    Kinds regards
    If you could give the project a star on GitHub. EspoCrm believe our work truly deserves more recognition. Thanks.​
  • telecastg
    Active Community Member
    • Jun 2018
    • 907

    #2
    Hello item

    This feature can be implemented by overriding the list view for the entity where you want to have the Print to Pdf button as a row action as follows:

    1) Create file: custom/Espo/Custom/Resources/metadata/clientDefs/{{name of your entity}}.json
    Code:
    {
        "recordViews": {
            "list": "custom:views/record/list-with-pdf"
        }
    
    }
    2) Create file: client/custom/src/views/record/list-with-pdf.js
    Code:
    Espo.define('custom:views/record/list-with-pdf', 'views/record/list', function (Dep) {
    
        return Dep.extend({
    
            rowActionsView: 'custom:views/record/row-actions/plus-pdf',
    
            actionPrintPdf: function () {
                this.createView('pdfTemplate', 'views/modals/select-template', {
                    entityType: this.entityType
                }, function (view) {
                    view.render();
                    this.listenToOnce(view, 'select', function (model) {
                        this.clearView('pdfTemplate');
                        window.open('?entryPoint=pdf&entityType='+this.model.name+'&entityId='+this.model.id+'&templateId=' + model.id, '_blank');
                    }, this);
                });
            }
    
        });
    });
    3) Create file: client/custom/src/views/record/row-actions/plus-pdf.js
    Code:
    Espo.define('custom:views/record/row-actions/plus-pdf', 'views/record/row-actions/default', function (Dep) {
    
        return Dep.extend({
    
            getActionList: function () {
                var list = [{
                    action: 'quickView',
                    label: 'View',
                    data: {
                        id: this.model.id
                    },
                    link: '#' + this.model.name + '/view/' + this.model.id
                }];
                if (this.options.acl.edit) {
                    list.push({
                        action: 'quickEdit',
                        label: 'Edit',
                        data: {
                            id: this.model.id
                        },
                        link: '#' + this.model.name + '/edit/' + this.model.id
                    });
                }
                if (this.options.acl.delete) {
                    list.push({
                        action: 'quickRemove',
                        label: 'Remove',
                        data: {
                            id: this.model.id
                        }
                    });
                }
                list.push({
                    action: 'printPdf',
                    label: 'Print Pdf',
                    data: {
                        entityId: this.model.id,
                        entityType: this.model.name                    
                    }
                });
                return list;
            }
    
        });
    });
    This is what it will look like for the entity or entities where you decide to add this feature, and when you click on "Print Pdf" a modal selector for a Template will display so you can select the template that you want to use for the PDF output.
    Last edited by telecastg; 04-15-2020, 05:02 PM.

    Comment


    • item
      item commented
      Editing a comment
      Thank you very much telecastg.
      Front-end is not my cup of tea

    • telecastg
      telecastg commented
      Editing a comment
      You're welcome -
  • item
    Active Community Member
    • Mar 2017
    • 1476

    #3
    Hello @telecastg,
    where i wrong : look my 2 console.log in list-with-pdf.js
    first triggert
    second .. no .. then i have a modal with nothing :s
    And i how can we have this behaviour on "list small" ?

    Thanks in advance

    PHP Code:
                    view.render();
                    console.log(data);
                    this.listenToOnce(view, 'select', function (model) {
                        this.clearView('pdfTemplate');
                        console.log(data);
                         window.open('?entryPoint=pdf&entityType='+data.entityType+'&entityId='+data.entityId+'&templateId=' + model.id, '_blank');
    
    first console.log => action: "printPdf", entitytype: "MyEntity", entityid: "5e92dabbdfd5d7f22", el: "#select-modal-container-3436", factory:....
    second condole.log => nothing 
    
    so i have a modal but nothing template to select
    If you could give the project a star on GitHub. EspoCrm believe our work truly deserves more recognition. Thanks.​

    Comment

    • telecastg
      Active Community Member
      • Jun 2018
      • 907

      #4
      I apologize item, I used the wrong definition for actionPrintPdf in client/custom/src/views/record/list-with-pdf.js but I have corrected it, please see the script above and copy the new function.

      The function that I had originally posted is a custom function that I developed for entities where I only have one template and that template id is part of the custom entity.

      The corrected function is the standard Espo function which will look for templates associated with your entity.

      The layout "list small" is normally used to list "children" in the relationship panels of a "parent", for example to list "opportunities" under an "Account".

      To apply to apply this behavior to a "list small" in a relationship panel view you would need to call the client/custom/src/views/record/row-actions/plus-pdf.js in the parent's clientDefs.

      For the above example (opportunities linked to an account) you would need to create a "custom" clientDefs script for Account like this:

      custom/Espo/Custom/Resources/metadata/clientDefs/Account.json
      Code:
      { 
          "relationshipPanels":
              "opportunities": {
                  "rowActionsView": "custom:views/record/row-actions/plus-pdf",
                  "layout": "listForAccount"
              }
      }
      Last edited by telecastg; 04-15-2020, 01:04 AM.

      Comment

      • item
        Active Community Member
        • Mar 2017
        • 1476

        #5
        Hello, telecastg

        still need some changement.. because when select a template, i think bad url (bad id ..)
        i think it's because
        this.id ... or this.model.id ..this.entityType.. this.entityId.. something so
        look url : same id and entityType is bad
        PHP Code:
        ?entryPoint=pdf&entityType=Template&entityId=5e8ef27d28900a2d3&templateId=5e8ef27d28900a2d3 
        
        Thanks

        just do this little changement in the script :

        PHP Code:
        this.createView('pdfTemplate', 'views/modals/select-template', {
                        entityType: this.entityType
                    }, function (view) 
        
        Last edited by item; 04-15-2020, 09:49 AM.
        If you could give the project a star on GitHub. EspoCrm believe our work truly deserves more recognition. Thanks.​

        Comment


        • telecastg
          telecastg commented
          Editing a comment
          Excellent !, thanks for the improvement. I have made the changes in the post.
          Last edited by telecastg; 04-16-2020, 06:32 AM.
      • ChrisSka83
        Senior Member
        • Apr 2023
        • 197

        #6
        Hello everyone,

        has anything changed in the meantime up to version 8.2.3 so that this script no longer works?

        I always get the following error message:

        PHP Code:
        Uncaught TypeError: Cannot read properties of undefined (reading 'name') 
        

        Comment


        • Kharg
          Kharg commented
          Editing a comment
          you could use link button to open the pdf template.

          Unfortunately I don't have a lot of time to look at this, but if global row actions are ever implemented in Espo I can develop an extension to add this feature.

        • ChrisSka83
          ChrisSka83 commented
          Editing a comment
          How can I use the Link button?
          I can only enter a link there.

        • Kharg
          Kharg commented
          Editing a comment
          By generating the link to the template using a formula? But you will be limited to just 1 template
      • item
        Active Community Member
        • Mar 2017
        • 1476

        #7
        Hi,
        just do that .. it's still "chaud"

        add to any clientDefs.json
        PHP Code:
            "rowActionDefs": {
                "print": {
                    "label": "Print",
                    "handler": "custom:handlers/list-row-action-print",
                    "acl": "edit"
                }
            },
            "rowActionList": [
                "print"
            ],

        and create this : (this file can certainly optimized with contexte.. this, model, view... )

        PHP Code:
        define('custom:handlers/list-row-action-print', ['action-handler','views/modal', 'model'], function (Dep) {
        
            return Dep.extend({
        
            isAvailable(model, action) {
                return true;
            },
        
            process(model, action) {
                if (action === 'print') {
                    this.print(model);
                    return;
                }
            },
        
            print: function (model) {
                this.view.createView('pdfTemplate', 'views/modals/select-template', {
                        entityType: model.entityType,
                        entityId: model.id,
                        model: model,
                    }, (view) => {
                        view.render();
        
                        view.listenToOnce(view, 'select', (model) => {
                            view.clearView('pdfTemplate');
                            window.open(
                                '?entryPoint=pdf&entityType=' +
                                view.model.entityType + '&entityId=' +
                                view.model.id + '&templateId=' + model.id, '_blank'
                            );
                        });
                    });
                },
            });
        });
        
        And then in listView, you will see "print" on row-action.
        Seemt to work well

        next job, to listSmall
        If you could give the project a star on GitHub. EspoCrm believe our work truly deserves more recognition. Thanks.​

        Comment

        • ChrisSka83
          Senior Member
          • Apr 2023
          • 197

          #8
          ok, I have now tried this with the tip from Kharg .
          Since I use 2 different templates, I had to insert 2 buttons.
          I have solved this as buttons in the list view.
          So I have one click less.

          Click image for larger version

Name:	image.png
Views:	121
Size:	5.7 KB
ID:	105765

          Comment


          • Kharg
            Kharg commented
            Editing a comment
            Looks great!
        Working...