关于javascript:如何通过JavaScript-函数实现生成PDF-文件

前端往往须要将已有的数据展现后果,导出成PDF 文件, 反对将设计好的报表另存为 PDF, XLSX 及 HTML 文件。也能够不预览间接将报表导出。

在纯 JavaScript 利用中减少导出性能
须要将报表文件导出为哪种格局,就须要引入相干的js文件,如下:

<script src="https://cdn.grapecity.com/activereportsjs/2.latest/dist/ar-js-pdf.js"></script>
<script src="https://cdn.grapecity.com/activereportsjs/2.latest/dist/ar-js-xlsx.js"></script>
<script src="https://cdn.grapecity.com/activereportsjs/2.latest/dist/ar-js-html.js"></script>

如果须要在运行时设置一些导出属性,能够参考 导出 API 导出代码如下:

var viewer = new ActiveReports.Viewer("#viewer-host");
viewer.availableExports = ["pdf"];

ActiveReports.Viewer 的构造函数中第二个参数承受 ViewerOptions 对象 能够设置 ExportsSettings 属性。 您能够用来为每种反对的导出格局预设设置的默认值。

无预览间接导出
ActiveReportsJS 在PdfExport, HtmlExport XlsxExport 提供了 exportDocument 办法 ,以下代码演示了从报表加载,到运行,再到导出,下载导出文件:

import { Core, PdfExport } from "@grapecity/activereports";

const pdfExportSettings: PdfExport.PdfSettings = {
  title: "Test document",
  author: "GrapeCity",
  keywords: "export, report",
  subject: "Report",
  pdfVersion: "1.4",
};

const report = new Core.PageReport();
await report.load("/reports/text-only.rdlx-json");
const doc = await report.run();
const result = await PdfExport.exportDocument(doc, pdfExportSettings);
result.download("exportedreport.pdf");

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理