Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
5.6 kB
2
Indexable
Never
const fs = require('fs');
const path = require('path');

// Function to remove all files in a directory
function removeAllFilesInFolder(folderPath) {
    // Read the contents of the directory
    fs.readdir(folderPath, (err, files) => {
        if (err) {
            console.error('Error reading directory:', err);
            return;
        }

        // Iterate through each file
        files.forEach(file => {
            // Construct the full path to the file
            const filePath = path.join(folderPath, file);

            // Remove the file
            fs.unlink(filePath, err => {
                if (err) {
                    console.error('Error deleting file:', filePath, err);
                    return;
                }
            });
        });
    });
}


const {jsPDF} = require('jspdf');
const autoTable = require('jspdf-autotable');

const doc = new jsPDF()


const generatePdfBufferArray = (headData = [], bodyData = []) => {
    autoTable.default(doc, {
        body: [
          [
            {
              content: 'Snapezee',
              styles: {
                halign: 'left',
                fontSize: 20,
                textColor: '#ffffff'
              }
            },
            {
              content: 'Invoice',
              styles: {
                halign: 'right',
                fontSize: 20,
                textColor: '#ffffff'
              }
            }
          ],
        ],
        theme: 'plain',
        styles: {
          fillColor: '#3366ff'
        }
      });
  
      autoTable.default(doc, {
        body: [
          [
            {
              content: 'Reference: #INV0001'
              +'\nDate: 2022-01-27'
              +'\nInvoice number: 123456',
              styles: {
                halign: 'right'
              }
            }
          ],
        ],
        theme: 'plain'
      });
  
      autoTable.default(doc, {
        body: [
          [
            {
              content: 'Billed to:'
              +'\nJohn Doe'
              +'\nBilling Address line 1'
              +'\nBilling Address line 2'
              +'\nZip code - City'
              +'\nCountry',
              styles: {
                halign: 'left'
              }
            },
            {
              content: 'From:'
              +'\nCompany name'
              +'\nShipping Address line 1'
              +'\nShipping Address line 2'
              +'\nZip code - City'
              +'\nCountry',
              styles: {
                halign: 'right'
              }
            }
          ],
        ],
        theme: 'plain'
      });
  
      autoTable.default(doc, {
        body: [
          [
            {
              content: 'Amount due:',
              styles: {
                halign:'right',
                fontSize: 14
              }
            }
          ],
          [
            {
              content: '$4000',
              styles: {
                halign:'right',
                fontSize: 20,
                textColor: '#3366ff'
              }
            }
          ],
          [
            {
              content: 'Due date: 2022-02-01',
              styles: {
                halign:'right'
              }
            }
          ]
        ],
        theme: 'plain'
      });
  
      autoTable.default(doc, {
        body: [
          [
            {
              content: 'Products & Services',
              styles: {
                halign:'left',
                fontSize: 14
              }
            }
          ]
        ],
        theme: 'plain'
      });
  
      autoTable.default(doc, {
        head: [headData],
        body: bodyData,
        theme: 'striped',
        headStyles:{
          fillColor: '#343a40',
          halign : "center"
        },
        bodyStyles : {
            halign : "center"
        }
      });
  
      autoTable.default(doc, {
        body: [
          [
            {
              content: 'Subtotal:',
              styles:{
                halign:'right'
              }
            },
            {
              content: '$3600',
              styles:{
                halign:'right'
              }
            },
          ],
          [
            {
              content: 'Total tax:',
              styles:{
                halign:'right'
              }
            },
            {
              content: '$400',
              styles:{
                halign:'right'
              }
            },
          ],
          [
            {
              content: 'Total amount:',
              styles:{
                halign:'right'
              }
            },
            {
              content: '$4000',
              styles:{
                halign:'right'
              }
            },
          ],
        ],
        theme: 'plain'
      });

  
    //   autoTable.default(doc, {
    //     body: [
    //       [
    //         {
    //           content: 'This is a centered footer',
    //           styles: {
    //             halign: 'center'
    //           }
    //         }
    //       ]
    //     ],
    //     theme: "plain"
    //   });
  
    doc.save("table.pdf");

    const pdfFileBuffer = fs.readFileSync(path.join(__dirname, 'table.pdf'));
    return pdfFileBuffer;
}

module.exports = {
    removeAllFilesInFolder,
    generatePdfBufferArray
}
Leave a Comment