Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I want to embed a screenshot of a web page into a LaTeX document as a vector graphic with clickable links.

I'm using wkhtmltopdf to render the web page as a letter-sized pdf file with working links. Then, I'm using this command to crop the pdf file while retaining the links:

gs -sDEVICE=pdfwrite -o "screenshot_cropped.pdf" -c "[/CropBox [`gs -sDEVICE=bbox -dBATCH -dNOPAUSE "screenshot.pdf" 2>&1 | awk '/HiRes/ { print $2,$3,$4,$5 }'`] /PAGES pdfmark" -f "screenshot.pdf"

(Commands taken from this thread on comp.text.tex; I can't use pdfcrop, since it apparently removes the links)

Finally, I'm embedding the file into my LaTeX document with the following:

\includegraphics[width=6 in]{screenshot_cropped.pdf}

Unfortunately, the links no longer work in the resulting pdf file. How can I fix this? I'm using XeLaTeX if it matters.

share|improve this question
    
Possible duplicate: How to preserve hyperlinks in included pdf? –  diabonas Feb 10 '12 at 15:01

2 Answers 2

up vote 10 down vote accepted

Quoting the pdfpages manual (page 2):

[...] all kinds of links1 will get lost during inclusion. (Using \includepdf, \includegraphics, or other low-level commands.)

However, there's a gleam of hope. Some links may be extracted and later reinserted by a package called pax which can be downloaded from CTAN [3]. Have a look at it!

The pax package mentioned is actually a Java program which reads the PDF file, extracts the links and writes their positions to a .pax file. This information can be processed by an extended version of \includegraphics which reinserts the links at the correct position. However, the package is considered experimental and only works with pdfLaTeX.

This is how to use pax:

  • Download and install the package from CTAN.
  • Run pdfannotextractor.pl --install. This downloads and installs PDFBox, a Java library necessary for using pax.
  • Now you can run pdfannotextractor.pl <filename.pdf> in order to read the links from any given PDF file and write them to filename.pax.
  • In your LaTeX document, invoke \usepackage{pax}. This extends \includegraphics in order to read and process the auxiliary .pax file.

Regarding the cropping of the PDF file with Ghostscript: In my experiments, this seemed to confuse pax, resulting in wrong positions of the hyperlinks. So it's probably best to call wkhtmltopdf with

wkhtmltopdf -B 0mm -L 0mm -R 0mm -T 0mm  

in order to not create any margin from the start.


This is my working test case (using the site http://tex.stackexchange.com/about):

Creation of the PDF and the annotation file:

wkhtmltopdf -B 0mm -L 0mm -R 0mm -T 0mm http://tex.stackexchange.com/about screenshot.pdf
pdfannotextractor.pl screenshot.pdf

LaTeX source code

\documentclass{article}
\usepackage[margin=0mm]{geometry}
\usepackage{pax}

% Visible squares for demonstration purposes, can be removed without harm
% (change \iftrue to \iffalse or remove the following lines altogether)
\iftrue
  \usepackage{hyperref}
  \hypersetup{
    filebordercolor={1 1 0},
  }
\fi

\begin{document}
    \includegraphics[scale=0.9]{screenshot}
\end{document}

Result:

output result, with visible squares denoting hyperlinks

(The cyan boxes are there to prove that hyperlinks work, and can be removed without consequences.)

share|improve this answer
    
So beautiful, thanks! –  Bkkbrad Feb 12 '12 at 15:06
1  
I am using the full TeXLive installation in Fedora 19, when I try to run /usr/share/texlive/texmf-dist/scripts/pax/pdfannotextractor.pl [--install|file.pdf] I get: Exception in thread "main" java.lang.NoClassDefFoundError: org/pdfbox/cos/ICOSVisitor. (using perl v5.16.3). I tried this: tex.stackexchange.com/a/79006/1871 but I still get an error (see in the comment to Heiko's answer there). –  alfC Jul 21 '13 at 9:14

I'm worried this is not going to be possible. It reminds me of a remark from the PGF manual regarding the pgfpages package.

A word of warning: using pgfpages will destroy hyperlinks. Actually, the hyperlinks are not destroyed, only they will appear at totally wrong positions on the final output. This is due to a fundamental flaw in the pdf specification: In pdf the bounding rectangle of a hyperlink is given in “absolute page coordinates” and translations or rotations do not affect them. Thus, the transformations applied by pgfpages to put the pages where you want them are (cannot, even) be applied to the coordinates of hyperlinks. It is unlikely that this will change in the foreseeable future.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.