Can I use GMT tools to embed hyperlinks in a PDF?


I am wondering if there’s a way to embed hyperlinks, e.g. HTTP or FTP, within a PDF via GMT? In particular, I am hoping to have psxy generated symbols associated with links that open within the PDF upon click.

Thanks much in advance!



1 Like

It is a nice idea, and I am sure PDF can allow this. However, someone would have to research how that can be embedded via PostScript (which is what we write) and not get stripped off by gs. If there are PS extensions for this supported by ghostscript then it might be possible. On the GMT end I could easily imagine that input like

lon lat [depth] [size] URLstring

could be checked and if the trailing text is an URL we add that link if there was a way to preserve it through the conversion.

Using the @[ @[ balise to take advantage of Latex ?

I’d think it should be possible in principle, since the latex hypertex package, e.g., seems to be able to preserve links from .dvi to .ps to .pdf?

Maybe it’s just a matter of adding the relevant PDF instructions and gs won’t touch them? Similar to what’s happening with transparency?

Yes maybe. Would you be able to try the Latex hypertex on a tiny tex file with a link and send me the PS file so I can examine what it looks like?

Invisible but clickable and workslixo.pdf (272.9 KB)

Page 21

I guess Joaquim already figured this out (not sure what he did), but in case:

here’s a hyperref (different package, the other seems obsolute) example of a .tex, .ps, and .pdf file that does have a simple link. Those were made with latex, dvi2ps, and ps2pdf, respectively.

Seems like the main PS stuff is around

SDict begin [/H /I/Border [0 0 1]BorderArrayPatch/Color [0 1 1]/Action
<</Subtype/URI/URI(>>/Subtype /Link H.B /ANN pdfmark

\end{document} (37.8 KB)

test_link.pdf (4.8 KB)

FWIW - by accident I discovered a while back that if you use URLs (or emails) for legends or the -U strings, these turn out to be clickable hyperlinks in the generated PDF (SumatraPDF and Acrobat Reader). Now, I don’t know whether this is because of the PDF readers automagically converting the strings to clickable URLs or whether this done in the ghostscript PS to PDF conversion. Here’s a minimal working example:

#!/usr/bin/env bash
gmt begin Hyperlinks pdf A+m0.5
gmt basemap -R0/10/0/10 -JX10 -B+t"Title" -Ba5f1 -U"" -V 
echo 5 5 | gmt plot -Sc0.4 -Gred -W0.5p,black
echo 5 5 "Click me:" | gmt text -B -D2+v
gmt legend -DJBC+o0/-1+w7.5 -V -F+c0.2+i+s+g100 <<EOF
H - Test
G 1p
L 10p,Arial C URL:
N 2
L 10p,Arial C Contact: 
gmt end

Seems to work even with gmt text (this is using SumatraPDF as reader) – the generated PDF here: Hyperlinks.pdf (10.4 KB)

Generated on WSL Debian/Windoze with gmt 6.2.0_053bf4e_2021.05.10

/edit - refined example

What I did was to follow the example in page 21 of the above linked pdf and add this to psconverter

				fprintf (fpo, "\n%% embed URL info\n");
				fprintf (fpo, "[ /Rect [50 225 295 245]\n");
				fprintf (fpo, "/Action << /Subtype /URI /URI ( >>\n");
				fprintf (fpo, "/Border [0 0 2]\n");
				fprintf (fpo, "/Color [0.7 0 1]\n");
				fprintf (fpo, "/Subtype /Link\n");
				fprintf (fpo, "/ANN pdfmark\n");

But oddly, the URL text is not shown.

Christian’s example shows the hyperlinks when open in Acrobat but not in Firefox.

So would Joaquim’s method work to do what Paul suggested, i.e. have symbols be associated with URLS?