问题描述
在一个项目中,我已经将html文件转换为pdf,比它的工作正常。但这个输出不显示css设计。现在我需要一个建议如何添加css设计与此pdf文件?
In a project I have convert html file into pdf,than it's working fine.But this output not showing css design.Now I need a suggestion how can I add css design with this pdf file?
这里是js函数代码:
$(function(){
var doc = new jsPDF();
var specialElementHandlers = {
'#editor': function (element, renderer) {
return true;
}
};
$('#cmd').click(function () {
doc.fromHTML($('#StudentInfoListTable').html(), 15, 15, {
'width': 170,
'elementHandlers': specialElementHandlers
});
doc.save('sample-file.pdf');
});
我从这个项目中获得了帮助
I took help from this projecthttps://github.com/MrRio/jsPDF
这是我的表查找
This is my table looking
这是pdf输出
我试图添加一个表
$('#cmd').click(function () {
var table = tableToJson($('#StudentInfoListTable').get(0))
var doc = new jsPDF('p', pt, 'a1', true);
doc.cellInitialize();
$.each(table, function (i, row){
console.debug(row);
$.each(row, function (j, cell){
doc.cell(10, 50,120, 50, cell, i); // 2nd parameter=top margin,1st=left margin 3rd=row cell width 4th=Row height
})
})
doc.save('sample-file.pdf');
});
以下是函数
function tableToJson(table) {
var data = [];
// first row needs to be headers
var headers = [];
for (var i=0; i<table.rows[0].cells.length; i++) {
headers[i] = table.rows[0].cells[i].innerHTML.toLowerCase().replace(/ /gi,'');
}
// go through cells
for (var i=0; i<table.rows.length; i++) {
var tableRow = table.rows[i];
var rowData = {};
for (var j=0; j<tableRow.cells.length; j++) {
rowData[ headers[j] ] = tableRow.cells[j].innerHTML;
}
data.push(rowData);
}
return data;
}
让我看到这个输出后哭泣!
It make me cry after see this output !!
>
以任何方式避免这种重叠?
在pdf中添加css的最佳解决方案是什么?
Any way to avoid this overlapping ?What is the best solution to add css in pdf?
推荐答案
确定后大量尝试我解决了it.Especial感谢mihaidp,这里的代码我已解决表行问题
Ok after lots of try I have solved it.Especial thanks for mihaidp,Here the code I have solve table row problem
$('#cmd').click(function () {
var table = tableToJson($('#StudentInfoListTable').get(0))
var doc = new jsPDF('p', 'pt', 'a4', true);
doc.cellInitialize();
$.each(table, function (i, row){
doc.setFontSize(10);
$.each(row, function (j, cell){
if(j=='name')
{
doc.cell(10, 50,100, 30, cell, i);
}
else if(j=='email')
{
doc.cell(10, 50,130, 30, cell, i);
}
else if(j=='track')
{
doc.cell(10, 50,40, 30, cell, i);
}
else if(j=='s.s.croll')
{
doc.cell(10, 50,51, 30, cell, i);
}
else if(j=='h.s.croll')
{
doc.cell(10, 50,51, 30, cell, i);
}
else
{
doc.cell(10, 50,70, 30, cell, i);
}
})
})
输出
这篇关于HTML到PDF(通过javascript)如何添加css或表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!