Side by side Tables
Opened this issue · 7 comments
Hello,
I am looking for a way to adjust the alignment of my table so that I can have 2 tables side by side.
I am using PDFKit, I already have my table sized appropriately, I just need to know how to add a second table next to it.
Thanks
Hello,
Is this possible anyhow with the current version?
Hi, you probably need to use PDFKit Document props to move at the right place, with some tricks for PdfTable. Be aware that if you use fitcolumn
plugin, you must specify a maxWidth when defining the plugin.
table.addPlugin(new (require('../plugins/fitcolumn'))({
column: 'description',
maxWidth: 120 // mandatory, or else the first table will take all width available on page
}))
Be also aware that if there is a page break, you will need to switch back to previous page yourself.
For a simple usecase, you can try this:
// Base data
const doc = ''; // PDFDocument
const table = ''; // PdfTable
const data1 = ''; // Array
const data2 = ''; // Array
// Save Y position
const y = doc.y
// Insert first table
table.addBody(data1)
// Reset position and move X
doc.y = y;
doc.x = 300;
table.pos.x = doc.x;
// Insert second table
table.addBody(data2);
// stream your pdf wherever you want to see the result
This was my first idea but for table.pos.x = doc.x; i get:
Property 'pos' does not exist on type 'VoilabPdfTable'
Do you have the last version from master branch? pos
property is set on PdfTable constructor, so it should be available.
Tested on my local machine:
const PdfTable = require('voilab-table');
const PdfDocument = require('pdfkit');
const doc = new PdfDocument()
const table = new PdfTable(doc, {
bottomMargin: 30
});
console.log(table.pos);
// prints : { x: 72, y: 72 }
Yes i use the latest 0.5.1 but in TS. :)
Oh, I don't have time right now to check the behaviour on TS. Feel free to make a pull request. The point is: pos
property must be available as public. Maybe there're some tricks to do to make TS understand this prop visibility?
I see it can be set with configuration
const table = new PdfTable(doc, {
bottomMargin: 30
pos: { x: ...
}
});
I will try to create new object for table on the right side.
Thanks for help.