类型“Blob”不可分配给类型“string |”缓冲器| Uint8Array |数组缓冲区'

问题描述 投票:0回答:1

我尝试按照此示例中的生成 URL 代码在标头中添加图像。

但是,我收到以下错误消息:

Type 'Blob' is not assignable to type 'string | Buffer | Uint8Array | ArrayBuffer'.
  Type 'Blob' is missing the following properties from type 'ArrayBuffer': byteLength, [Symbol.toStringTag]  TS2322

    25 |               children: [
    26 |                 new ImageRun({
  > 27 |                   data: blob,
       |                   ^
    28 |                   transformation: {
    29 |                     width: 100,
    30 |                     height: 100

完整代码:

  const runWord=async()=>{
    const testImg = await fetch("https://raw.githubusercontent.com/dolanmiu/docx/master/demo/images/cat.jpg")
                          .then((r)=>r.blob());
    const doc = new docs.Document({
      sections:[
        {
          properties:{},
          children:[
            new docs.Paragraph({
              children:[
                new docs.TextRun("Hello World"),
                new docs.TextRun({
                  text: "Foo Bar",
                  bold: true
                })
              ]
            }),
            new docs.Paragraph({
              children:[
                new docs.ImageRun({
                  data: testImg,
                  transformation: {
                    width:100,
                    height:100
                  }
                })
              ]
            })
          ]
        }
      ]
    })
    docs.Packer.toBlob(doc).then((blob)=>{
      saveAs(blob,"Test Document.docx");
    })
  }
reactjs docx react-tsx
1个回答
0
投票
const runWord = async () => {
  const response = await fetch("https://raw.githubusercontent.com/dolanmiu/docx/master/demo/images/cat.jpg");
  const blob = await response.blob();
  const arrayBuffer = await blob.arrayBuffer();
  const uint8Array = new Uint8Array(arrayBuffer);

  const doc = new docs.Document({
    sections: [
      {
        properties: {},
        children: [
          new docs.Paragraph({
            children: [
              new docs.TextRun("Hello World"),
              new docs.TextRun({
                text: "Foo Bar",
                bold: true
              })
            ]
          }),
          new docs.Paragraph({
            children: [
              new docs.ImageRun({
                data: uint8Array,
                transformation: {
                  width: 100,
                  height: 100
                }
              })
            ]
          })
        ]
      }
    ]
  });

  docs.Packer.toBlob(doc).then((blob) => {
    saveAs(blob, "Test Document.docx");
  });
};
© www.soinside.com 2019 - 2024. All rights reserved.