How can I import a funcition of other file?

How can I import a funcition of other file?

otavio_laskosk_pcotavio_laskosk_pc Posts: 21Questions: 6Answers: 0
edited February 2021 in Free community support

I am using a file only to header and footer like this:

              doc['header'] = (function (page, pages, headlines) {

                    var data = new Date();
                    var dia = data.getDate();
                    var mes = data.getMonth()+1;
                    var ano = data.getFullYear();
                    if(dia<10)
                    {
                        dia='0'+dia;
                    } 
                    if(mes<10)
                    {
                        mes='0'+mes;
                    }
                    var dataAtual = dia + '/' + mes + '/' + ano;    


                    return {
                        columns: [
                            {
                                image: test.jpg,
                                width: 100
                            },

                            {
                                fontSize: 16,
                                bold: true,
                                text:'Relatório de Comp. 668',
                                alignment: 'center',
                                margin:[10,3,-140,0]
                            },
                            {
                                text: dataAtual,
                                bold: true,
                                alignment: 'right',
                                margin: [150, 10, -107, 68]
                            },
                            {
                                text:'\n\n _______________________________________________________________________________________________________________',
                                bold:true,
                                margin: [-394, -2, 40, 4],
                                alignment: 'left'
                            }    

                        ],
                        fontSize: 10,
                        alignment: 'justify',
                        style: 'header',
                        //margin: [42, 18,55]
                        margin: [41, 6, 80, 0]
                    };
                }
                        );

                //Adiciona Rodapé no relatório 
                doc['footer'] = (function (page, pages) {
                    return {
                        columns: [

                            {
                                stack: [
                                    {text: '_______________________________________________________________________________________________________________', bold: true},
                                    {text: 'text', style: 'subheader'}
                                ],
                                style: 'e'
                            },
                            {
                                text: [
                                    '\n',
                                    {text: page.toString(), italics: true},
                                    ' / ',
                                    {text: pages.toString(), italics: true}

                                ],
                                bold: true,
                                margin: [-20, 0, 50]
                            }
                        ],
                        margin: [40, 0, 40]
                    };
                });

And I'm trying to pull it int the buttons like this:

    buttons: [

        {
        customize: function (doc) {
            var headerAndFooter = document.createElement('script');
            headerAndFooter.src = home_uri + '/views/_includes/_js/headerAndFooter.js';
            document.head.appendChild(headerAndFooter);
        }

}
]

but it's not working.

Anyone know how to do it?

Answers

  • allanallan Posts: 61,446Questions: 1Answers: 10,054 Site admin

    You need to append it to the doc parameter that is passed in (that's the new document for the print view) - doc.head.appendChild(headerAndFooter); should do it.

    Allan

  • otavio_laskosk_pcotavio_laskosk_pc Posts: 21Questions: 6Answers: 0
    edited February 2021

    Hi Allan,

    Thanks by your replied,

    I've tried it like this:

    var headerAndFooter = doc.createElement('script');
    headerAndFooter.type = "text/javascript";
    headerAndFooter.src = home_uri + '/views/_includes/_js/headerAndFooter_1.js';
    doc.head.appendChild(headerAndFooter);

    but it didn't work very well:

    the buttom keep running non-stop like this:

    [image removed upon request]

    and it don't print the report.

  • allanallan Posts: 61,446Questions: 1Answers: 10,054 Site admin

    Sounds like a Javascript error is occurring. Checking in your browser's console will confirm if that is the case or not.

    If you can link to a test case showing the issue I would be able to help debug it.

    Allan

  • otavio_laskosk_pcotavio_laskosk_pc Posts: 21Questions: 6Answers: 0

    Hi Allan,

    Thanks by your replied

    In the browser's console showed the following erros:

    Uncaught TypeError: doc.createElement is not a function
    at Object.customize (diversos.js:288)
    at s.action (buttons.html5.min.js:27)
    at q (dataTables.buttons.min.js:13)
    at HTMLButtonElement.<anonymous> (dataTables.buttons.min.js:14)
    at HTMLButtonElement.dispatch (jquery-2.2.4.js:4737)
    at HTMLButtonElement.elemData.handle (jquery-2.2.4.js:4549)
    customize @ diversos.js:288
    action @ buttons.html5.min.js:27
    q @ dataTables.buttons.min.js:13
    (anonymous) @ dataTables.buttons.min.js:14
    dispatch @ jquery-2.2.4.js:4737
    elemData.handle @ jquery-2.2.4.js:4549

    Have you ever see this errors?

  • allanallan Posts: 61,446Questions: 1Answers: 10,054 Site admin

    Try:

        {
        customize: function (win) {
            var headerAndFooter = win.document.createElement('script');
            headerAndFooter.src = home_uri + '/views/_includes/_js/headerAndFooter.js';
            win.document.head.appendChild(headerAndFooter);
        }
    

    The first parameter passed into the customize callback for the print type is a window object, not a document (which I'd forgotten before!).

    Allan

  • otavio_laskosk_pcotavio_laskosk_pc Posts: 21Questions: 6Answers: 0

    Hi Allan,

    Thanks for your replied,

    I've tried to put this command like this:

    buttons: [

            {
    
                extend: 'pdfHtml5',
                download: 'open',
                title: ' ',
                text: 'Gerar relatório PDF',
                exportOptions: {
                    columns: [0, 1, 2, 3, 4, 5, 6, 7]
                },
    
        customize: function (win) (
    
                var headerAndFooter = win.document.createElement('script');
                headerAndFooter.type = "text/javascript";
                headerAndFooter.src = home_uri + '/views/_includes/_js/headerAndFooter.js';
                win.document.head.appendChild(headerAndFooter);
    

    }
    }
    ],

    but the browser's console showed the follow errors:

    Uncaught TypeError: Cannot read property 'createElement' of undefined
    at Object.customize (diversos.js:290)
    at s.action (buttons.html5.min.js:27)
    at q (dataTables.buttons.min.js:13)
    at HTMLButtonElement.<anonymous> (dataTables.buttons.min.js:14)
    at HTMLButtonElement.dispatch (jquery-2.2.4.js:4737)
    at HTMLButtonElement.elemData.handle (jquery-2.2.4.js:4549)

    I put the source code in attachment "diversos.rar" if you want see how I'm desenvolving it.

    I'm puting this code in the wrong place?

  • allanallan Posts: 61,446Questions: 1Answers: 10,054 Site admin

    Yes, you are putting it in the pdfHtml5 button type. I had assumed (incorrectly I now see) that you wanted to use a print view output, since that is the only export file type that would accept Javascript being embedded in it.

    While the PDF format itself does accept Javascript, the pdfmake library that is used for the pdf generation doesn't, as far as I am aware, allow Javascript to be embedded.

    Allan

  • otavio_laskosk_pcotavio_laskosk_pc Posts: 21Questions: 6Answers: 0

    Hi Allan

    Thanks for your replied,

    So, There isn't a way to impor other file's command in this case?

  • allanallan Posts: 61,446Questions: 1Answers: 10,054 Site admin

    Not into a created pdf file. You'd need to use a server-side pdf exporter to get that kind of ability I suspect.

    Allan

  • otavio_laskosk_pcotavio_laskosk_pc Posts: 21Questions: 6Answers: 0

    Ok Allan,

    Thanks for your replied!

This discussion has been closed.