Untitled

 avatar
unknown
plain_text
a year ago
11 kB
6
Indexable
const fs = require('fs');
const path = require('path');

const { jsPDF } = require('jspdf');
const { default: autoTable } = require("jspdf-autotable");
const {generateDateFormate }= require("./dateTimeFormater");


// 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 generatePdfBufferArray = (headData = [], bodyData = [], savedOrder, mobileUser, customerAddress, vatPercentage, defaultCurrency, companyAddress, companyEmail ) => {
    const doc = new jsPDF();

    autoTable(doc, {
        body: [
          [
              {
                  content: 'Invoice',
                  styles: {
                    halign: 'left',
                    fontSize: 20,
                    textColor: '#000000',
                    font : "courier",
                    fontStyle : "bold"
                  }
                },
            {
              content: ` <img src="./../Uploads/favicon.ico" alt="Girl in a jacket" width="500" height="600">
              `,
              styles: {
                halign: 'right',
                fontSize: 20,
                textColor: '#000000',
                font : "courier",
                fontStyle : "bold"
              }
            },
          ],
        ],
        theme: 'plain',
      });
      
      autoTable(doc, {
        body: [
          [
            {
              content: `Invoice number ${savedOrder?.orderNumber}` 
              +`\nDate of issue ${generateDateFormate()}`
              +`\nDate due ${generateDateFormate()}`,
              styles: {
                halign: 'left',
                font : "courier",
                textColor : '#000000',
                fontSize : 11
              }
            }
          ],
        ],
        theme: 'plain'
      });
      
      autoTable(doc, {
        body: [
          [
            {
              content: 'SnapeZee',
              styles: {
                halign: 'left',
                font : "courier",
                textColor : '#000000',
                fontSize : 11,
                fontStyle : 'bold'
              }
            },
            {
              content: 'Bill to',
              styles: {
                halign: 'left',
                font : "courier",
                textColor : '#000000',
                fontSize : 11,
                fontStyle : 'bold',
              }
            },
          ],
          [
              {
                  content: `${companyAddress}`
                  +`\n${companyEmail}`,
                  styles: {
                    halign: 'left',
                    font : "courier",
                    textColor : '#000000',
                    fontSize : 11,
                  }
                },
                {
                  content: `${mobileUser?.email}`
                  +`\n${ customerAddress ? customerAddress : 'Digital'}`,
                  styles: {
                    halign: 'left',
                    font : "courier",
                    textColor : '#000000',
                    fontSize : 11,
                  }
                },
          ],
        ],
        theme: 'plain'
      });
      
      
      autoTable(doc, {
        head: [headData],
        body: bodyData,
        theme: 'grid',
        styles : {
          font : 'courier',
        },
        headStyles : {
          fillColor : "#fffff",
          halign : 'center'
        },
        columnStyles: { 
          0 : {halign : 'center', textColor : '#000000'},
          1 : {halign : 'left', textColor : '#000000'},
          2 : {halign : 'center', textColor : '#000000'},
          3 : {halign : 'center', textColor : '#000000'},
          4 : {halign : 'center', textColor : '#000000'},
          5 : {halign : 'center', textColor : '#000000'}
         }
      });
      
      autoTable(doc, {
        body: [
          [
            {
              content: 'Subtotal',
              styles:{
                halign:'right',
                font : 'courier',
                textColor : '#000000',
                cellPadding : {right : 14},
              }
            },
            {
              content: `${savedOrder?.subtotal} (${defaultCurrency})`,
              styles:{
                halign:'right',
                font : 'courier',
                textColor : '#000000',
              }
            },
          ],
          [
            {
              content: `Total GST (${vatPercentage}%)`,
              styles:{
                halign:'right',
                font : 'courier',
                textColor : '#000000',
              }
            },
            {
              content: `${savedOrder?.vatAmount} (${defaultCurrency})`,
              styles:{
                halign:'right',
                font : 'courier',
                textColor : '#000000'
              }
            },
          ],
          [
            {
              content: 'Total amount',
              styles:{
                halign:'right',
                font : 'courier',
                textColor : '#000000',
                cellPadding : {right : 6},
              }
            },
            {
              content: `${savedOrder?.total} (${defaultCurrency})`,
              styles:{
                halign:'right',
                font : 'courier',
                textColor : '#000000',
                
              }
            },
          ],
        ],
        theme: 'plain'
      });
    // 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: `Date: ${generateDateFormat(savedOrder?.createdAt)}`
    //                     + `\nInvoice number: ${savedOrder?.orderNumber}`,
    //                 styles: {
    //                     halign: 'right'
    //                 }
    //             }
    //         ],
    //     ],
    //     theme: 'plain'
    // });

    // autoTable.default(doc, {
    //     body: [
    //         [
    //             {
    //                 content: 'Billed to:'
    //                     + `\n${mobileUser?.name}`
    //                     + `\n${address?.addresses[0]?.address ? address?.addresses[0]?.address : ""}`
    //                     + `\n${address?.addresses[0]?.state ? address?.addresses[0]?.state : ""}, ${address?.addresses[0]?.city ? address?.addresses[0]?.city : ""}, ${address?.addresses[0]?.country ? address?.addresses[0]?.country : ""}`,
    //                 styles: {
    //                     halign: 'left'
    //                 }
    //             },
    //             {
    //                 content: 'From:'
    //                     + '\nSnapezee'
    //                     + '\ninfo@snapzee.com'
    //                     + '\nApexo Inc',
    //                 // + '\nZip code - City'
    //                 // + '\nCountry',
    //                 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: `${savedOrder?.subtotal}`,
    //                 styles: {
    //                     halign: 'right'
    //                 }
    //             },
    //         ],
    //         [
    //             {
    //                 content: 'Total tax:',
    //                 styles: {
    //                     halign: 'right'
    //                 }
    //             },
    //             {
    //                 content: `${savedOrder?.vatAmount}`,
    //                 styles: {
    //                     halign: 'right'
    //                 }
    //             },
    //         ],
    //         [
    //             {
    //                 content: 'Total amount:',
    //                 styles: {
    //                     halign: 'right'
    //                 }
    //             },
    //             {
    //                 content: `${savedOrder?.total}`,
    //                 styles: {
    //                     halign: 'right'
    //                 }
    //             },
    //         ],
    //     ],
    //     theme: 'plain'
    // });


    doc.save(path.join(__dirname, "../", `trash/${savedOrder?.orderNumber}.pdf`));

    const pdfFileBuffer = fs.readFileSync(path.join(__dirname, "../", `trash/${savedOrder?.orderNumber}.pdf`));
    return pdfFileBuffer;
}


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