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

fix: respect provided y position for addRow #49

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

pomm0
Copy link

@pomm0 pomm0 commented Jun 22, 2022

I figured that the current state of this addon does not allow arbitrarily setting the initial y position and will be the value of the current pdf position. Sometimes it comes handy to be able to set it, since I can set the x position too (unsure if thats supposed to do or not).

With this change it allows to define the y start position like so:

const doc = new PDFDocument({
    layout: 'landscape',
});

const imageWidth = 200;
const additionalLeftPadding = 30;

doc.image(
    `${__dirname}/sample.jpg`,
    doc.page.margins.left,
    doc.page.margins.top,
    { width: imageWidth }
);

const table = new PDFTable(doc, {
    pos: {
        y: doc.page.margins.top,  // <---- Thats the part which is working as expected now
        x: doc.page.margins.left + imageWidth + additionalLeftPadding
    }
});

table.addColumns([
    {
        id: 'product',
        header: 'Product',
        width: 70,
        padding: [5, 0],
        border: 'T',
    },
    {
        id: 'quantity',
        header: 'Quantity',
        width: 70,
        padding: [5, 0],
        border: 'T',
    },
    {
        id: 'price',
        header: 'Price',
        width: 70,
        padding: [5, 0],
        border: 'T',
    },
    {
        id: 'total',
        header: 'Total',
        width: 70,
        padding: [5, 0],
        border: 'T',
    },
]);

table.addBody([
    {product: 'Product 1', quantity: 1, price: 20.10, total: 20.10},
    {product: 'Product 2', quantity: 4, price: 4.00, total: 16.00},
    {product: 'Product 3', quantity: 2, price: 17.85, total: 35.70}
]);

Without this change:
Selection_005

With this change:
Selection_006

I could made it working with setting the y posititon of the pdf itself before creating the table (doc.y = doc.page.margins.top;), since I'm not familiar with best practises I don't know if that would be the way to go. My first intention was by passing it to the constructor, which surprisingly didn't work.

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

Successfully merging this pull request may close these issues.

1 participant