问题
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