docx icon indicating copy to clipboard operation
docx copied to clipboard

\t not being recognized in google docs

Open fcpaulmndza opened this issue 3 years ago • 6 comments

Code:

footers: {
          default: new Footer({
            children: [
              new Paragraph({
                style: 'footer',
                children: [
                  new TextRun("Test\t"),
                  new TextRun({
                    children: ['Page ', PageNumber.CURRENT, ' of ', PageNumber.TOTAL_PAGES],
                  }),
                ],
                tabStops: [
                  {
                    type: TabStopType.RIGHT,
                    position: TabStopPosition.MAX,
                  }
                ],
              })
            ],
          }),
        }

Result:

Screen Shot 2021-11-09 at 10 31 51 PM

Checking the docx file in Google Docs

fcpaulmndza avatar Nov 09 '21 14:11 fcpaulmndza

Hi there! I think the understanding of tabs should be so that \t affects only one textRun, so this works for me:

const fs = require('fs');
const { Document, Packer, Paragraph, TextRun, Footer, TabStopType, TabStopPosition, PageNumber}  = require('docx');

const doc = new Document({
    sections: [
        {
            children: [
                new Paragraph({
                    children: [
                        new TextRun('Lorem \t ipsum dolor sit amet'),
                    ],
                }),
            ],
            footers: {
                default: new Footer({
                    children: [
                    new Paragraph({
                        style: 'footer',
                        children: [
                            new TextRun({
                              children: ['Test \t', 'Page ', PageNumber.CURRENT, ' of ', PageNumber.TOTAL_PAGES],
                            }),
                        ],
                        tabStops: [{
                            type: TabStopType.RIGHT,
                            position: TabStopPosition.MAX,
                        }],
                    })
                    ],
                }),
            }
        },
    ],
});

Packer.toBuffer(doc).then((buffer) => {
    fs.writeFileSync("tabTest.docx", buffer);
});

image

anti-the-social avatar Nov 10 '21 07:11 anti-the-social

And just in case, if you try to produce page number on the right via tabs then the footer text length has to be smaller, than page size otherwise it goes fancy looking (not on the same line as top line of the text) image

anti-the-social avatar Nov 10 '21 07:11 anti-the-social

Thanks @anti-the-social! but I'm still getting the same result

fcpaulmndza avatar Nov 12 '21 06:11 fcpaulmndza

Seems like a compatibility issue. It works in MS Word but not in Google docs @anti-the-social

fcpaulmndza avatar Nov 12 '21 06:11 fcpaulmndza

I think this issue should be sort of risen to the google docs, not to this particular library. And for you, I'd suggest using iframe for page numbering https://docx.js.org/#/usage/text-frames?id=intro Be careful with it, since alignment to the right isn't working yet https://github.com/dolanmiu/docx/issues/933 And one more related issue https://github.com/dolanmiu/docx/issues/1082

anti-the-social avatar Nov 12 '21 20:11 anti-the-social

I still can't get the right tab to be recognized in google docs

CedYF avatar Aug 09 '22 00:08 CedYF

Fixed in a PR

Now there is a dedicated Tab object which has perfect compatability

Example of usage:

https://github.com/dolanmiu/docx/blob/master/demo/1-basic.ts

dolanmiu avatar Oct 26 '22 23:10 dolanmiu