getting this error "Cannot read properties of undefined (reading 'table')" when set title= '' in

getting this error "Cannot read properties of undefined (reading 'table')" when set title= '' in

balaramLCSbalaramLCS Posts: 1Questions: 1Answers: 0
edited February 2023 in Free community support

When I'm set title='' in pdfHtml5 export getting this error ERROR TypeError: Cannot read properties of undefined (reading 'table')

my code as following for PDF export

{
            extend: "pdfHtml5",
            title: '',
            filename: function (){
              
              $(".export-txt2").on('change', function() {
                  return $("#repHdr1").val() + '\n' +  $("#repHdr2").val();
                  // return "filename"
                  
              })

              return $("#repHdr1").val() + '\n' +  $("#repHdr2").val();
              // return "change filename";
            },
            className: "btn btn-primary",
            orientation: 'landscape',
            pageSize: 'A4',
            exportOptions: {
              columns: ':visible',
              search: 'applied',
              order: 'applied',
            },
            customize: (doc) => {
              doc.layout = 'lightHorizotalLines'
              // doc.content[1].table.widths = Array(doc.content[1].table.body[0].length + 1).join('*').split('');
              let colCount = $("#datatable tr th").length;
              let width = [];
              let percentage = 100 / colCount;
              for (let index = 0; index < colCount; index++) {
                width.push((percentage) + "%");
              }
              doc.content[1].table.widths = width;

              doc.defaultStyle.alignment = 'center';
              doc.styles.tableHeader.alignment = 'center';
              //pageMargins [left, top, right, bottom] 
              //----------------------
              doc.pageMargins = [10, 25, 10, 25];
              // doc.pageMargins = [10, 20, 10, 20];
              var now = new Date();
              var jsDate = now.getDate() + '-' + (now.getMonth() + 1) + '-' + now.getFullYear();
              // Set the font size fot the entire document
              doc.defaultStyle.fontSize = 10;
              // console.log("pdf style: ", doc)
              // Set the fontsize for the table header
              doc.styles.tableHeader.fontSize = 12;
              doc.styles.title.fontSize = 12;

               //----------------------
               this.hedAllPage ? doc['header'] = (function (page, pages) {
                return {
                  columns: [
                    {
                      alignment: 'center',
                      text: page == 1? "heder\ntitle" : $("#repHdr1").val() + '\n' + $("#repHdr2").val(),
                      fontSize: page == 10? 0 : 10,
                    }
                  ],
                  margin: page == 1? [10,0,10,0] : [10,0,10,0],
                }
              }): "";



              doc['footer'] = (function (page, pages) {
                return {
                  columns: [
                    {
                      alignment: 'left',
                      text: ['Created on: ', { text: jsDate.toString() }]
                    },
                    {
                      alignment: 'right',
                      text: ['page ', { text: page.toString() }, ' of ', { text: pages.toString() }]
                    }
                  ],
                  margin: 10,
                }
              });
            },

Edited by Colin - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

Answers

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    We're happy to take a look, but as per the forum rules, please link to a test case - a test case that replicates the issue will ensure you'll get a quick and accurate response. Information on how to create a test case (if you aren't able to link to the page you are working on) is available here.

    Cheers,

    Colin

  • DrkSoulDrkSoul Posts: 1Questions: 0Answers: 0
    edited September 6

    Good evening, I have a similar error when I want to export to PDF, I use datatable 5

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

    function Load_Tabla_Orden() {
    var exportColumns = [0, 1, 2, 3, 4, 5, 6, 7, 8];

    var table = $('#<%=gvListado.ClientID%>').DataTable({
      dom: "<'row'<'col-sm-4'l><'col-sm-4 text-center'B><'col-sm-4'f>>" +
        "<'row'<'col-sm-12'tr>>" +
        "<'row'<'col-sm-8'i><'col-sm-4'p>>"
      ,
      buttons: [{
        extend: 'copyHtml5',
        text: '<i class="fa-solid fa-copy"></i>',
        titleAttr: 'Copiar',
        exportOptions: { columns: exportColumns }
      },
      {
        extend: 'excelHtml5',
        text: '<i class="fa-solid fa-file-excel"></i>',
        titleAttr: 'Excel',
        title: 'Exportación Excel',
        exportOptions: { columns: exportColumns }
      },
      {
        extend: 'csvHtml5',
        text: '<i class="fa-solid fa-file-csv"></i>',
        titleAttr: 'CSV',
        title: 'Exportación CSV',
        exportOptions: { columns: exportColumns }
      },
      {
        extend: 'pdfHtml5',
        text: '<i class="fa-solid fa-file-pdf"></i>',
        titleAttr: 'PDF',
        orientation: 'portrait', // Posición horizontal - landscape
        pageSize: 'A4',
        messageTop: 'PDF creado desde Mercurio.',
        title: 'Exportación PDF',
        exportOptions: {
          columns: exportColumns,
          format: {
            body: function (data, row, column, node) {
              // Reemplazar &nbsp; por un valor vacío
              return data === '&nbsp;' ? '' : data;
            }
          }
        },
        customize: function (doc) {
          if (doc.content && doc.content[1] && doc.content[1].table) {
            var table = doc.content[1].table;
            if (table.widths) {
              table.widths = Array(exportColumns.length).fill('*'); // Ajustar el ancho de las columnas
            }
          }
          doc.styles.tableHeader.fontSize = 10; // Tamaño de fuente del encabezado
          doc.defaultStyle.fontSize = 9; // Tamaño de fuente del contenido
          doc.pageOrientation = 'portrait'; // Orientación horizontal
        }
    
    
      }
      ],
      "lengthMenu": [[10, 25, 50, 100], [10, 25, 50, 100]],
      "pageLength": 10,
      "deferRender": true,
      "colReorder": true,
      //"scrollX": true,
      "language": { url: "/Contenidos/DataTables_5/Spanish.json" }
    });
    

    }

  • allanallan Posts: 63,512Questions: 1Answers: 10,472 Site admin

    As Colin stated, please link to a test case showing the issue so we can debug it.

    Does it work if you remove your customize function?

    Allan

Sign In or Register to comment.