在React中,你可以做以下操作:
检查
this
官方仓库以更改 jspdf-autotable 生成器类。
此外,你可以使用标题图像,但需要将其转换为base64格式。
Convert 图像为
base64 在线服务。
还可以使用我的PDF类来使用TypeScript生成一个具有js-pdf和autotable插件的适当PDF报告:
import jspdf from "jspdf";
import autoTable from "jspdf-autotable";
import CustomPDFFont from "../assets/make_pdf_assets/Font";
import CustomHeadingImg from "../assets/make_pdf_assets/HeadingImage";
import colors from "../config/colors";
export default class ExportPdf {
document: any;
isValueAdded: boolean;
pageSize: any;
pageHeight: any;
pageWidth: any;
createdAt: string;
constructor() {
this.isValueAdded = false;
this.document = new jspdf("p", "px");
this.document.addFileToVFS("font_name.ttf", CustomPDFFont);
this.document.addFont(
"font_name.ttf",
"font_face_name",
"normal"
);
this.document.setFont("font_face_name");
this.pageSize = this.document.internal.pageSize;
this.pageHeight = this.pageSize.height
? this.pageSize.height
: this.pageSize.getHeight();
this.pageWidth = this.pageSize.width
? this.pageSize.width
: this.pageSize.getHeight();
this.createdAt = "Mamad Steve";
}
private _setHeaderLogo = (data: any) => {
this.document.addImage(
CustomHeadingImg,
"png",
data.settings.margin.left,
20,
70,
20
);
};
private _setDateTime = () => {
const date: Date = new Date();
let reportDate: string = [
date.getFullYear(),
date.getMonth(),
date.getDay(),
].toString();
let reportTime: string = [
date.getHours(),
date.getMinutes(),
date.getSeconds(),
].toString();
reportDate = reportDate.replaceAll(",", "/");
reportTime = reportTime.replaceAll(",", ":");
this.document.setFontSize(10);
this.document.text(
`Date: ${reportDate} Time: ${reportTime}`,
this.pageWidth - 140,
50,
);
};
private _setPageNum = (data: any) => {
const content = "Page " + this.document.internal.getNumberOfPages();
this.document.setFontSize(10);
this.document.text(
`${content}, By ${this.createdAt}`,
data.settings.margin.left,
this.pageHeight - 10
);
};
public addNewTableInPdf = (data: any[], columns: any[]): void => {
autoTable(this.document, {
head: columns,
body: data,
theme: "grid",
styles: { font: "font_face_name", halign: "center" },
didDrawPage: (data) => {
this._setHeaderLogo(data);
this._setDateTime();
this._setPageNum(data);
},
headStyles: { fillColor: colors.primary, textColor: "#fff" },
margin: { top: 50 },
});
};
}
这个类是我自己写的,它运行良好,但可能对其他开发人员来说不够简洁。但结果是:
输入图片描述