Git Product home page Git Product logo

Comments (8)

piersstorey avatar piersstorey commented on September 24, 2024 2

Just in case someone finds this issue. I've used a solution shown here: https://jsfiddle.net/j9vaqpnz/10/

from jspdf-autotable.

simonbengtsson avatar simonbengtsson commented on September 24, 2024

Unfortunately there is no simple way of doing alignment and you have to manually calculate the text positions for now in the renderCell method. In the Custom Style example I made an example of center alignment. To simplify alignments is very high on the priority list however.

from jspdf-autotable.

chip avatar chip commented on September 24, 2024

Ok. Thanks for the tip. 😄

from jspdf-autotable.

chip avatar chip commented on September 24, 2024

When I look at the Custom Style example PDF, I don't see anything centered. What I'm trying to accomplish is right aligning currency data. My initial thought was to do something like this:

// Inside renderCell
x = doc.internal.pageSize.width - width

However, it appears that width represents cell width, not width of the actual text value. I'm having trouble finding documentation on the text object, so any suggestions would be appreciated. Thanks.

from jspdf-autotable.

simonbengtsson avatar simonbengtsson commented on September 24, 2024

I updated the advanced styling example with a right aligned currency column. It should work, however as mentioned this is harder than it should be and it will be easier to style tables in version 2.0.

// In the renderCell function
// All cells with key 'currency' will be right aligned
doc.rect(x, y, width, height, 'S');
if (key === 'currency') {
    var strWidth = doc.getStringUnitWidth('' + value) * doc.internal.getFontSize();
    x += width;
    x -= 2 * settings.padding;
    x -= strWidth;
}
y += settings.lineHeight / 2 + doc.internal.getLineHeight() / 2 - 2.5;
doc.text('' + value, x + settings.padding, y);

from jspdf-autotable.

chip avatar chip commented on September 24, 2024

I had just cobbled this together from using the jsPDF API, which gave me a similar result:

if (key === 'currency') {
  var textWidth = doc.getTextDimensions(value);
  var padding = settings.padding || 0;
  var margin = settings && settings.margins ? settings.margins.horizontal : 0;
  x = doc.internal.pageSize.width - textWidth.w - padding - margin;
}

However, I'll use your example since you have a lot more experience with this kind of work. 😄

Thanks so much for your help!

from jspdf-autotable.

simonbengtsson avatar simonbengtsson commented on September 24, 2024

First of, no need to check if settings values exists. If not set by the user, I set default values for them. Second, your solution would only work for the right most column, but otherwise it looks good!

Some time ago I tested getTextDimensions versus using getStringUnitWidth and found that getStringUnitWidth was somewhat more exact. However, none of the them are perfect unfortunately.

No worries! :)

from jspdf-autotable.

chip avatar chip commented on September 24, 2024

Thanks for the info.

from jspdf-autotable.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.