question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

I have an idea for it. We can pass a two-dimensional array of texts, to a method with presentation options like the following:


doc.table([
  ["cell11"],["cell21"],["cell31"],
  ["cell12"],["cell22"],["cell32"],
  ["cell13"],["cell23"],["cell33"]
  ],{
    width:20,
    height:40,
    x:30,
    y:40
});

Then in the module we make rectangles and texts for each cell.

Issue Analytics

  • State:open
  • Created 12 years ago
  • Reactions:13
  • Comments:59 (3 by maintainers)

github_iconTop GitHub Comments

20reactions
giuseppe-santorocommented, Aug 5, 2013

Hi people! I’m developing a function to generate tables, in general my idea is:

doc.table data options

Where data is an array of rows and each row is rappresented as an object. For example, suppose you want to create an invoice, data should be something like this:

data =
    [
        { code: '0001', name: 'Black table', quantity: '10', price: '$ 19.20' }
        { code: '0005', name: 'White table', quantity: '8',  price: '$ 19.20' }
        { code: '0012', name: 'Red chair',   quantity: '40', price: '$ 12.00' }
    ]

In my opinion, this strategy is more useful than a two dimensional array when you get the data from a database.

The second parameter, options, contains a set of options that modify the rendering of the table. An example of options should be:

options =

    columns:
    [
        { id: 'code',     width: 10, name: 'Code' }
        { id: 'name',     width: 40, name: 'Name' }
        { id: 'quantity', width: 25, name: 'Quantity' }
        { id: 'price',    width: 25, name: 'Price' }
    ]

    margins:
        left: 20
        top: 40
        right: 20
        bottom: 0

    padding:
        left: 10
        top: 10
        right: 10
        bottom: 10

options.columns.id is the is ID used to map the column with the corresponding data. options.columns.width is the percentage of width that the column occupies; this meas that the sum of all width must be 100. options.columns.name is the name of the column that will be render as an header of the table. options.margins is the table margin, so it is the distance from the page borders to the table borders. options.padding is the cell padding, so it is the distance from the cell borders to the text in the cell.

What do you think about? any suggestion is appreciated!

11reactions
LanguageAgnosticcommented, Jul 26, 2018

Here’s another way inspired by the above.

Call

//Simple
createTable(doc, [[1, 2], [1, 2], [1, 2, 3, 4]]);


//Setting the width of the table
createTable(doc, [[1, 2], [1, 2], [1, 2, 3, 4]], 500);

Method

function createTable(doc, data, width = 500) {
  const startY = doc.y,
    startX = doc.x,
    distanceY = 15,
    distanceX = 10;

  doc.fontSize(12);

  let currentY = startY;

  data.forEach(value => {
    let currentX = startX,
      size = value.length;

    let blockSize = width / size;

    value.forEach(text => {
      //Write text
      doc.text(text, currentX + distanceX, currentY);

      //Create rectangles
      doc
        .lineJoin("miter")
        .rect(currentX, currentY, blockSize, distanceY)
        .stroke();

      currentX += blockSize;
    });

    currentY += distanceY;
  });
}

Result

exemple 2

Hope it helps some of you 👍 Feel free to improve/customize it

Read more comments on GitHub >

github_iconTop Results From Across the Web

Tables & desks - IKEA
From coffee tables to computer desks, and bedside tables to dining sets. A table is a versatile piece of furniture, often multitasking as...
Read more >
tables - Amazon.com
This personal folding table is lightweight and portable, making it convenient for indoor and outdoor use.This personal folding table is lightweight and portable ......
Read more >
Tables You'll Love in 2022 - Wayfair.com
Shop Wayfair for Tables to match every style and budget. Enjoy Free Shipping on most stuff, even big stuff.
Read more >
Accent Tables (Coffee, Console, End & Side Tables)
Accent tables, coffee & side tables for your living room. Find stylish accent pieces, console tables & living room furniture that you'll love...
Read more >
Dining Room Tables - Target
Shop Target for Dining Tables you will love at great low prices. Choose from Same Day Delivery, Drive Up or Order Pickup. Free...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found