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

Creating multiples pages with fragments of text -> Using BufferPages #1472

Open
vitucordeiro opened this issue Oct 30, 2023 · 0 comments
Open

Comments

@vitucordeiro
Copy link

vitucordeiro commented Oct 30, 2023

Bug Report

Description of the problem

I'm using an object with an interface to generate the PDF. The loop has two functions: Create the page and generate the content on the page. I've noticed that by looping and generating the pages, somehow, it's generating more pages than expected and the content is being distributed among those pages created.

Code sample

This is the interface.

export interface PageInfo {
  pages: {
      to:string;
      pageNumber: number;
      data:{
        content: [string | string[], any] | string[] ;
        coordinates?: {
            x: number;
            y: number;
        };
      }[]

  }[];
}```

This is the methods 

```ts 
private async createPDF(pageInfo:PageInfo, pagesLength:number) {
          return new Promise<Buffer>((resolve, reject) => {
          console.time('Creating PDF');
          // Configuração do Pdfkit
          const pdfDoc = pdfCreate 
          const buffers: Buffer[] = [];
          pdfDoc.on('data', buffers.push.bind(buffers));

          pdfDoc.on('end', () => {
            const pdfData = Buffer.concat(buffers);
            resolve(pdfData);
          });
      
          for (let j = 0; j < pagesLength; j++) {

            pdfDoc.addPage();
            pdfDoc.switchToPage(j);
            // bug 
            let data = pageInfo.pages[j].data;
            for(let i = 0; i < data.length; i++) {  
              let content = data[i].content
              let stringContent = content.toString();
              let { x, y } = data[i].coordinates;
              pdfDoc.text(stringContent, x, y, {
                align: 'justify',
                ellipsis: true
              })
            }
            pdfDoc.save();
          }
      
          pdfDoc.end();
          console.timeEnd('Creating PDF');
        });
      }
      
      async createAndSavePDF(pageInfo:PageInfo, pagesLength:number, Response) {
        try {
          
          const pdfData = await this.createPDF(pageInfo, pagesLength);
      
          Response.setHeader('Content-Length', pdfData.length);
          Response.setHeader('Content-Type', 'application/pdf');
          Response.setHeader('Content-Disposition', 'attachment;filename=out.pdf');
      
          Response.end(pdfData);
        } catch (error) {
          Response.status(500).json({ error: error.message });
        }
      }

This is the result of a example of PDF:
Screenshot from 2023-10-30 13-57-23

Your environment

  • pdfkit version: 0.13
  • Node version: 18.17
  • Browser version (if applicable):
  • Operating System: Ubuntu 18.04.6
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