TransWikia.com

Strange behavior of href on .pdf file in hyperref

TeX - LaTeX Asked by Noricks on March 6, 2021

Thank you for taking the time to read this problem.
I want to use href to create a hyperlink to my other PDF file. Unfortunately, there are two cases of problems:

  1. If I use href{run:pathtofile}{text}, the code works fine for PDF readers like Adobe Acrobat or SmartPDF. However, they don’t work on web browsers.

  2. When I use href{raletive-pathtofile}{text} when using the .txt suffix, the code works well. However, when using .pdf suffix, the href link will be considered as an anchor and the link does not work. Meanwhile, url command does not have this problem.

Here is some test code:

documentclass{article}
usepackage[colorlinks,linkcolor=red,anchorcolor=blue,citecolor=green,CJKbookmarks=True]{hyperref}
pagestyle{plain}

begin{document}
    section{Problem}
    When use URL .pdf: url{./test/test.pdf} 
    When use href .pdf: href{./test/test.pdf}{testfile} 
    newline
    When use URL .txt: url{./test/test.txt} 
    When use href .txt: href{./test/test.txt}{testfile} 
    newline
    When use URL without extension: url{./test/test} 
    When use href without extension: href{./test/test}{testfile} 
end{document}

Environment setting:

Windows 2004 64-bit
TexLive 2019
XeTex, pdfTex

One Answer

You can compile the following document to understand the behavior of hyperref:

documentclass{article}
pdfobjcompresslevel=0
pdfcompresslevel=0
usepackage[colorlinks,linkcolor=red,anchorcolor=blue,citecolor=green,CJKbookmarks=True]{hyperref}
pagestyle{plain}

begin{document}
section{Problem}
When use URL .pdf: url{./test/test.pdf} 
When use href .pdf: href{./test/test.pdf}{testfile} 
newline
When use URL .txt: url{./test/test.txt} 
When use href .txt: href{./test/test.txt}{testfile} 
newline
When use URL without extension: url{./test/test} 
When use href without extension: href{./test/test}{testfile} 
end{document}

The produced PDF is now uncompressed so you can open it with your favorite text editor.

First you find all the annotations (this is what links are called in a PDF document) added to the page by their stream number (in the same order as in the source file):

/Annots [ 7 0 R 8 0 R 9 0 R 10 0 R 11 0 R 12 0 R ]

Then you find the stream object (from #7 to #12):

7 0 obj
<<
/Type /Annot
/Border[0 0 0]/H/I/C[0 1 1]
/Rect [227.749 632.204 308.197 643.329]
/Subtype/Link/A<</Type/Action/S/URI/URI(./test/test.pdf)>>
>>
endobj
8 0 obj
<<
/Type /Annot
/Border[0 0 0]/H/I/C[0 .5 .5]
/Rect [223.626 620.526 254.455 631.374]
/Subtype/Link/A<</F(./test/test.pdf)/S/GoToR/D[0/Fit]>>
>>
endobj
9 0 obj
<<
/Type /Annot
/Border[0 0 0]/H/I/C[0 1 1]
/Rect [226.366 597.722 306.813 607.464]
/Subtype/Link/A<</Type/Action/S/URI/URI(./test/test.txt)>>
>>
endobj
10 0 obj
<<
/Type /Annot
/Border[0 0 0]/H/I/C[0 1 1]
/Rect [222.242 586.598 253.071 595.509]
/Subtype/Link/A<</Type/Action/S/URI/URI(./test/test.txt)>>
>>
endobj
11 0 obj
<<
/Type /Annot
/Border[0 0 0]/H/I/C[0 1 1]
/Rect [288.411 561.857 347.937 571.598]
/Subtype/Link/A<</Type/Action/S/URI/URI(./test/test)>>
>>
endobj
12 0 obj
<<
/Type /Annot
/Border[0 0 0]/H/I/C[0 .5 .5]
/Rect [284.288 550.732 315.116 559.643]
/Subtype/Link/A<</F(./test/test.pdf)/S/GoToR/D[0/Fit]>>
>>
endobj

In each case, streams contains a /Subtype/Link/A annotation.

All links produced by url and the link to .txt file produced by href are /Type/Action/S/URI (a link to an external URI/URL) with correct link URL (relative URL).

The explicit link to .pdf file produced by href (#7 obj) is F(./test/test.pdf)/S/GoToR/D[0/Fit] (a link to an external PDF file). This is convenient because the PDF viewer then knows that it can open the file itself.

The link from a URL without an extension generated by href looks weird (the .pdf extension is added to the link).

Note about PDF viewer of web browers

In PDF viewer of web browsers, if your base URL (the URL of your document) uses the file: scheme, all relative links seem disabled (presumably for security reasons).

Answered by Paul Gaborit on March 6, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP