Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Text Alignement #1078

Closed
Smiling-bishop opened this issue Mar 6, 2017 · 8 comments
Closed

Text Alignement #1078

Smiling-bishop opened this issue Mar 6, 2017 · 8 comments

Comments

@Smiling-bishop
Copy link

Hey,

I just try to align center text but when I do the same thing that the example, my text is not align center.
I copy/paste your example but still not working.

Help me please :p

@Uzlopak
Copy link
Collaborator

Uzlopak commented May 23, 2017

#1016

@Sarfarazsajjad
Copy link

@arasabbasi #1016 is about auto-table plugin and does not solve the problem.

@Uzlopak
Copy link
Collaborator

Uzlopak commented Sep 16, 2017

@Sarfarazsajjad

While I was refactoring the jspdf-Code I realized, that the code for handling alignments is broken. It simply doesnt work if you put multiple lines in to it like

doc.text(['a','b','c'], 10, 10);

You can check for my code #1419 and see, that my refactored code is working.

@Sarfarazsajjad
Copy link

Sarfarazsajjad commented Sep 16, 2017

@tsan below example works please try it

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<lines.length;i++){
      lineTop = (lineHeight/2)*i
      doc.text(lines[i],pageCenter,20+lineTop,'center'); //see this line
    }
    doc.save('Generated.pdf');

@Uzlopak
Copy link
Collaborator

Uzlopak commented Sep 16, 2017

Why should I try something which is just a workaround but not a fix?

@Uzlopak
Copy link
Collaborator

Uzlopak commented Sep 16, 2017

Ok, thumbs down. But still... it is just a workaround and not a real solution. We are programmers and not plumbers.

@Uzlopak
Copy link
Collaborator

Uzlopak commented Sep 16, 2017

Yeah, give me a thumbs down and give your own post a thumbs up. You get from me a thumbs up for your ingenuity too 👍

@Uzlopak
Copy link
Collaborator

Uzlopak commented Feb 15, 2018

Will be solved with #1433

@Uzlopak Uzlopak closed this as completed Feb 15, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants