0

I am trying to link to local files in the pdf using absolute file path which contains spaces (this cannot be avoided since the files are being stored in iCloud whose local file path already contains a space). For \url there is an option obeyspaces which allows me to insert the file path directly without having to change space into %20. However this option does not work for \href. Is there some way to make \href absorb the input as \url does?

Below is a MWE.

\documentclass{article}

\PassOptionsToPackage{obeyspaces}{url}
\usepackage{hyperref}

\def\absolutepath{/Users/username/Library/Mobile Documents/com\string~apple\string~CloudDocs}
\def\bookpath{The Path/For The/PDF File.pdf}

\begin{document}

\href{file://\absolutepath/\bookpath#page=40}{p.\,40}

\url{file://\absolutepath/\bookpath#page=40}

\end{document}
1
  • the url link is faulty, it is not correctly percent encoded. The first is a file link, not an uri link. Commented May 25, 2023 at 9:26

1 Answer 1

3

If you uncompress the pdf by adding at the begin

\ExplSyntaxOn\sys_ensure_backend:\pdf_uncompress:\ExplSyntaxOff

you can inspect the link annotations.

For the url a (actually two) link annotation of action type URI is created:

/Subtype/Link
 /A<<
  /Type/Action
  /S/URI
/URI(file:///Users/username/Library/Mobile\040Documents/com~apple~CloudDocs/The\040Path/For\040The/PDF\040File.pdf#page=40)
  >>

The value is actually faulty (but will probably work in most viewers anyway): is used \040 for the spaces, but url's should be percent encoded.

For the first hyperref creates a file link (GotoR):

/Subtype/Link
/A<</S/GoToR
/F(/Users/username/Library/Mobile\040Documents/com~apple~CloudDocs/The\040Path/For\040The/PDF\040File.pdf)
/D(page=40)>>

It should normally work, if the local path is correct and no security settings prevent the opening of the file.

If you load the pdfmanagement with \DocumentMetadata, you have a few more options both to force the action type and to automatically percent encode urls:

\DocumentMetadata{} %load pdfmanagement
\documentclass{article}

\PassOptionsToPackage{obeyspaces}{url}
\usepackage{hyperref}

\def\absolutepath{/Users/username/Library/Mobile Documents/com\string~apple\string~CloudDocs}
\def\bookpath{The Path/For The/PDF File.pdf}

\begin{document}

\hrefpdf{file://\absolutepath/\bookpath#page=40}{p.\,40} %GoToR link

\hrefurl[urlencode]{file://\absolutepath/\bookpath#page=40}{p.\,40} %URI link

\url[urlencode]{file://\absolutepath/\bookpath#page=40}

\end{document}
2
  • Thank you for this detailed answer! There is one more question though: when I tried to add \DocumentMetadata{} inside a .cls file, I got an error saying that this command can only be used before \documentclass, does this mean that to make \hrefurl[urlencode] work the user has to enable \DocumentMetadata{} per document?
    – Jinwen
    Commented May 25, 2023 at 9:57
  • yes. The command is meant for metadata, and authors should set there for example the language and in future also title and similar. Commented May 25, 2023 at 10:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .