我使用jspdf将html表导出为pdf。我正在尝试格式化表格。我能够调整边距。我想将几列表格右对齐,少数左对齐。在表格中,我希望第一列与左对齐,最后一列与右对齐。
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="utf-8">
<title></title>
{{ HTML::script("js/jspdf/js/jquery/jquery-1.7.1.min.js")}}
{{ HTML::script("js/jspdf/js/jquery/jquery-ui-1.8.17.custom.min.js")}}
{{ HTML::script("js/jspdf/js/libs/polyfill.js")}}
{{ HTML::script("js/jspdf/jspdf.js")}}
{{ HTML::script("js/jspdf/js/libs/deflate.js")}}
{{ HTML::script("js/jspdf/js/libs/adler32cs.js/adler32cs.js")}}
{{ HTML::script("js/jspdf/js/libs/FileSaver.js/FileSaver.js")}}
{{ HTML::script("js/jspdf/js/libs/Blob.js/Blob.js")}}
{{ HTML::script("js/jspdf/jspdf.plugin.standard_fonts_metrics.js")}}
{{ HTML::script("js/jspdf/jspdf.plugin.split_text_to_size.js")}}
{{ HTML::script("js/jspdf/jspdf.plugin.addimage.js")}}
{{ HTML::script("js/jspdf/jspdf.plugin.cell.js")}}
{{ HTML::script("js/jspdf/jspdf.plugin.from_html.js")}}
{{ HTML::script("js/jspdf/js/basic-test.js")}}
</head>
<body>
<div id="tblSaveAsPdf" class="table-details margin-top-small collapse" style="display: none; margin-top: -10em;">
<table class="table table-bordered table-striped table-hover bootstrap-datatable datatable dataTable table-responsive">
<thead>
<tr role="row"> <%--class="headerRow"--%>
<th role="columnheader" class="col-lg-2">Account</th>
<th role="columnheader" class="col-lg-3">Description</th>
<th role="columnheader" class="col-lg-3">Journal Entry#</th>
<th role="columnheader" class="col-lg-2">Debit</th>
<th role="columnheader" class="col-lg-2">Credit</th>
</tr>
</thead>
<tfoot>
<tr role="row">
<td style="text-align: center">
<span>TOTAL</span>
</td>
<td></td>
<td></td>
<td style="text-align: right">
<span data-bind="text: pp_formattedDebitTotal"></span>
</td>
<td style="text-align: right">
<span data-bind="text: pp_formattedCreditTotal"></span>
</td>
</tr>
</tfoot>
<tbody data-bind="foreach: pp_voidCheckGLSummarys">
<tr >
<td>
<span data-bind="text: pp_account"></span>
</td>
<td >
<span data-bind="text: pp_accountName"></span>
</td>
<td>
<span data-bind="text: pp_entry"></span>
</td>
<td>
<span data-bind="text: pp_amountDebit"></span>
</td>
<td>
<span data-bind="text: pp_amountCredit"></span>
</td>
</tr>
</table>
</div>
</tbody>
<div>
<button onclick="demoFromHTML()" class="button">SAvePDF</button></p></div></div>
</div>
这是javascript文件
<script>
$(document).ready(function() {
demoFromHTML();
});
function demoFromHTML() {
var pdf = new jsPDF('p', 'pt', 'letter')
, source = $('#tblSaveAsPdf')[0]
, specialElementHandlers = {
'#bypassme': function(element, renderer){
return true
}
}
margins = {
top: 80,
bottom: 60,
left: 40,
width: 522
};
pdf.fromHTML(
source // HTML string or DOM elem ref.
, margins.left // x coord
, margins.top // y coord
, {
'width': margins.width // max width of content on PDF
, 'elementHandlers': specialElementHandlers
},
function (dispose) {
pdf.save('Test.pdf');
},
margins
)
}
</script>
答案 0 :(得分:1)
好的解决方案是将表拆分为不同的表,并将其与jspdf.plugin.htmltable.js对齐。希望它可以帮到你
答案 1 :(得分:1)
更好的解决方案是修改jspdf.plugin.html table.js以切换cell.align。并在html表格的单元格中添加对齐。
switch (options.align || cell.align) {
case '-webkit-right':
case 'right':
if (!(txt instanceof Array)) {
txt = [txt];
}
for (i = 0; i < txt.length; i++) {
var textSize = this.getStringUnitWidth(txt[i]) * this.internal.getFontSize();
this.text(txt[i], cursor.x + cell_widths[x] - textSize - padding, cursor.y + this.internal.getLineHeight()*(i+1));
}
break;