简体   繁体   English

如何将带有 npm 'docx' 包的图像添加到 Word 文档?

[英]How to add images with npm 'docx' package to word document?

I found this npm package docx that helps to generate word document files with javascript: https://github.com/dolanmiu/docx我发现这个 npm 包docx有助于用 javascript 生成 word 文档文件: https : //github.com/dolanmiu/docx

I want to add an image to my doc but I can't seem to figure out how, or what's happening.我想在我的doc添加一个图像,但我似乎无法弄清楚是如何发生的,或者发生了什么。 I have spent quite a few hours on this, but the documentation isn't as comprehensive enough for a noob like me.我在这上面花了好几个小时,但是文档对于像我这样的菜鸟来说还不够全面。

Here is what I tried:这是我尝试过的:

Attempt #1:尝试#1:

import React, { useState } from "react";
import * as fs from "fs";
import moment from "moment";
import { Document, Packer, Paragraph, Media } from "docx";
import { saveAs } from "file-saver";
const GenerateDoc = () => {
    const doc = new Document();
    const logo = Media.addImage(doc, fs.readFileSync("myImage.png"), 200, 200); // ERROR HERE: "fs.readFileSync is not a function
    doc.addSection({children: [new Paragraph(logo)]});
    const download = () => {
        // download function...
    };
    return (
        <div>
            <button onClick={() => {download()}}>Generate</button>
        </div>
    );
};
export default GenerateDoc;

Output #1: TypeError: fs__WEBPACK_IMPORTED_MODULE_2__.readFileSync is not a function输出 #1:类型错误:fs__WEBPACK_IMPORTED_MODULE_2__.readFileSync 不是函数

Attempt #2:尝试#2:

import React, { useState } from "react";
import moment from "moment";
import { Document, Packer, Paragraph, Media } from "docx";
import { saveAs } from "file-saver";
const GenerateDoc = () => {
    const doc = new Document();
    const logo = doc.createImage("myImage.png"); // ERROR HERE: doc.createImage is not a function
    doc.addSection({children: [new Paragraph(logo)]});
    const download = () => {
        // download function...
    };
    return (
        <div>
            <button onClick={() => {download()}}>Generate</button>
        </div>
    );
};
export default GenerateDoc;

Output #2: TypeError: doc.createImage is not a function输出 #2:类型错误:doc.createImage 不是函数

What is working:什么工作:

import React, { useState } from "react";
import moment from "moment";
import { Document, Packer, Paragraph, Media } from "docx";
import { saveAs } from "file-saver";
const GenerateDoc = () => {
    const doc = new Document();
    const logo = Media.addImage(doc); // returns a blank image in the word document since I didn't specify a file.
    doc.addSection({
        children: [new Paragraph(logo)]
    });
    const download = () => {
        // download function...
    };
    return (
        <div>
            <button onClick={() => {download()}}>Generate</button>
        </div>
    );
};
export default GenerateDoc;

Here are the documentations:以下是文档:
- https://docx.js.org/#/usage/images - https://docx.js.org/#/usage/images
- https://runkit.com/dolanmiu/docx-demo5 - https://runkit.com/dolanmiu/docx-demo5
- https://github.com/dolanmiu/docx/wiki/Images - https://github.com/dolanmiu/docx/wiki/Images

Any help is GREATLY appreciated!!任何帮助是极大的赞赏!!

Thanks in advance!!提前致谢!!

Edit:编辑:

Attempt #3:尝试#3:

import React, { useState } from "react";
import moment from "moment";
import { Document, Packer, Paragraph, Media } from "docx";
import { saveAs } from "file-saver";
const GenerateDoc = () => {
    const doc = new Document();
    const logo = Media.addImage(doc, '../myImage.png'); // ERROR: InvalidCharacterError: Failed to execute 'atob' on 'Window': The string to be decoded is not correctly encoded.
    doc.addSection({
        children: [new Paragraph(logo)]
    });
    const download = () => {
        // download function...
    };
    return (
        <div>
            <button onClick={() => {download()}}>Generate</button>
        </div>
    );
};
export default GenerateDoc;

Output #3: InvalidCharacterError: Failed to execute 'atob' on 'Window': The string to be decoded is not correctly encoded.输出 #3:InvalidCharacterError:无法在 'Window' 上执行 'atob':要解码的字符串未正确编码。

DOCX author here, DOCX作者在这里,

Here is an example of docx with images in React (base64 and with URL request from the internet):这是一个 docx 的示例,在 React 中带有图像(base64 和来自互联网的 URL 请求):

https://stackblitz.com/edit/react-ts-qdqu7z https://stackblitz.com/edit/react-ts-qdqu7z

One way is by loading the file by import, converting to blob, and use ImageRun.一种方法是通过导入加载文件,转换为 blob,然后使用 ImageRun。

 import LetterHead from '../images/letterhead.jpg'; ... const export = async () => { const letterHead = await fetch(LetterHead); const doc = new Document({ sections: [ ... new ImageRun ({ data : await letterHead.blob() ... ... Packer.toBlob(doc)... }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM