COLOURING THE DATATABLE HEADER ON PDF EXPORT

COLOURING THE DATATABLE HEADER ON PDF EXPORT

AmbientAmbient Posts: 23Questions: 9Answers: 0
edited November 2022 in Buttons

The below code is working fine. However, when I add the commented out code in customize function to colour the pdf header on export it just leaves the export button circling and nothing happens. What am I doing wrong.
Sorry, I can't Post Code as there is reams of it.

// SAVE TO PDF FILE BELOW

   {
        extend: 'pdfHtml5',
        style: 'tableExample',  



     styles: {
       header: {
        fontSize: 28,
        color: "#7E4F29",
        bold: true


        },
       subheader: {
        fontSize: 15,
        bold: true
        },
       quote: {
        italics: true
        },
       small: {
        fontSize: 8
        }   
       },

            text: '<span class="fa fa-file-pdf-o fa-2x icon-purple"></span>', 
                            className: 'btn', titleAttr: 'Export to PDF',
            footer: true,
            orientation: 'portrait',   // Needs coded to switch if necessary to landscape
            filename: 'ProductList_' + d,
            messageTop: 'Opus Retail Solutions Product List Report.' + d,
            download: 'open',
            exportOptions: {
                             columns: ':visible',
            stripHtml: true,
                          },


            //SET CUSTOM OPTIONS FOR PDF

            customize: function(doc) {      

                  /*  doc.content[1].table.body[0].forEach(function(h) {
                                   h.fillColor = 'green';
               }); */


                  doc.watermark = {text: 'XXXXXXXXXXs', color: 'grey', opacity: 0.1};
                                  doc.pageMargins = [10,10,10,10];
                                  doc.defaultStyle.fontSize = 7;
                                  doc.styles.tableHeader.fontSize = 7;
                                  doc.styles.title.fontSize = 9;
                                 doc.content[0].text = doc.content[0].text.trim();
                                 doc.content.splice(1, 0, {
                                  margin: [0, 0, 0, 12],
                                  alignment: 'left',
                                  width: 200,
                                  height:120            
                });

        }            //END OF CUSTOMISED FUNCTION


},             

Answers

  • kthorngrenkthorngren Posts: 20,142Questions: 26Answers: 4,736
    edited November 2022

    Sorry, I can't Post Code as there is reams of it.

    You don't need to post all the code. We just need enough to see the problem. You can build just enough of the example to show the issue. Use the Download Builder to get the buttons code, etc.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    on export it just leaves the export button circling and nothing happens

    Like you are getting a Javascript error. Look at the browser's console.

    If you still need help then please build a simple test case so we can help debug.

    Kevin

  • AmbientAmbient Posts: 23Questions: 9Answers: 0

    Thank you Kevin!

  • colincolin Posts: 15,112Questions: 1Answers: 2,583

    This example from this thread is showing how to colour the headers, hopefully that'll do the trick,

    Colin

  • AmbientAmbient Posts: 23Questions: 9Answers: 0

    Thank you Colin. Tried that and my example is taken from here.
    live.datatables.net/biqaroru/461/edit

    I am not great with Java. I have tried no commas, adding ; adding nothing still I get a spinning pdf button. I know it's something really simple that I'm missing.

  • allanallan Posts: 61,443Questions: 1Answers: 10,053 Site admin

    The link you give appears to be working fine for me. We'd need a link to a page showing the error to be able to diagnose the error.

    Allan

  • AmbientAmbient Posts: 23Questions: 9Answers: 0

    Okay, I have narrowed this down to this piece of code. However, I do think that it's my data. Question is...how would I fix it?

    doc.content[1].table.body[0].forEach(function(h) {
    h.fillColor = 'green';
    });

    Getting error.

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

    This code works but when I use my data. The PDF button just sits spinning. By the way thanks very much to all those who made suggestions.

    https://jsfiddle.net/AmbientGuitar/go7fqxtm/41/

  • allanallan Posts: 61,443Questions: 1Answers: 10,053 Site admin

    You want doc.content[2].table.body[0] in your case (since you have a message as well).

    Use console.log(doc.content); tpo dump the object to your browser's console so you can inspect it.

    Allan

  • AmbientAmbient Posts: 23Questions: 9Answers: 0

    Thanks Allan, I'm still in the process of learning. Appreciate your help!

Sign In or Register to comment.