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

getTextDimensions not working in case of multiline text #1933

Closed
hemachandsai opened this issue Sep 7, 2018 · 8 comments
Closed

getTextDimensions not working in case of multiline text #1933

hemachandsai opened this issue Sep 7, 2018 · 8 comments

Comments

@hemachandsai
Copy link

iam trying to add multipline text by using splittexttosize method but then when iam trying to retrieve its height iam getting wrong value which is too high?? what is the possible way to fix this and is there any way pass fontsize to gettextdimensions method? Thanks in advance

@Uzlopak
Copy link
Collaborator

Uzlopak commented Sep 7, 2018

getTextDimensions is using fontSize as far as i know. How do you calculate the height. Do you have some code?

@hemachandsai
Copy link
Author

do we have any option to pass font size as parameter to getTextDimensions() and when ever iam doing getTextDimensions of multilinetext it is not giving the correct result ? how should i use it for multiline text as i had a need to calculate previous text content and place the newer ones.Thanks for your speedy response @arasabbasi

@hemachandsai
Copy link
Author

Sorry iam still trying all the features so may the previous example may be confusing and clustered take this
var head2FullText = "Note that many diagnostic processes are procedures that generate observations and diagnostic reports. In many cases, such an observation does not require an explicit representation of the procedure used to create the observation, but where there are details of interest about how the diagnostic procedure was performed, the Procedure resource is used to describe the activity.In contrast to the Observation resource, the DiagnosticReport resource typically includes additional clinical context and some mix of atomic results, images, imaging reports, textual and coded interpretation, and formatted representations. Laboratory reports, pathology reports, and imaging reports should be represented using the DiagnosticReport resource. The Observation resource is referenced by the DiagnosticReport to provide the atomic results for a particular investigation"

var head2Text = doc.splitTextToSize(head2FullText,570,{fontSize:12});
const head2FinalHeight = doc.getTextDimensions(head2Text,{fontSize:10});
i had initiated doc in a4 and unit as points. And the result for head2FinaHeight is:
[object Object] {
h: 656.2491732283465,
w: 199.49974866141733
}
according to this the height of this text is 656pt which is more than 3/4th of page but ideally it is just 7 lines in page. @arasabbasi can you help me with this ??

@Uzlopak
Copy link
Collaborator

Uzlopak commented Sep 7, 2018

Ok... getTextDimensions is part of cell.js

Never touched that part of jsPDF.

Probably you get the right height by calculating the length of the resulting array of splitTextToSize e.g. (7 lines) * fontSize (e.g. 12) * 1.25 * scaleFactor ... or maybe divided by scaleFactor...

@hemachandsai
Copy link
Author

Thanks for your help @arasabbasi . Appericiate your efforts to opensource community

@Uzlopak
Copy link
Collaborator

Uzlopak commented Sep 11, 2018

Ok I checked the code of the method getTextDimensions. I strongly recommend not to use it. You should calculate it by the method i mentioned above.

@Uzlopak
Copy link
Collaborator

Uzlopak commented Sep 11, 2018

Ok, I refactored jsPDF.getTextDimensions in cell.js. It will be part of 1.4.2

See #1945

@Uzlopak Uzlopak closed this as completed Sep 11, 2018
@GGlielmi
Copy link

GGlielmi commented May 24, 2024

I'm currently using this solution

doc.setFontSize(fontSize);
doc.setFont(font, style);
doc.setLineHeightFactor(lineHeightFactor);
const amountOfLines = doc.splitTextToSize(text, width).length;
const height =
  (amountOfLines * fontSize * lineHeightFactor -
    fontSize * (lineHeightFactor - 1)) /
  doc.internal.scaleFactor;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants