Eric Ransom
Eric Ransom

Reputation: 69

Vue-tables-2 create pdf file from multiple selected table rows using pdfmake library

I would like to create a pdf of multiple selected rows within vue-tables-2. I have found a pdf library called pdfmake which looks great. Being new to this, I am struggling to see how I can:

  1. bring this into a vue-tables-2 component. Do I import this in the component?
  2. how would I create a pdf of multiple selected table row data? I have this.checkedRows for the tableData content. How do I get this into the pdf?

I see how pdfmake has instructions to build out datatable content, but how can I make this work with vue-tables-2? pdfmake table content screenshot

If anyone knows a better pdf library for vue-tables-2 please let me know. Here is my code so far...

<v-server-table url="/removals" :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="submit" @click="createPDF">Create PDF</button>

 </v-server-table>

My data content is just a very simple prototype right now:

data() {
    return {

        tableData: [],

        checkedRows: [],

        columns: [
            'selected',
            'sku',
        ],

        options: {

        }
    }

And my method...

methods: {

    createPDF() {
        pdfMake.createPdf(docDefinition).download('PO.pdf');
    }


}

Upvotes: 2

Views: 4095

Answers (1)

Boussadjra Brahim
Boussadjra Brahim

Reputation: 1

you could install pdfmake using the following command :

 npm install pdfmake --save-dev

and import and use it as follow

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

    export default{ 

        data() {
            return {
              tableData: [],
              checkedRows: [],
              columns: [
                   'selected',
                   'sku',
               ],

            options: {

                     }
           },
         methods: {

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

                    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');
    }



    }
     }

Upvotes: 3

Related Questions