Announcement

Collapse
No announcement yet.

print to pdf with 2 click

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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

  • #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 -

  • #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

    Comment


    • #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


      • #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', {
                        
        entityTypethis.entityType
                    
        }, function (view
        Last edited by item; 04-15-2020, 09:49 AM.

        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.

      • #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 TypeErrorCannot read properties of undefined (reading 'name'

        Comment


        • Kharg
          Kharg commented
          Editing a comment
          try to use this.view.model.id and this.view.model.name instead of just this.model.id/name

        • ChrisSka83
          ChrisSka83 commented
          Editing a comment
          when I use this.view.model.name, it shows me the following error message:

          Uncaught TypeError: Cannot read properties of undefined (reading 'model')


          I have only changed this in the following file: list-with-pdf.js

          If I change it in all 2 files, nothing works at all.

          I always get as far as the template selection. Only when I then select a template does the error occur.
          Last edited by ChrisSka83; Yesterday, 08:25 PM.
      Working...
      X