Javascript 使用jspdf对齐表中列的文本
我使用jspdf将html表格导出为pdf。我正在尝试格式化表格。我能够调整边距。我想将表中的几列向右对齐,几列向左对齐。在表中,我希望第一列向左对齐,最后一列向右对齐Javascript 使用jspdf对齐表中列的文本,javascript,jquery,html,css,jspdf,Javascript,Jquery,Html,Css,Jspdf,我使用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
<!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>
{{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”)}
账户
描述
日记账分录#
借记
信用
全部的
保存PDF
这里是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>
$(文档).ready(函数(){
demoFromHTML();
});
函数demoFromHTML(){
var pdf=新的jsPDF('p','pt','letter')
,source=$('tblSaveAsPdf')[0]
,SpecialElementHandler={
“#绕过我”:函数(元素、渲染器){
返回真值
}
}
边距={
排名:80,
底数:60,
左:40,,
宽度:522
};
pdf.fromHTML(
source//HTML字符串或DOM元素引用。
,margins.left//x坐标
,margins.top//y坐标
, {
“宽度”:margins.width//PDF上内容的最大宽度
,“elementHandlers”:specialElementHandlers
},
函数(dispose){
保存('Test.pdf');
},
边距
)
}
确定解决方案是将表按列拆分为不同的表,并将其与jspdf.plugin.htmltable.js对齐。希望它能帮助您更好的解决方案是将jspdf.plugin.htmltable.js修改为swith cell.align。并在html表格的单元格中添加align
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;
开关(options.align | | cell.align){
案例'-webkit right':
案例“正确”:
如果(!(数组的txt实例)){
txt=[txt];
}
对于(i=0;i