AnsweredAssumed Answered

Sugar PDF generator sometimes fails

Question asked by Kristjan Geir Mathiesen on Aug 25, 2017
Latest reply on Aug 25, 2017 by Kristjan Geir Mathiesen

Hi all.

 

We just created our custom module - Meeting Minutes. This module uses the Sugar PDF generator to produce PDF documents:

We´ve mainly be using these guidelines -> http://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_7.8/Architecture/SugarPDF/Generating_PDFs/

 

What happens: For some users, clicking on the "Generate Report" button takes them back to List View.

What should happen: A PDF document should appear on the screen in a new tab, ready for downloading or whatever.

 

The code we are using in record.js to generate the PDF:

        handleGenerate: function () {
                // Gather information needed for the report
                var allFields = []
                for(var fieldnr in this.fields) {
                        // exclude undefined fields
                        if(this.fields[fieldnr].label && this.fields[fieldnr].value){
                                var field = {};
                                field.label = this.fields[fieldnr].label;
                                if(field.label != "Minutes"){
                                        if(field.label == "Teams" || field.label == "Tags"){
                                                var names = "";
                                                for(var valuenr in this.fields[fieldnr].value){
                                                        if(names.length > 0)
                                                                names = names + ", " + this.fields[fieldnr].value[valuenr].name;
                                                        else
                                                                names = this.fields[fieldnr].value[valuenr].name;
                                                }
                                                field.value = names;
                                        } else {
                                                field.value = this.fields[fieldnr].value;
                                        }
                                        allFields.push(field);
                                }
                        }
                }


                // Fetch rich content
                var minutes = "";
                for(var editFieldNr in this.editableFields){
                        if(this.editableFields[editFieldNr].name == "rich_content_c"){
                                var richField = this.editableFields[editFieldNr];
                                minutes = new XMLSerializer().serializeToString(richField.$el["0"].children.rich_content_c.contentDocument);
                        }
                }


                // Generate the report
                var contents = "";
                var isSubjectSet = false;
                var isEmptyLine = true;
                for(var index in allFields){
                        if(isSubjectSet){
                                if(isEmptyLine)
                                        contents = contents + "<tr>";

                                contents = contents + "<td><p style='' data-mce-style='font-size: 11'>" + allFields[index].label + ": " + allFields[index].value + "</p></td>";

                                if(!isEmptyLine)
                                        contents = contents + "</tr>";

                                isEmptyLine = !isEmptyLine;
                        } else {
                                contents = contents + "<subject>" + allFields[index].value + "</subject><table>";
                                isSubjectSet = true;
                        }
                }
                contents = contents + "</table></br>" + this.getBody(minutes);

                // Replace ampersands with escaped version
                contents = contents.replace("&", "%26");

                // Display the report
                window.open('https://{our server}/index.php?module=MN_meeting_minutes&action=sugarpdf&sugarpdf=minutes&contents=' + contents, '_blank');

        },

        getBody: function(html) {
                //Only get the contents within the <body> tag
                var bodySplit = html.split(/^(.*<body.*?)>/);
                var afterBody = bodySplit[bodySplit.length-1];
                var final = afterBody.split("</body>")[0];
                return final;
        },
})

Thanks,

KGM

Outcomes