simonbengtsson / jsPDF-AutoTable

jsPDF plugin for generating PDF tables with javascript
https://simonbengtsson.github.io/jsPDF-AutoTable/
MIT License
2.33k stars 624 forks source link

export type HookData #1075

Open aynuayex opened 1 week ago

aynuayex commented 1 week ago

export the type HookData from the library so that we can use it?

// eslint-disable-next-line new-cap
    const doc = new jsPDF()

    // Define the header and footer content
    const header = () => {
      doc.setFontSize(14)
      doc.text('Company Name: Alem Expect Solution', 14, 22) // Custom header
      doc.text(
        'Address: Addis Ababa Ethiopia, Gerji Mebrat Hail, Mag Building 204 office',
        14,
        27,
      ) // Custom footer
    }

    // Define the footer content
    const footer = (pdfdata: HookData) => {
      // const pageCount = doc.internal.getNumberOfPages()
      // const pageCount = doc.internal.pages.length - 1
      const pageCount = doc.getNumberOfPages()
      doc.setFontSize(10)
      doc.text(
        `Page ${pdfdata.pageNumber} of ${pageCount}`,
        pdfdata.settings.margin.left,
        doc.internal.pageSize.height - 10,
      ) // Footer with page count
    }

    // Safely map over data rows or use an empty array if data is undefined
    const dataRows =
      data?.data?.map((row) =>
        columns.map((col) => {
          const accessor = col.accessorKey as keyof FinanceReportType // Assert accessorKey as a key of FinanceReportType
          return accessor ? row[accessor] : ''
        }),
      ) || []
    // Create the table with data
    autoTable(doc, {
      head: [columns.map((c) => c.header)], // Table header
      body: dataRows, // Table body
      startY: 40, // Start the table below the header
      didDrawPage: (pdfdata) => {
        header() // Add header when drawing the page
        footer(pdfdata) // Add footer when drawing the page
      },
    })

    // Save the PDF to the browser
    doc.save('example.pdf')