Docx: Adding Hyperlink breaks Word document

Created on 27 Aug 2019  路  11Comments  路  Source: dolanmiu/docx

I'm on 5.0.0rc5. When I try to add a hyperlink to the document, I get the following message (while opening the word document): 'Word found unreadable content in "example (5).docx". Do you want to recover the contents of this document? If you trust the source of this document, click Yes.'

See the stackblitz below, note the versions of docx and file-saver in package.json

https://stackblitz.com/edit/angular-cny1zr?file=src/app/app.component.ts

Am I adding the hyperlinks wrong?

Thanks in advance!

P.s. please excuse my not using a proper bug report template.

Edit, I'm on MacOS by the way

Most helpful comment

Hi, I have the same problem:

After adding a link to the document, MS Word can't open it without alert (tested on Windows and macOS):
photo5195323579934288889

I used the code from this example:
https://github.com/dolanmiu/docx/blob/master/demo/35-hyperlinks.ts

Package version: 5.0.0rc5

Resulting document example:
https://drive.google.com/file/d/1HlGzCMdbTVUZw2cP24hXgB9BycGkRG0T/

All 11 comments

@dolanmiu Is this something that is known and/or being worked on? Otherwise, I'll move on to an alternative solution.

p.s. Great library, awesome work

Hi, I have the same problem:

After adding a link to the document, MS Word can't open it without alert (tested on Windows and macOS):
photo5195323579934288889

I used the code from this example:
https://github.com/dolanmiu/docx/blob/master/demo/35-hyperlinks.ts

Package version: 5.0.0rc5

Resulting document example:
https://drive.google.com/file/d/1HlGzCMdbTVUZw2cP24hXgB9BycGkRG0T/

same problem on version: 5.0.0-rc7

new Paragraph({
  children: [
    document.createHyperlink('https://www.google.com', 'Awesome link')
  ]
})

Is this the right way to create a link ?

I found the problem

in document.xml.rels my hyperlink as a duplicate id (here rId5) :

duplicate_id

i dont know how if it's the hyperlink that as the wrong relationshipsCount or if it's header || footer

but removind those duplicate ids fixes my problem

440 fix it for me too

@Sraleik How did #440 fix it for you?

as said by @ramonmata
It's a hacky fix.
I'm trying to find a better one

but for now modifying the library like that works :

https://github.com/ramonmata/docx/blob/104c4c4deb84e1a5deb47ac7fc4dc156632f4cc6/src/file/relationships/relationships.ts#L27

@dolanmiu We could just add some method like lodash's uniqueId which will return unique numbers.

+ import getUniqueId from '../utils/get-unique-id';

- this.linkId = relationshipsCount + 1;
+ this.linkId = getUniqueId();

What do you think about this approach? I can make a PR if the solution is OK for you.

I think this PR does just that, it uses a well known library to generate the unique ID. Only problem is that it includes letters and numbers, but apparently it still works as expected:

https://github.com/dolanmiu/docx/pull/448

I will test it out locally before I merge

Thanks @dolanmiu! You're a lifesaver, really wanted this issue to be fixed

Fixed with #448

Was this page helpful?
0 / 5 - 0 ratings

Related issues

madarche picture madarche  路  5Comments

iainfreestone picture iainfreestone  路  3Comments

efx picture efx  路  5Comments

zahrafali picture zahrafali  路  5Comments

GiladFeder picture GiladFeder  路  6Comments