Generate a PDF document from a selection of table rows using the pdfmake library in Vue-tables-2

Searching for a way to generate a pdf of multiple selected rows in vue-tables-2, I came across the pdf library called pdfmake which seems promising. As someone new to this concept, I'm having difficulty figuring out how to:

  1. Integrate it into a vue-tables-2 component. Should I import it directly into the component?
  2. Create a pdf containing data from multiple selected table rows. I have access to this.checkedRows which contains the tableData content. How do I incorporate this into the pdf?

I noticed that pdfmake provides instructions on constructing datatable content, but I'm unsure how to apply this to vue-tables-2. Take a look at this pdfmake table content screenshot

If you are aware of a better pdf library option for use with vue-tables-2, please share your suggestions. Here's the code snippet I currently have...

<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>

The structure of my data is quite basic at the moment:

data() {
    return {

        tableData: [],

        checkedRows: [],

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

        options: {

        }
    }

And the method I'm using...

methods: {

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


}

Answer №1

To add pdfmake to your project, run the following command:

 npm install pdfmake --save-dev

Then, you can import and use it in your code like this:

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

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The window fails to retain the scroll position when overflow is enabled

Whenever I click on a specific div, I use jQuery to dynamically apply overflow: hidden to the html and body. $(".mydiv").on("click", function() { $("html, body").css("overflow", "hidden"); }); However, this action causes the window to scroll to the to ...

When the form is submitted, any blank inputs and their corresponding hidden fields will be disabled

I created a form that has multiple input fields, and users have the option to enter values or leave them blank. Each input field is accompanied by a hidden input field which contains a specific id unique to the corresponding visible input field. To disable ...

NodeJs: Transforming strings into UUID v4 efficiently

Suppose I have the following string: const input = '83e54feeeb444c7484b3c7a81b5ba2fd'; I want to transform it into a UUID v4 format as shown below: 83e54fee-eb44-4c74-84b3-c7a81b5ba2fd My current approach involves using a custom function: funct ...

Using Javascript in n8n to merge two JSON arrays into a single data structure

When working on a project, I extract JSON objects from the Zammad-API. One of the tickets retrieved is as follows: [ { "id": 53, "group_id": 2, "priority_id": 2, "state_id": 2, "organizati ...

jQuery .attr malfunctioning, retrieving incorrect links

I'm currently in the process of working on this page for a project $(function() { $(".modal-launcher, #modal-background").click(function() { $(".modal-content, #modal-background").toggleClass("active"); $(".vid-1i").attr("src", "l ...

Transforming HTML into a Document Object Model (DOM) for manipulation within a Node

Is it possible to convert raw HTML into a DOM/NodeList object and manipulate elements within that NodeList before converting it back into a string? Here's an example: request( url, function ( err, response, body ) { var bodyDOM = DOM.parse( body ...

In Vue, the sorting and filtering functionality is not always honored by JS-enhanced form components

Currently, I'm developing a simple to-do app using Vue. In this application, each item in a list generated by v-for consists of standard form fields like text inputs and checkboxes, as well as special form fields which are Vue components for WYSIWYG e ...

Deleting elements from arrayA that do not exist in arrayB using JavaScript

Seeking guidance on a small project of mine. For example, I have these two arrays: chosenItems = ['apple', 'banana', 'cherry', 'date', 'kiwi'] availableFruits = ['apple', 'cherry', &ap ...

A guide to accessing a prop from a child component in ReactJS

Currently, I am delving into React.js and experimenting with integrating the pokeAPI into a website project. One of the widgets I am working on involves displaying multiple pokemons along with their respective statistics. To achieve this, I have set up a ...

Utilizing Props in React to Slice and Dice Data Within a Separate Component

Currently, I am in the process of creating an about text for a profile that will include an option to expand or collapse based on its length. To achieve this, I am utilizing a function from the main home component: <AboutText text={aboutData}/> Abo ...

`Is there a way to modify the zAxis of a Paper component in Material-UI?`

Hello, I am curious about how to change the z-axis of a paper from MUI. https://i.sstatic.net/iKXLG.jpg The issue I'm facing is that the carousel is overlapping my menu and I need the menu to be on top of everything. Here is how I have it structure ...

JavaScript and HTML - specify the location in the html document where the JavaScript script will be displayed

I'm a beginner when it comes to JavaScript. I am trying to make sure that my HTML page remains unchanged while JavaScript text is displayed in a specific location without refreshing the entire page. To trigger a JavaScript function via a button on a ...

Modifying the color of an individual object in THREE.js: Step-by-step guide

I am currently working on editing a program that uses Three.js and Tween.js. Despite my efforts to find a solution both here and online, I have not been successful. The program involves creating multiple objects (cones) using THREE.Mesh, with a script that ...

Combining two elements in Angular 2

I am looking to find the intersection of two objects. My goal is to compare these objects and if they have matching values on corresponding keys, then I want to add them to a new object. obj1 = { "Projects": [ "test" ], "Companies": [ "facebook", "google ...

Simple guide on implementing React with Typescript to support both a basic set of properties and an expanded one

I'm grappling with a React wrapper component that dynamically renders specific components based on the "variant" prop. Despite my efforts, I cannot seem to get the union type working properly. Here's a snippet of the code for the wrapper componen ...

Updating Vue 3 slot content dynamically

Is there a way to dynamically modify slot content in Vue 3? I attempted to retrieve slots using useSlot, set a ref, update the ref.value (slotref.value = [...slotref.value, slotref.value[0]]), but the content remained unchanged. ...

Manipulating events through adjusting values of a JQuery-UI range-slider

My range-slider's values are being set with the code: $('...').slider( 'values', [ 0, 10000 ] );. However, I am facing an issue where this line triggers the change( event, ui ) event twice. Is there a way to ensure it only triggers ...

Find the vertices located on the "upper surfaces" of the ring using Three.js

I'm currently immersed in a project centered around creating different types of "rings" using Three.js. Each ring is formed through an algorithm I designed myself, which involves defining 4 sections placed at π, π/2, 3π/2, and 2π, then utilizing q ...

An async/await global variable in Javascript is initially defined, then ultimately becomes undefined

As I work on establishing a mongoDB endpoint with NodeJS and implementing this backend, I encounter an issue within the code. In particular, the function static async injectDB sets a global variable let restaurants that is then accessed by another function ...

Ways to enlarge image size without compromising the image resolution?

My image is in PNG format or as a blob. It has dimensions of 800px by 600px. However, when I try to resize it using various canvas methods like the one mentioned in this Stack Overflow thread: Resize image, need a good library, it loses quality. I wou ...