TurboDocx / html-to-docx

HTML to DOCX converter
https://TurboDocx.com
MIT License
48 stars 5 forks source link

DOCX File Not Opening in Microsoft Word When Using Margins Configuration #35

Closed maxdinech closed 4 months ago

maxdinech commented 4 months ago

Hello,

I encountered an issue where DOCX files generated using the html-to-docx library fail to open in Microsoft Word. The error message displayed by Word is:

Word encountered an error trying to open the file.

After investigating the problem, I discovered that the issue is related to the margins object in the documentOptions configuration. When I remove the margins object, the DOCX file is generated correctly and opens without any issues in Word.

Steps to Reproduce:

  1. Use the following code to generate a DOCX file:

    import HTMLtoDOCX from '@turbodocx/html-to-docx';
    
    const htmlContent = `
      <html>
        <body>
          <h1>Hello, World!</h1>
          <p>This is a test document.</p>
        </body>
      </html>
    `;
    
    const documentOptions = {
      margins: {
        top: 1000,
        right: 1000,
        bottom: 1000,
        left: 1000,
      },
      font: 'Calibri',
    };
    
    const docxBuffer = await HTMLtoDOCX(htmlContent, '', documentOptions, '');
  2. Attempt to open the generated DOCX file in Microsoft Word.

Observed Behavior:

Expected Behavior:

Workaround:

Example of Working Code (without margins):

import HTMLtoDOCX from '@turbodocx/html-to-docx';

const htmlContent = `
  <html>
    <body>
      <h1>Hello, World!</h1>
      <p>This is a test document.</p>
    </body>
  </html>
`;

const documentOptions = {
  font: 'Calibri',
};

const docxBuffer = await HTMLtoDOCX(htmlContent, '', documentOptions, '');

Environment:

nicolasiscoding commented 4 months ago

Thank you for submitting, we will take a look

nicolasiscoding commented 4 months ago

@maxdinech fixed in v1.9.4