html 转 world

  1. html-docx-js

  const image = '<style>image{width:100%}</style>';

  const htmlHead = `<head><meta charset="UTF-8" />${image}</head>`;`

  const htmlBody = `<body>${getHtml.main}</body>`;

  const htmlTemplate = `<!doctype html><html lang="en" class="downLoad">${htmlHead + htmlBody}</html>`;

  let htmlTemplateNew = htmlTemplate.replaceAll('  ', '&nbsp;&nbsp;');

  htmlTemplateNew = htmlTemplate.replaceAll('\n', '<br/>');

  htmlTemplateNew = htmlTemplateNew.replaceAll(

    `width="${554}" height="1">', 'width="620" height="1">`,

    '',

  );

  // 文件流,用来保存本地

  const converted = window.htmlDocx.asBlob(htmlTemplateNew, {

    mimeType:

      'application/vnd.openxmlformats-officedocument.wordprocessingml.document',

    header: getHtml.header,

    footer: getHtml.footer,

  });

  1. docx.js

export default function (command: Command, callback) {

  return function (options: IExportDocxOption) {

    const { fileName } = options;

    const {

      data: { header, main, footer },

    } = command.getValue();

    const doc = new Document({

      sections: [

        {

          headers: {

            default: new Header({

              children: convertElementListToDocxChildren(header || []),

            }),

          },

          footers: {

            default: new Footer({

              children: convertElementListToDocxChildren(footer || []),

            }),

          },

          children: convertElementListToDocxChildren(main || []),

        },

      ],

    });

    Packer.toBlob(doc).then((blob) => {

      // saveAs(blob, `${fileName}.docx`);

      callback(blob);

    });

    // Packer.toBuffer(doc).then((buffer) => {

    //   callback(buffer);

    // });

  };

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容