Docx: Generate buffer from image in browser

Created on 10 Jan 2019  路  6Comments  路  Source: dolanmiu/docx

So I'm using the library on browser-side without nodeJS.
In the docs the method document.createImage() requires a buffer. I'm using a library to implement the nodeJS buffer functionality in the browser but I honestly have no clue about how to proceed without having to implement fileSystem functionality.

I'd like to use an URL to load the image.
Thanks for any help!

Most helpful comment

Buffer is only supported in NodeJS, there is no such concept as buffer in the browser world!

Fortunately, createImage() can also take in a base64 string or a Uint8Array or ArrayBuffer, which are in the browser world:

https://github.com/dolanmiu/docx/blob/master/src/file/file.ts#L139

All 6 comments

Buffer is only supported in NodeJS, there is no such concept as buffer in the browser world!

Fortunately, createImage() can also take in a base64 string or a Uint8Array or ArrayBuffer, which are in the browser world:

https://github.com/dolanmiu/docx/blob/master/src/file/file.ts#L139

Thanks! Also, may I ask why can't images be inserted directly? I'm not very familiar with nodeJS environment.

Images cant be inserted directly like a file path? C:\cat.png?

It used to be like that, but then it wouldn't work for browsers. Can't access C drive on the browser

And what about URLs? (in the browser environment)

I wanted that de-coupling from the implementation

I wanted docx to not care about how/where/when the image was gotten from. It can be from NodeJS buffers, or through Angular's this.http.get. All docx needed to know was the data.

It's the same reason why the packers are the way they are today

Makes sense. Thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

efx picture efx  路  6Comments

zeco2309 picture zeco2309  路  5Comments

brunogaleski picture brunogaleski  路  6Comments

GiladFeder picture GiladFeder  路  6Comments

iainfreestone picture iainfreestone  路  3Comments