html - 打印预览未显示正确的布局
问题描述
我需要打印我的 html,所以我布置了我想要的打印方式。但是当我点击print button
并被定向到打印预览时,布局都搞砸了。请在下面查看我的代码:
在 HTML 中
<div id="print-section" *ngIf="propertyLedger">
<h2>SOSA | PMIS</h2>
<hr>
<table class="table borderless">
<tr>
<th class="col-md-2">Transaction Date</th>
<td>{{dateToday | date}}</td>
</tr>
<tr>
<th class="col-md-2">Collecting Agent</th>
<td>{{propertyLedger.CollectinAgentName}}</td>
</tr>
<tr>
<th class="col-md-2">Unit Name</th>
<td>{{propertyLedger.UnitName}}</td>
</tr>
<tr>
<th class="col-md-2">Payment Type</th>
<td>{{propertyLedger.PaymentType}}</td>
</tr>
<tr>
<th class="col-md-2">O.R. Number</th>
<td>{{propertyLedger.ORNumber}}</td>
</tr>
<tr>
<th class="col-md-2">Remarks</th>
<td>{{propertyLedger.Remarks}}</td>
</tr>
</table>
<hr>
<h3>Details</h3>
<hr>
</div>
<button
class="btn btn-primary" (click)="print()">Print</button>
在 ts 文件中
print() {
let printContents, popupWin;
printContents = document.getElementById('print-section').innerHTML;
popupWin = window.open('', '_blank', 'top=0,left=0,height=100%,width=auto');
popupWin.document.open();
popupWin.document.write(`
<html>
<head>
<title>Print tab</title>
<style>
//........Customized style.......
</style>
</head>
<body onload="window.print();window.close()">${printContents}</body>
</html>`
);
popupWin.document.close();
}
在我的打印预览
中,表格左侧的文本似乎居中。你能帮我解决这个问题吗?如果您有办法在没有任何表格的情况下获得我想要的布局,那将是最好的!太感谢了。
解决方案
刚读完。HTML 中的默认值具有这些属性。
th {
display: table-cell;
vertical-align: inherit;
font-weight: bold;
text-align: center
}
你必须把它改成这样:
th {
display: table-cell;
vertical-align: inherit;
font-weight: bold;
text-align: left;
}
此外,如果您不使用 HTML5,您还可以将标签设置为:
<th align="left" class="col-md-2">Unit Name</th>
推荐阅读
- partitioning - Azure EventHub:如何获取现有 EventHub 的分区键列表
- python - (selenium) 在 Google 图片中找不到超过 20 个的图片元素
- c# - 异常处理程序未捕获 SOAP api 故障的问题 - 显示“此应用程序处于中断模式”
- apache-flink - Flink:在其他程序中嵌入 flink 是什么意思?
- micronaut - 要求在 POST 中发送一个字段
- android - 在房间数据库 Android 中为 @DatabaseView 传递参数
- c# - 从 Word 文档中获取页码
- date - 如何使用时区直接解析日期和时间
- scala - Scala 测试 SparkException:任务不可序列化
- python-3.x - 如何在python中将十六进制值转换为浮点数