I'm getting an image's binary data as an ArrayBuffer
, and inserting it into a document using docx.js:
getImageBinaryDataAsArrayBuffer().then(function (imageBuffer) {
var doc = new docx.Document();
var image = docx.Media.addImage(doc, imageBuffer);
doc.addSection({
children: [
new docx.Paragraph({
children: [image],
alignment: docx.AlignmentType.CENTER
}),
]
});
docx.Packer.toBlob(doc).then(function (blob) {
// save the doc somewhere
})
}).catch(function (err) {
console.log(err);
});
This works, but it seems like the image size is defaulting to 100x100, and is not preserving the aspect ratio. In the docx.js documentation for Images, it looks like you can specify the height and width when you add the image to the doc:
Media.addImage(doc, [IMAGE_BUFFER], [WIDTH], [HEIGHT], [POSITION_OPTIONS]);
but I don't know the image's natural height and width since all I'm working with is an ArrayBuffer
. (Can you determine an image's height and width from ArrayBuffer
data? My gut says no...)
Is there a way to tell docx.js to use the image's natural height and width? Or at least to preserve the aspect ratio?