Search code examples
hyperlinkannotationspdf-generationpdfkitpdfrw

How to convert PDF web links to file open actions with python pdfrw library


I'm using pdfkit to convert html to pdf which works great, but the external links in the pdf are web links.

The pdf viewer that we are using does not recognize the pdf web links, but file open actions do work.

I've been trying to change the pdf link annotation from a web link to a file open action with the pdfrw library.

I tried to edit the pdf annotation with the following code, but it's not working.

        annot.A.update(pdfrw.PdfDict( S='/Launch'))
        annot.A.update(pdfrw.PdfDict( F={}))
        annot.A.F.update(pdfrw.PdfDict( Type='/Filespec'))
        annot.A.F.update(pdfrw.PdfDict( F='(part-1.pdf)'))
        annot.A.pop('/Type')
        annot.A.pop('/URI')

The link shows up in the new pdf, but there is no action or destination file associated with it.


Solution

  • So after a similar battle today...

    you can't define S='/Launch' as string like that. You have to use:

      annot.A.update(pdfrw.PdfDict( S=pdfrw.PdfName('Launch')))
    

    Similarly with filespec etc. You may also need to take the () off the filename - as that seemed to break it, at least in Apple Preview.