Relative File Linking in PDF (Reportlab)

假如想象 提交于 2019-12-08 05:47:14

问题


Is it possible to link to a relative external document that isn't a PDF file from a PDF document?

I'm specifically thinking something like an excel spreadsheet.

In ReportLab I can add something like

Elements.append(Paragraph("<a href=\"pdf://linkedfile.pdf\">File</a>",style))

and it will successfully link to and open linkedfile.pdf in the same folder as my generated PDF, however the documentation doesn't mention any other file types other than pdf and obviously xls:// doesn't work.

Does anyone know if this is a limitation of PDF files and is prevented for security reasons? Or is this just something ReportLab doesn't support out the box? Could it be achieved by extending a ReportLab class?

Thanks.


回答1:


This is how I got relative links to files to work in reportlab:

from reportlab.pdfgen import canvas
from reportlab.lib.units import inch

# Create a canvas and add a rectangle to it
c = canvas.Canvas("link_test.pdf")
c.translate(inch, 9 * inch)
c.rect(inch,inch,1*inch,1*inch, fill=1)

# example.xls is in the same directory as the pdf
c.linkURL(r'example.xls', (inch, inch, 2*inch, 2*inch), relative=1)
c.save()

I'm using Adobe Reader 11.0.10.32. When I click the rectangle I get a few warnings, but file does open up after clicking "Allow" and "Yes".




回答2:


Have you tried using file:// instead of pdf://?. I can't test this at the moment and it may be dependent on the pdf-viewer / file-manager you are using.



来源:https://stackoverflow.com/questions/21331399/relative-file-linking-in-pdf-reportlab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!