如何计算使用jsPDF库创建的PDF文档中字符串的长度(以mm为单位)?

如何计算使用jsPDF库创建的PDF文档中字符串的长度(以mm为单位)?,pdf,pdf-generation,jspdf,measure,Pdf,Pdf Generation,Jspdf,Measure,我使用jsPDF库创建和打印PDF文档。这个库公开了一些低级的方法,这些方法还可以,但是我有大量的字段要创建,其中许多字段是相似的,我需要创建更高级的抽象 例如,我有一个createLabel函数,我想调用它来代替这个低级的东西 var doc = new jsPDF('portrait', 'mm', 'a4'); doc.addFont('Arial', "sans-serif", "normal"); // name doc.setFontSize(14); doc.text(10, 1

我使用jsPDF库创建和打印PDF文档。这个库公开了一些低级的方法,这些方法还可以,但是我有大量的字段要创建,其中许多字段是相似的,我需要创建更高级的抽象

例如,我有一个
createLabel
函数,我想调用它来代替这个低级的东西

var doc = new jsPDF('portrait', 'mm', 'a4');
doc.addFont('Arial', "sans-serif", "normal");

// name
doc.setFontSize(14);
doc.text(10, 19, "name:");
doc.setLineWidth(0.1);
doc.line(25, 19, 100, 19); // meaning x1, y1, x2, y2

// CUI
doc.setFontSize(14);
doc.text(10, 29, "CUI:");
doc.setLineWidth(0.1);
doc.line(21, 29, 100, 29);

// same stuff but use functions instead.
createLabel("name: ", 10,50, 100); // meaning (labelName, x, y, totalWidth) 
createLabel("CUI: ", 10,60, 100);

如您所见,第二组标签的线未放置在正确的位置。他们太偏左了。它们的起始位置是根据labelName的长度生成的,而此长度计算失败。我怎样才能使它正常工作?到目前为止,守则是:

function createLabel(name, x, y, totalWidth) {
    //draw name
    doc.setFontSize(14);
    doc.text(x, y, name);

    // draw line
    const nameLength = (measureLength(name)) + 2; 
    doc.setLineWidth(0.1);

        // i want to start the line after the name ends + 2 mm.
        // and end the line in such a way that nameLength + lineLength == totalWidth of the compoenent.

    doc.line(x + nameLength, y, x + totalWidth, y);
}

function measureLength(str) {
    let canvas = document.createElement('canvas'); // in memory canvas.. not rendered anywere..
    let ctx = canvas.getContext("2d")
    ctx.font = "14px Arial";

    let width = ctx.measureText(str).width;

    let mm = ( width * 25.4 ) / 149 // meaning (px * 25.4) / screen DPI
    console.log (mm); 
    return mm; // of course, this calculation turns out wrong..
}
如何使此
测量长度
功能正常工作?我发现的大多数解决方案都涉及DOM,但这是PDF

注意:我对PDF文档和画布使用相同的字体(“14px Arial”)。
感谢您提供的任何见解:)

这可能会解决您的问题:

createLabel(name, x, y, totalWidth) {
   doc.setFontSize(14);
   doc.text(x, y, name);

   // draw line
   const nameLength = (doc.getTextDimensions(name).w / (72 / 25.6) ) + 2;
   console.log('nameLength', nameLength); // todo remove

   doc.setLineWidth(0.1);

   // i want to start the line after the name ends + 2 mm.
   // and end the line in such a way that nameLength + lineLength == totalWidth of the compoenent.

   doc.line(x + nameLength, y, x + totalWidth, y);
}
检查我如何计算
nameLength
——使用内置jsPDF函数并转换为mm。 有用链接:

这是结果:

请记住,您使用
x+totalWidth
表示线宽,因此与顶部的手动示例相比,行长
x