使用JSPDF-Autotable在同一文档中打印表和图表
我如何使用JSPDF自动化印刷图表或图像和表格? 假设我有一个图表,我想将其放在桌子上。 或者我有一些客户端自定义的HTML文本,我需要将其与自定义HTML下方的小表相结合。 我试图将最高边距提高到插入图像的开放空间,这有点有效。 但是,如果我不知道桌子前或之后必须插入元素的大小怎么办?
async printThisThingsTogether() {
const doc = new jsPDF('p', 'mm','a4');
const marginsParent = { top: 105, bottom: 35, left: 15, right: 15 };
doc.setFontSize(5);
doc.setPage(1);
var coolCanvas = document.getElementById('coolCanvas')! as HTMLCanvasElement;
var newCanvasImg = coolCanvas!.toDataURL("image/png", 1.0)!;
doc.addImage(newCanvasImg, 'PNG', 10, 10, 190, 90 );
let page = document.getElementById(`table`) as HTMLTableElement;
autoTable(doc, {
html: page,
margin: marginsParent,
useCss: false,
theme: 'plain',
styles: {
lineColor: [44, 62, 80],
lineWidth: 1,
},
headStyles: {
fillColor: [241, 196, 15],
fontSize: 15,
},
footStyles: {
fillColor: [241, 196, 15],
fontSize: 15,
},
bodyStyles: {
fillColor: [52, 73, 94],
textColor: 240,
},
alternateRowStyles: {
fillColor: [74, 96, 117],
},
});
await doc.output('dataurlnewwindow', {filename: 'listagem.pdf'});
}
How would I print both a chart or image and a table, using jspdf-autotable?
Let's say I have a chart and I want to put that right before the table.
Or I have some HTML text customized by a client and I need to combine it with a small table right below the custom HTML.
I tried to increase the top margin to open space to the image inserted, it kinda worked.
But what if I don't know the size of the element I'll have to insert before or after the table?
async printThisThingsTogether() {
const doc = new jsPDF('p', 'mm','a4');
const marginsParent = { top: 105, bottom: 35, left: 15, right: 15 };
doc.setFontSize(5);
doc.setPage(1);
var coolCanvas = document.getElementById('coolCanvas')! as HTMLCanvasElement;
var newCanvasImg = coolCanvas!.toDataURL("image/png", 1.0)!;
doc.addImage(newCanvasImg, 'PNG', 10, 10, 190, 90 );
let page = document.getElementById(`table`) as HTMLTableElement;
autoTable(doc, {
html: page,
margin: marginsParent,
useCss: false,
theme: 'plain',
styles: {
lineColor: [44, 62, 80],
lineWidth: 1,
},
headStyles: {
fillColor: [241, 196, 15],
fontSize: 15,
},
footStyles: {
fillColor: [241, 196, 15],
fontSize: 15,
},
bodyStyles: {
fillColor: [52, 73, 94],
textColor: 240,
},
alternateRowStyles: {
fillColor: [74, 96, 117],
},
});
await doc.output('dataurlnewwindow', {filename: 'listagem.pdf'});
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果要将表格/图像/HTML内容放在表格上方,则应设置自动操作的
starty
,这是指启动表的y点。如果要将非表内容放在表面下方,则应获取表通过
doc.lastautotable.finaly
其中 doc 涉及JSPDF实例。然后将内容放在其中。希望它有帮助。
干杯
If you want to put chart/image/html content above the table, you should set
startY
of the autoTable which refers to starting Y point of the table.If you want to put non-table content below the table then you should get the the Y point where the table ends via
doc.lastAutoTable.finalY
in which doc refers the jsPDF instance. Then put the content with respect to that.Hope it helps.
Cheers