Search code examples
imagems-wordhyperlinkdocx

How to implement external hyperlink within image using docx js


I am facing problems trying to embed a hyperlink in an image. The code from the documentation (https://github.com/dolanmiu/docx/blob/master/demo/35-hyperlinks.ts) beginning at line 84 is not working for me:

new Paragraph({
  children: [
      new ExternalHyperlink({
          children: [
              new ImageRun({
                  data: fs.readFileSync("./demo/images/image1.jpeg"),
                  transformation: {
                      width: 100,
                      height: 100,
                  },
              }),
          ],
          link: "http://www.google.com",
      }),
  ],
})

When I copy this very snippet into my code, the image is rendered as expected but there is no link being integrated. It seems like the ExternalHyperLink element is not working specifically with images, as it works flawlessly when trying to add the link to a TextRun element. I tested also if it was a problem related to the code being positioned within the header section but the behaviour is the same when shifting the code to the body of the document.

I am using the latest version of docx. Can anyone give me an advice what is going wrong here?


Solution

  • For anybody who is also stumbling across this problem:

    After further analysis together with the creator of the docx js library we were able to identify this behaviour as an existing bug. Will be solved with the next major release 8.0.0 of the package, see also: https://github.com/dolanmiu/docx/issues/1699