Как выровнять несколько строк центра в jsPDF? - PullRequest
0 голосов
/ 17 мая 2019

Я нашел это решение для выравнивания текста по центру

(function(API){
API.centerText = function(txt, options, x, y) {
    options = options ||{};
    /* Use the options align property to specify desired text alignment
     * Param x will be ignored if desired text alignment is 'center'.
     * Usage of options can easily extend the function to apply different text 
     * styles and sizes 
    */
    if( options.align == "center" ){
        // Get current font size
        var fontSize = this.internal.getFontSize();

        // Get page width
        var pageWidth = this.internal.pageSize.width;

        // Get the actual text's width
        /* You multiply the unit width of your string by your font size and divide
         * by the internal scale factor. The division is necessary
         * for the case where you use units other than 'pt' in the constructor
         * of jsPDF.
        */
        txtWidth = this.getStringUnitWidth(txt)*fontSize/this.internal.scaleFactor;

        // Calculate text's x coordinate
        x = ( pageWidth - txtWidth ) / 2;
    }

    // Draw text at x,y
    this.text(txt,x,y);
}
})(jsPDF.API);

Я могу использовать это для выравнивания центра

doc.centerText('this is first line',{align: "center"},0,40);

Но это работает только для одной строки, если текст кратенлинии, это не работает

doc.centerText('this is first line\n this is second line',{align: "center"},0,40);

Как выровнять центр по нескольким строкам?

...