jsPDF调整文本行间距

时间:2013-08-29 23:04:13

标签: javascript jquery jspdf

在jsPDF的documentation中,我无法找到增加文本行之间空间的方法或函数。我想知道是否知识渊博的人会点头分享他/她的知识:)。非常感谢。

2 个答案:

答案 0 :(得分:5)

您还可以在jsPDF构造函数中添加参数:

file = new jsPDF({orientation: "p", lineHeight: 1.5)})

来自jsPDF代码(函数jsPDF(orientation,unit,format,compressPdf)):

var options = {};

if (typeof orientation === 'object') {
    options = orientation;

    orientation = options.orientation;
    unit = options.unit || unit;
    format = options.format || format;
    compressPdf = options.compress || options.compressPdf || compressPdf;
}

// Default options
unit = unit || 'mm';
format = format || 'a4';
orientation = ('' + (orientation || 'P')).toLowerCase();

var format_as_string = ('' + format).toLowerCase(),
    compress = !!compressPdf && typeof Uint8Array === 'function',
    textColor = options.textColor || '0 g',
    drawColor = options.drawColor || '0 G',
    activeFontSize = options.fontSize || 16,
    lineHeightProportion = options.lineHeight || 1.15,
    lineWidth = options.lineWidth || 0.200025; // 2mm

答案 1 :(得分:3)

API.text的输出使用lineHeightProportion确定行高:

        out(
            'BT\n/' +
                activeFontKey + ' ' + activeFontSize + ' Tf\n' + // font face, style, size
                (activeFontSize * lineHeightProportion) + ' TL\n' + // line spacing
                textColor +
                '\n' + f2(x * k) + ' ' + f2((pageHeight - y) * k) + ' Td\n(' +
                str +
                ') Tj\nET'
        );

将上述各行更改为

                // (activeFontSize * lineHeightProportion) + ' TL\n' + // line spacing
                (activeFontSize * this.lineHeightProportion) + ' TL\n' + // line spacing

并设置变量:

pdf = new jsPDF("portrait", "in", "letter");
pdf.lineHeightProportion = 2;

应该这样做。

https://github.com/MrRio/jsPDF/pull/167