Uso docxtemplater para colocar mi imagen en el archivo docx pero sigue terminando con una imagen de error
Intenté enviarlo como lista de archivos, base64, búfer pero nunca funciona
este es mi código, gracias de antemano.
export const generateDocument = async (data, file, setFile) => { const opts = { centered: true, getImage: function (tagValue, tagName) { return PizZipUtils.getBinaryContent(data.imageData[0], (error, content) => content); }, getSize: function (img, tagValue, tagName) { return [110, 130]; }, }; loadFile(data.logo === "BBS" ? './BBS.docx' : "./PPT.docx", (error, content) => { if (error) { throw error; } let zip = new PizZip(content); let doc = new Docxtemplater(zip, { modules: [new ImageModule(opts)], paragraphLoop: true, linebreaks: true }); doc.setData({...data}); // render the document (replace all occurences of {first_name} by John, {last_name} by Doe, ...) doc.render(); let out = doc.getZip().generate({ type: "blob", mimeType: "docxType", }); saveAs(out, 'Resume.docx'); });Puede haber otras formas, pero definitivamente funciona si su función getImage() devuelve un ArrayBuffer con los datos de la imagen. Buffer parece ser parte de node.js Si su código se ejecuta en el navegador, puede npm install buffer .
Si los datos de su imagen están codificados en base64, puede usar esta función de conversión:
import { Buffer } from "buffer"; function base64DataURLToArrayBuffer(stringBase64) { const binaryString = window.atob(stringBase64); const len = binaryString.length; const bytes = Buffer.alloc(len, 0); for (let i = 0; i < len; i += 1) { const ascii = binaryString.charCodeAt(i); bytes[i] = ascii; } return bytes; }