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

Error using jsPDF cell table() method #2223

Closed
tmmsmoreira opened this issue Jan 4, 2019 · 9 comments
Closed

Error using jsPDF cell table() method #2223

tmmsmoreira opened this issue Jan 4, 2019 · 9 comments
Labels

Comments

@tmmsmoreira
Copy link

Hello,

First, I want to say, what an amazing framework!
I was testing it to implement in a current project that I am working on and I am having some issues creating tables on the document. Based on your documentation, I am doing everything correctly. Here is a piece of code so you can check the problem that I am having:

JS File

var data = [
  {
    coin: "100",
    game_group: "GameGroup",
    game_name: "XPTO1",
    game_version: "25",
    machine: "20485861",
    vlt: "0"
  },
  {
    coin: "100",
    game_group: "GameGroup",
    game_name: "XPTO2",
    game_version: "25",
    machine: "20485861",
    vlt: "0"
  }
];

var header = ["coin", "game_group", "game_name", "game_version", "machine", "vlt"]

function save() {
  var doc = new jsPDF()
  doc.text(20, 20, 'Hello world!')
  doc.table(1, 1, data, header)
  doc.save()
}

HTML File

<button onclick="save()">Save</button>

The error that I am having is this:

jspdf.min.js:29 Uncaught Error: Invalid arguments passed to jsPDF.rect
    at Object.l.__private__.rect.l.rect (jspdf.min.js:29)
    at Object._.cell (jspdf.min.js:88)
    at Object._.printHeaderRow (jspdf.min.js:88)
    at Object._.table (jspdf.min.js:88)
    at save (pen.js:25)
    at HTMLButtonElement.onclick (index.html?editors=1111:6)

Here is the codepen link so you can check the problem on your console:
https://codepen.io/anon/pen/EGQzNr

Thank you for your attention and support, and keep up the good work!

@viraj4422
Copy link

Try this:

  {
    coin: "100",
    game_group: "GameGroup",
    game_name: "XPTO1",
    game_version: "25",
    machine: "20485861",
    vlt: "0"
  },
  {
    coin: "100",
    game_group: "GameGroup",
    game_name: "XPTO2",
    game_version: "25",
    machine: "20485861",
    vlt: "0"
  }
];
var header = createHeaders(["coin", "game_group", "game_name", "game_version", "machine", "vlt"]);

function createHeaders(keys) {
    return keys.map(key => ({
      'name': key,
      'prompt': key,
      'width':65,
      'align':'center',
      'padding':0
    }));
}

function save() {
  var doc = new jsPDF()
  doc.text(20, 20, 'Hello world!')
  doc.table(1, 1, data, header)
  doc.save()
}

@tmmsmoreira
Copy link
Author

Try this:

  {
    coin: "100",
    game_group: "GameGroup",
    game_name: "XPTO1",
    game_version: "25",
    machine: "20485861",
    vlt: "0"
  },
  {
    coin: "100",
    game_group: "GameGroup",
    game_name: "XPTO2",
    game_version: "25",
    machine: "20485861",
    vlt: "0"
  }
];
var header = createHeaders(["coin", "game_group", "game_name", "game_version", "machine", "vlt"]);

function createHeaders(keys) {
    return keys.map(key => ({
      'name': key,
      'prompt': key,
      'width':65,
      'align':'center',
      'padding':0
    }));
}

function save() {
  var doc = new jsPDF()
  doc.text(20, 20, 'Hello world!')
  doc.table(1, 1, data, header)
  doc.save()
}

Now it's working. Documentation is not very explicit about this point. Thank you so much @viraj4422

@Uzlopak
Copy link
Collaborator

Uzlopak commented Jan 9, 2019

I did not refactor cell.js till now. Try autoTable plugin as an alternative ;)

@viraj4422
Copy link

@arasabbasi There is also an issue regarding table header overlapping page header on page 2 onwards.

Page 2:

image

Is this a known bug ? Would you recommend going with auto-table ?

@lexcode
Copy link

lexcode commented Jan 15, 2019

@viraj4422 I am having same issue of table line overlapping on second page, have you found a solution, would you mind share it if you did, please?

Thanks

@Uzlopak
Copy link
Collaborator

Uzlopak commented Apr 6, 2019

Maybe you should use autotable plugin by @simonbengtsson

It is much more into table representation than jsPDF will be.

@Uzlopak Uzlopak added the cell.js label Apr 6, 2019
@Uzlopak
Copy link
Collaborator

Uzlopak commented Apr 11, 2019

@viraj4422
@lexcode
Yes, it was an annoying bug. Refactored cell.js #2412

Will be fixed in next release.

@Uzlopak Uzlopak closed this as completed Apr 11, 2019
tobiaskret added a commit to tobiaskret/DefinitelyTyped that referenced this issue Nov 6, 2019
Change the type of the argument `headers`of the method `table` from a string array to an object array (with specific properties).

See: parallax/jsPDF#2223
sandersn pushed a commit to DefinitelyTyped/DefinitelyTyped that referenced this issue Nov 6, 2019
Change the type of the argument `headers`of the method `table` from a string array to an object array (with specific properties).

See: parallax/jsPDF#2223
@stiofand
Copy link

This remains broken, and also the doc for the table() method suggests headers should be an array of strings, but the examples show an array of objects,

Feb 2021

@HackbrettXXX
Copy link
Collaborator

@stevematdavies this should have been fixed in #3087, but is not yet released on npm.

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

No branches or pull requests

6 participants