Is there any way to center text with jsPDF?

前端 未结 9 1199
半阙折子戏
半阙折子戏 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:14

    doc.text(text,left,top,'center') can be used to center text. It can be used with array of lines as well but when it is used with array the center does not work right so I have used it in a loop for every object in the array.

    var lMargin=15; //left margin in mm
    var rMargin=15; //right margin in mm
    var pdfInMM=210;  // width of A4 in mm
    var pageCenter=pdfInMM/2;
    
    var doc = new jsPDF("p","mm","a4");
    var paragraph="Apple's iPhone 7 is officially upon us. After a week of pre-orders, the latest in the iPhone lineup officially launches today.\n\nEager Apple fans will be lining up out the door at Apple and carrier stores around the country to grab up the iPhone 7 and iPhone 7 Plus, while Android owners look on bemusedly.\n\nDuring the Apple Event last week, the tech giant revealed a number of big, positive changes coming to the iPhone 7. It's thinner. The camera is better. And, perhaps best of all, the iPhone 7 is finally water resistant.\n\nStill, while there may be plenty to like about the new iPhone, there's plenty more that's left us disappointed. Enough, at least, to make smartphone shoppers consider waiting until 2017, when Apple is reportedly going to let loose on all cylinders with an all-glass chassis design.";
    
    var lines =doc.splitTextToSize(paragraph, (pdfInMM-lMargin-rMargin));
    var dim = doc.getTextDimensions('Text');
    var lineHeight = dim.h
    for(var i=0;i

提交回复
热议问题