javascript
LODOP=getLodop();
var str = '<svg><text x="0mm" y="5mm" textLength="300px" lengthAdjust="spacingAndGlyphs" style="width: 300px;height:50px word-break: break-all;font-size:25px">测试文本测试文本测试文本测试文本测试文本11</text></svg>';
LODOP.PRINT_INITA(10,10,762,533,"打印控件功能演示_Lodop功能");
LODOP.SET_PRINT_STYLE("FontColor","#0000FF");
LODOP.ADD_PRINT_IMAGE(0,0,300,50,str);
LODOP.PRINT_DESIGN();
以上代码固定宽度300px,文字短了会拉长,长了会压缩。如果短的时候不想让他拉长,那么可以先计算一下文本的宽度,如果宽度长于300,那textLength就设置300,如果小于300,那textLength就设置为这个文本宽度。
javascript
textSize(text) {
const span = document.createElement('span')
const result = {}
result.width = span.offsetWidth
result.height = span.offsetHeight
span.style.visibility = 'hidden'
span.style.fontSize = '11px'
span.style.fontFamily = 'Avenir, Helvetica, Arial, sans-serif'
span.style.display = 'inline-block'
span.style.whiteSpace = 'nowrap'
document.body.appendChild(span)
if (typeof span.textContent != 'undefined') {
span.textContent = text
} else {
span.innerText = text
}
result.width =
parseFloat(window.getComputedStyle(span).width) - result.width
// result.height = parseFloat(window.getComputedStyle(span).height) - result.height;
return result.width
},