【问题标题】:Add text in header for jspdf if (autoTable Html)Add text in header for jspdf if (autoTable Html)
【发布时间】:2022-12-14 16:55:21
【问题描述】:

我们如何将文本添加到 pdf 的顶部?我使用自动表。

 const handleDownloadPdf = async (orientation) => {
    const report = new jsPDF({
        orientation: orientation,
        unit: "pt",
        format: "a4",
    });

    report.autoTable({
        html: refExportExcel,
        margin: 15,
    })
    report.save('Report.pdf');
};

refExportExcel:它是表格的 ref(NextJS)。

【问题讨论】:

    标签: pdf-generation jspdf jspdf-autotable


    【解决方案1】:

    You can use the text method before calling the autotable to add text to the top

    import "./styles.css";
    import jsPDF from "jspdf";
    import autoTable from "jspdf-autotable";
    
    const handleDownloadPdf = () => {
      const report = new jsPDF({
        unit: "pt",
        format: "a4"
      });
      const refExportExcel = document.getElementById("myTable");
      report.text(20, 20, "This is some text at the top of the PDF.");
      report.autoTable({
        html: refExportExcel,
        margin: 35
      });
      report.save("Report.pdf");
    };
    
    export default function App() {
      return (
        <div className="App">
          <table id="myTable">
            <tr>
              <th>Company</th>
              <th>Contact</th>
              <th>Country</th>
            </tr>
            <tr>
              <td>Alfreds Futterkiste</td>
              <td>Maria Anders</td>
              <td>Germany</td>
            </tr>
            <tr>
              <td>Centro comercial Moctezuma</td>
              <td>Francisco Chang</td>
              <td>Mexico</td>
            </tr>
          </table>
          <button onClick={handleDownloadPdf}>Create</button>
        </div>
      );
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-06
      • 2017-08-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多