我尝试按照此示例中的生成 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");
})
}
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");
});
};