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

Write several texts on the same page with defined Y position #1556

Open
serolgames opened this issue Oct 20, 2024 · 0 comments
Open

Write several texts on the same page with defined Y position #1556

serolgames opened this issue Oct 20, 2024 · 0 comments

Comments

@serolgames
Copy link

serolgames commented Oct 20, 2024

Hello ! This question is quite important i'm stuck for a professionnal work.

How can I write several texts on the same page with a defined Y position ?
My goal is to create a table and then write the content of the columns on the same line.
Like this : column1 | column2 | column3

Easy you would say :

export type PDFTableLine = { text: string; width: number }

    private writeLine = (content: PDFTableLine[]) => 
    {
        let offsetX = this.doc.page.margins.left;
        const startY = this.doc.y;
        let maxLineHeight = 0;
    
        //checking what column will be the tallest
        for (let i = 0; i < content.length; i++) {
            const textHeight = this.doc.heightOfString(content[i].text, {
                width: content[i].width,
            });
            maxLineHeight = Math.max(maxLineHeight, textHeight);
        }
        
        //write all the content of columns
        for (let i = 0; i < content.length; i++) 
        {
            this.doc.text(content[i].text, offsetX, startY, {
                width: content[i].width,
            })
    
            offsetX += content[i].width;//set the offset so the next text will be displayed on the next column
        }
    
        //avoid overlap text with setting the next line below the tallest column
        this.doc.y = startY + maxLineHeight;
    
        this.writeLineSeparator(); //basically create a line with stroke()
    }

It's working perfectly ! Unless... If the first text is quite long and pdfkit creates a page to write the rest of the text, then the others column will be displayed on the next page from the Y position from the top of the page. I want all my columns to starts on the same page even if the first text creates a new page. How can I achieve that ?

(Ps: the issue also exist in pdfkit-table package) that's why i'm creating my own class to build tables

  • pdfkit version: 0.15.0
  • Node version: v20.15.1
  • Operating System: Ubuntu linux
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

No branches or pull requests

1 participant