我想使用javascript在信封上列印數百個地址。我能夠使用下面的代碼來實作這個目標
。 function combineSenderInfo() {
var from = "<div style='padding-top:0px'> <h5> </h5>"
var name = "" "<br> "。
var address = "1107 Test Av" "< br>" "Test, Pa 19111" "< br>"。
var phone = " " "</div>"/span>;
return from name address phone。
}
function combineReceiverInfo(recieverData){
var from = "<br>" "<div style='max-width:500px; margin:0 auto; '>< h3> "
var name = recieverData["name"] "</h3>"。
var address = "<h3 style=''>"/span> recieverData["address1"/span>] "< br>" recieverData["address2"] "<br>" 。
var phone = recieverData["phone"] "</h3>< /div>";
return from name address phone;
}
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
function AddressesPrintManager(MassAddressDict){
var printWindow = window. open('', '', 'height=600,width=800') 。
printWindow.document.write('</title> ' )。
printWindow.document. write('<style>* { font-family: sans-serif } p { margin:0px 0; }h6{display:inline; text-transform:uppercase}</style>< style media="print">@page { size: size: 4. 5in 9.5in landscape;margin: 0; } </style>')。)
printWindow.document.write('</head><body style="display: flex;flex-direction: column; height: 90%;box-sizing: border-box; padding-top: 0px"> ')。
var padding = 200.
Object.keys(MassAddressDict)。 forEach(function(key) {
console.log(key " " MassAddressDict[key]) 。
var recieverData = MassAddressDict[key].
var senderInfo = combineSenderInfo() 。
//var packageDetails = combinePackageDetails();
var receiverInfo = combineReceiverInfo(recieverData)。
var index = Object. keys(MassAddressDict).indexOf(key)。
/padding = padding - 1.5。
console.log("Padding increased to ", padding)
printWindow.document.write(`<div id="label${index}" style="display:flex;justify-content:space-between" >`) 。
printWindow.document.write(senderInfo)。
//printWindow.document.write(packageDetails);/span>
printWindow.document.write(`<p style='padding-bottom: 95px'></p>`)
printWindow.document.write('</div> ')。
printWindow.document.write(receiverInfo)。
printWindow.document.write(`<p style='padding-bottom: ${padding}px'></p>`)
});
printWindow.document.write('</body></3html> ')。
printWindow.document.close()。
printWindow.print()。
}
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
上面的代碼產生這樣的結果
我已經嘗試過調整地址的位置。
我曾嘗試用
調整填充物 padding = padding - 1.5
但是這個方法并不奏效。有誰知道我怎樣才能解決一些頁面后地址位置漂移的問題嗎?
uj5u.com熱心網友回復:
為每個信封設定一個分頁。每個信封都是一個不同的頁面,預測每個信封到底有多高,可能永遠不會成功。在整個東西周圍放一個div:
printWindow.document. write('<div style="page-break-after:always;">') 。
printWindow.document.write('<div id="label${index}")
style="display:flex;justify-content:space-between;"> ')。
printWindow.document.write(senderInfo)。
printWindow.document.write(`<p style='padding-bottom: 95px'></p>`)
printWindow.document.write('</div>')。
printWindow.document.write(receiverInfo);
printWindow.document.write('</div> ')。
請確保洗掉你的<p>與填充物,以避免創建空白頁面。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/306706.html
標籤:

