guigrpa / docx-templates

Template-based docx report creation
MIT License
909 stars 145 forks source link

Watermark image support #297

Open victormartinezj opened 1 year ago

victormartinezj commented 1 year ago

I tried to add an image for watermark, in the template I added with text watermark the next snipped:

{{ IMAGE watermarkImage() }}

When using the library I used the next code:


  const buffer = await createReport({
    template,
    additionalJsContext: {
      watermarkImage: async () => {
          const url = "someUrl";
          const response = await axios(url, { responseType: "arraybuffer" });
          const imageBuffer = Buffer.from(response.data, "binary").toString("base64");
          return { width: 13, height: 7, data: imageBuffer, extension: ".jpg" };
          },
    },
    cmdDelimiter: ["{{", "}}"],
 });

The same function works great when the image is not watermark, but when I used it in watermark it's not working and the result is the same text in the template: {{ IMAGE watermarkImage() }} ![water](https://user-images.githubusercontent.com/35510826/220212098-dc01b7e7-4ac1-4602-ac1f-c7e708d7635b.png) I suppose that the problem reside in the configuration of watermarks not in the library itself, I think that the configuration of 'text watermark' and 'image watermark' in word it's different and the library can't convert the text watermark I added: {{ IMAGE watermarkImage() }} In an image watermark. Would it be possible to add that functionality? Is there an actual functionality and I did something wrong? Thanks.
jjhbw commented 1 year ago

Sorry for the late reply, but the answer only just occurred to me. I don't think page watermarks can be injected in the same way that regular images are injected. This would require custom manipulation of the docx XML tree, so I've marked this issue as a feature request.