dolanmiu / docx

Easily generate and modify .docx files with JS/TS with a nice declarative API. Works for Node and on the Browser.
https://docx.js.org/
MIT License
4.24k stars 479 forks source link

Table in tabel: The inner version is not rendering the correct structure so it doesn't seem to work like that. #2724

Open NguyenBa1410 opened 2 weeks ago

NguyenBa1410 commented 2 weeks ago

`async generateSaudiHealthCertSeafoodDocx( query: HealthCertFoodDto, sellerStaff: Staff, ) { const PHONE_NUMBER = '07534563401'; const PROFILE_URL = 'https://www.linkedin.com/in/dolan1'; const EMAIL = 'docx@com';

try {
  const pageWidth = 11908.8;
  const marginSize = 720;
  const displaySize = pageWidth - marginSize * 2;
  const widthIndex = 720;
  const borderNone: ITableCellBorders = {
    top: { style: 'single', color: 'FFFFFF' },
    bottom: { style: 'single', color: 'FFFFFF' },
    left: { style: 'single', color: 'FFFFFF' },
    right: { style: 'single', color: 'FFFFFF' },
  };
  const doc = new Document({
    styles: {
      paragraphStyles: [
        {
          id: 'normalPara',
          name: 'Normal Para',
          basedOn: 'Normal',
          quickFormat: true,
          run: {
            font: 'Calibri',
            size: 12,
          },
          paragraph: {
            spacing: {
              line: 240,
              before: 0,
              after: 0,
            },
            rightTabStop: TabStopPosition.MAX,
            leftTabStop: 453.543307087,
          },
        },
        {
          id: 'boldPara',
          name: 'Bold Para',
          basedOn: 'Bold',
          quickFormat: true,
          run: {
            font: 'Calibri',
            size: 18,
            bold: true,
          },
          paragraph: {
            spacing: {
              line: 240,
              before: 0,
              after: 0,
            },
            rightTabStop: TabStopPosition.MAX,
            leftTabStop: 453.543307087,
          },
        },
      ],
    },
    sections: [
      {
        properties: {
          page: {
            margin: {
              top: marginSize,
              left: marginSize,
              right: marginSize,
              bottom: marginSize,
            },
          },
        },
        children: [
          new Table({
            columnWidths: [displaySize / 2, displaySize / 2],
            rows: [
              new TableRow({
                children: [
                  new TableCell({
                    borders: borderNone,
                    children: [
                      new Paragraph({
                        children: [
                          new TextRun({
                            text: 'Health Certificate for Export of Products of Aquatic Animal Origin to the Kingdom of Saudi Arabia (KSA)',
                            color: 'FF0000',
                          }),
                        ],
                        style: 'boldPara',
                        alignment: AlignmentType.CENTER,
                      }),
                    ],
                  }),
                  new TableCell({
                    borders: borderNone,
                    children: [
                      new Paragraph({
                        children: [
                          new TextRun({
                            text: 'الشهادة صحية لتصدير املنتجات البحرية ذات أصل حيواني إلى اململكة العربية السعودية',
                            color: 'FF0000',
                          }),
                        ],
                        style: 'boldPara',
                        alignment: AlignmentType.CENTER,
                        bidirectional: true,
                      }),
                    ],
                  }),
                ],
              }),
            ],
          }),
          new Table({
            columnWidths: [
              widthIndex,
              displaySize / 2 - widthIndex,
              widthIndex,
              displaySize / 2 - widthIndex,
            ],
            rows: [
              new TableRow({
                children: [
                  new TableCell({
                    children: [
                      new Paragraph({
                        text: 'I.1',
                        style: 'boldPara',
                      }),
                    ],
                  }),
                  new TableCell({
                    children: [
                      new Table({
                        rows: [
                          new TableRow({
                            children: [],
                          }),
                        ],
                      }),
                      new Paragraph({
                        children: [
                          new TextRun({
                            text: 'Consignor (Exporter)',
                          }),
                          new TextRun({
                            text: '\t', // Khoảng cách giữa các đoạn văn
                          }),
                          new TextRun({
                            text: 'المرسل (المصدر)',
                          }),
                        ],
                        style: 'boldPara',
                      }),
                    ],
                  }),
                  new TableCell({
                    children: [
                      new Paragraph({
                        text: 'I.2',
                        style: 'boldPara',
                      }),
                    ],
                  }),
                  new TableCell({
                    children: [
                      new Paragraph({
                        text: 'Certificate Reference No.',
                        style: 'boldPara',
                      }),
                    ],
                  }),
                ],
              }),
            ],
          }),
        ],
      },
    ],
  });

  const file = await Packer.toBuffer(doc);

  return file;
} catch (error) {
  console.error('Error generating document:', error);
  throw new Error('Document generation failed');
}

}`

new TableCell({ children: [ new Table({ rows: [ new TableRow({ children: [], }), ], }), new Paragraph({ children: [ new TextRun({ text: 'Consignor (Exporter)', }), new TextRun({ text: '\t', // Khoảng cách giữa các đoạn văn }), new TextRun({ text: 'المرسل (المصدر)', }), ], style: 'boldPara', }), ], }),

The internal tableCell seems to be malfunctioning and it has lost all the structure of a table.

Thank you for raising an issue to docx

Please do not raise an issue unless it is an issue.