JSFiddle - React, Tailwind, and code Playground

Export and print

by Flexmonster Pivot Table

HTML

<link rel="stylesheet" href="https://s3.amazonaws.com/flexmonster/2.3/demo.css">
<script src="https://cdn.flexmonster.com/flexmonster.js"></script>

<button onclick="pdf()">Export to PDF</button>

<div id="pivot-container"></div>

JavaScript

let pivot = new Flexmonster({
  container: "pivot-container",
  componentFolder: "https://cdn.flexmonster.com/",
  beforetoolbarcreated: customizeToolbar,
  toolbar: true,
  report: {
    dataSource: {
      filename: "data/data.csv",
    },
    options: {
      grid: {
        showHierarchyCaptions: false,
      },
    },
    slice: {
      columns: [
        {
          uniqueName: "Color",
          filter: {
            members: ["red", "green", "blue"],
          },
        },
        {
          uniqueName: "[Measures]",
        },
      ],
      measures: [
        {
          aggregation: "sum",
          uniqueName: "Price",
        },
        {
          caption: "Sales $ YTD Jan 24-Feb 24",
          uniqueName: "Price",
          aggregation: "sum",
        },
      ],
      rows: [
        {
          uniqueName: "Category",
        },
      ],
    },
    localization: {
      aggregations: {
        sum: {
          caption: "Sum",
          grandTotalCaption: "Σ {0}",
          totalCaption: "Σ {0}",
        },
      },
    },
  },
})

function customizeToolbar(toolbar) {
  let tabs = toolbar.getTabs()
  toolbar.getTabs = function () {
    let exportTab = tabs.find((tab) => tab.id == "fm-tab-export")
    if (!exportTab) return tabs
    let exportToPdfTab = exportTab.menu.find(
      (tab) => tab.id == "fm-tab-export-pdf",
    )
    exportToPdfTab.handler = pdf
    return tabs
  }
}

function pdf() {
  const params = {
    //Import the font
    fontUrl: "https://cdn.flexmonster.com/fonts/NotoSans-Regular.ttf",
  }
  pivot.exportTo("pdf", params)
}