Is there any way to center text with jsPDF?

前端 未结 9 1277
半阙折子戏
半阙折子戏 2021-02-01 02:07

I\'m trying to create a simple pdf doc using javascript. I found jsPDF but I don\'t figure out how to center text. Is it possible?

9条回答
  •  忘了有多久
    2021-02-01 03:06

    I have found that the current version of jsPdf supports a parameter 'align' with the function signature like this:

    API.text = function (text, x, y, flags, angle, align)
    

    So the following should give you a center-aligned text:

    doc.text('The text', doc.internal.pageSize.width, 50, null, null, 'center');
    

    However, at the current point in time, an error is thrown in the library when strict mode is on because a 'var' is missing. There is an issue and pull request for it, but the fix hasn't made it in: https://github.com/MrRio/jsPDF/issues/575

    Whoever is looking for this, one day, you might be able to use this to make it easier to center text.

提交回复
热议问题