Search code examples
javascriptvue.jsvuejs2vue-componentvue-tables-2

Vue-tables-2 create .pdf (pdfmake) 'malformed table row' error


I am close to being able to download a .pdf file of multiple selected (checkboxes) rows in vue-tables-2 datatables.

In my code I am pushing this.checkedRows to table.body for both the headers columns and this.checkedRows content .... a checkbox input and 'sku' which is a string. Here is my code:

<template>
    <el-container>
        <side-nav></side-nav>
        <el-main>
            <v-server-table url="/getListings" :data="tableData" :columns="columns" :options="options">
                <input slot="selected" slot-scope="props" type="checkbox" :checked="props.row.selected" v-model="checkedRows" :value="props.row">
                <button slot="afterFilter" type="button" class="btn btn-primary btn-pdf" @click="createPDF">Create Purchase Order</button>
            </v-server-table>
        </el-main>
    </el-container>
</template>


<script>

import {ServerTable, Event} from 'vue-tables-2';    
Vue.use(ServerTable, {}, false, 'bootstrap4');

var pdfMake = require('pdfmake/build/pdfmake.js');
    var pdfFonts = require('pdfmake/build/vfs_fonts.js');
    pdfMake.vfs = pdfFonts.pdfMake.vfs;

export default {
    data() {
        return {
            toggle:[],
            tableData: [],
            checkedRows: [],
            columns: [
                'selected',
                'sku',
            ],     
            options: {

            }
        }
    },

    methods: {

        createPDF() {
            var docDefinition = {
            content: [
                {
                    table: {
                        headerRows: 1,
                        widths: [ 'auto', 'auto' ],

                        body: [

                        ]
                    }
                }
            ]
            };
            docDefinition.content[0].table.body.push(this.columns);
            for(var i=0;i<this.checkedRows.length;i++){
                docDefinition.content[0].table.body.push(Object.values(this.checkedRows[i]));  
            }
            pdfMake.createPdf(docDefinition).download('PO.pdf');
        }            
    }
}

</script>

The issue is in the mismatch of 2 header columns 'selected', and 'sku' and 1 column of row data {'sku#'} where I am getting a 'malformed table row error' as well as a 'cell is undefined' error. Here are the errors:

enter image description here

enter image description here

enter image description here

I actually don't need the checkbox selected column in the .pdf, but I don't know how to only include the needed header columns like sku etc., and leave out the selected header column.


Solution

  • In your case you need the following solution :

    docDefinition.content[0].table.body.push(["sku"]);
            for(var i=0;i<this.checkedRows.length;i++){
    
          docDefinition.content[0].table.body.push([this.checkedRows[i]["sku"]]);  
            }
    

    but in general you needd that :

    docDefinition.content[0].table.body.push(["col1","col2",..,"coln"]);
            for(var i=0;i<this.checkedRows.length;i++){
    
          docDefinition.content[0].table.body.push([this.checkedRows[i]["col1"],this.checkedRows[i]["col2"],...,this.checkedRows[i]["coln"]]);  
            }