question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

React ImageRun Typescript Error: Type 'Blob' is not assignable to type 'string | Buffer | Uint8Array | ArrayBuffer'

See original GitHub issue

Hi everyone,

I’m trying to follow the generate URL code from this example to add an image within a header.

However, I get the following error message:

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

Here is the full code:

import "./App.css";
import { saveAs } from "file-saver";
import {Document, Footer, Header, ImageRun, Packer, PageBreak, Paragraph, TextRun} from "docx";

export default function App() {
  const generate = async () => {

    const blob = await fetch(
      "https://raw.githubusercontent.com/dolanmiu/docx/master/demo/images/cat.jpg"
    ).then(r => {
        return r.blob();
    });

    const doc = new Document({
      sections: [
        {
          properties: {
            titlePage: true // allows for first and default header text
          },
          headers: {
            default: new Header({
              children: [
                new ImageRun({
                  data: blob, // Type 'Blob' error here
                  transformation: {
                    width: 100,
                    height: 100
                  }
                }),
                new Paragraph({
                  text: "Default - Some more header text"
                })
              ]
            }),
            first: new Header({
              children: [
                new Paragraph({
                  text: "First header text"
                }),
                new Paragraph({
                  text: "First - Some more header text"
                })
              ]
            })
          },
          footers: {
            default: new Footer({
              children: [
                new Paragraph({
                  text: "Default Footer text"
                })
              ]
            }),
            first: new Footer({
              children: [
                new Paragraph({
                  text: "First Cool Footer text"
                })
              ]
            })
          },
          children: [
            new Paragraph({
              children: [new TextRun("Hello World 1"), new PageBreak()]
            }),
            new Paragraph({
              children: [new TextRun("Hello World 2"), new PageBreak()]
            }),
            new Paragraph({
              children: [new TextRun("Hello World 3"), new PageBreak()]
            }),
            new Paragraph({
              children: [new TextRun("Hello World 4"), new PageBreak()]
            }),
            new Paragraph({
              children: [new TextRun("Hello World 5"), new PageBreak()]
            })
          ]
        }
      ]
    });

    Packer.toBlob(doc).then((blob) => {
      console.log(blob);
      saveAs(blob, "example.docx");
      console.log("Document created successfully");
    });
  };

  return (
    <div className="App">
      <h1>Hello CodeSandbox</h1>
      <p>
        Start editing to see some magic happen :)
        <button onClick={generate}>Generate Report with docx!</button>
      </p>
    </div>
  );
}

I get that the error is telling me that Type “Blob” isn’t an assignable type, but why is it working in the example?

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
anti-the-socialcommented, Apr 19, 2022

Hi!

I assume you like to pass buffer into ImageRun, not a blob. So some conversion is needed. https://stackoverflow.com/questions/11821096/what-is-the-difference-between-an-arraybuffer-and-a-blob

I can be wrong, but data: Buffer.from() works for me

Please close an issue if this solved your issue.

0reactions
dolanmiucommented, May 6, 2022

Closing as it’s more of a Q&A

Read more comments on GitHub >

github_iconTop Results From Across the Web

ArrayBuffer' is not assignable to type 'string' - Stack Overflow
The error message says it all. You declare a string type of csv variable. You then assign string | ArrayBuffer type (of reader.result...
Read more >
Type error in Buffer.from() · Issue #23155 · microsoft/TypeScript
Type 'Buffer' is not assignable to type 'string'. This is weird, because Buffer.from can accept a string or a Buffer. The d.ts for...
Read more >
Blob.arrayBuffer() - Web APIs - MDN Web Docs
A promise that resolves with an ArrayBuffer that contains the blob's data in binary form. Exceptions. While this method doesn't throw exceptions ...
Read more >
Buffer | typescript - v3.7.7
Defined in node_modules/@types/node/globals.d.ts:83. Allocates a new buffer containing the given {str}. deprecated. since v10.0.0 - Use Buffer.from(string[, ...
Read more >
How to convert ArrayBuffer to and from String
See Easier ArrayBuffer <-> String conversion with the Encoding API for more details. ArrayBuffers are used to transport raw data and several new ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found