[Tex/LaTex] Set the print flag on links with hyperref to preserve them with Ghostscript >= 9.25

ghostscripthyperlinkhyperrefpdftex

To reduce the size of a PDF produced with pdflatex I usually run this command:

$ gs -sDEVICE=pdfwrite        \
     -dCompatibilityLevel=1.4 \
     -dPDFSETTINGS=/printer   \
     -dEmbedAllFonts=true     \
     -dSubsetFonts=true       \
     -dFastWebView=true       \
     -dNOPAUSE                \
     -dQUIET -dBATCH          \
     -sOutputFile=out.pdf     \
      in.pdf

When I updated Ghostscript to version 25 (9.25), the hyperlinks automatically made by hyperref stopped working: they were not any more clickable.

I tried filing a bug on the Ghostscript bugzilla, where they replied that it is the intended behaviour. See this bug report.

Basically, quoting their answer:

If a /Link Annotation has the 'Print' bit of the annotations /Flags
value not set, then the PDF interpreter will (by default) not process the
annotation. If the PDF interpreter skips the annotation then the
pdfwrite device doesn't ever see it.

If the Annotation (of whatever kind) does set the Print bit, then
(again in default setup) the PDF interpreter will process the
annotation and pass it to the pdfwrite device.

You can change the behaviour of the interpreter. If you set
-dPrinted=false, then the interpreter no longer cares about the Print bit of the annotation flag. In this mode it instead checks the NoView
bit, if that isn't set, thenit processes the annotation.

In this case, if the NoView bit was set, then the annotation would
be skipped.

So you need to know something about the way the annotation has been
created, currently.

The reason this is important is that the control (-dPrinted) which was
supposed to control whether or not the annotation is processed was
being ignored. Obviously that's not the way it was supposed to work,
and has been fixed. Its unfortunate that this causes you a problem,
and I may well extend the operation of this control in future, but as
of now this is behaving as intended.

With the option -dPrinted=false the links are working again (from Ghostscript's manual):

-dPrinted

-dPrinted=false
Determines whether the file should be displayed or printed using the "screen" or "printer" options for annotations and images.

With -dPrinted, the output will use the file's "print" options;

with -dPrinted=false, the output will use the file's "screen" options.

If neither of these is specified, the output will use the screen options
for any output device that doesn't have an OutputFile parameter, and
the printer options for devices that do have this parameter.

$ gs -sDEVICE=pdfwrite         \
     -dCompatibilityLevel=1.4  \
     -dPrinted=false           \
     -dPDFSETTINGS=/printer    \
     -dEmbedAllFonts=true      \
     -dSubsetFonts=true        \
     -dFastWebView=true        \
     -dNOPAUSE -dQUIET -dBATCH \
     -sOutputFile=out.pdf      \
      in.pdf

It makes sense to use this switch, since the hyperlinks are not intended to be printed, of course.

But in case I want to set them, how to set the print flag by default on the hyperlinks created by hyperref?

I tried checking the hyperref manual:

PDF form field macros (\TextField, \CheckBox, …) support boolean
flag options. The option name is the lowercase version of the names in
the PDF specification (1.7):

http://www.adobe.com/devnet/pdf/pdf_reference.html

http://www.adobe.com/devnet/acrobat/pdfs/pdf_reference.pdf

Options (convert to lowercase) except flags in square brackets:

  • Table 8.16 Annotation flags (page 608):
    1. Invisible
    2. Hidden (PDF 1.2)
    3. Print (PDF 1.2)
    4. NoZoom (PDF 1.3)
    5. NoRotate (PDF 1.3)
    6. NoView (PDF 1.3)
    7. [ReadOnly (PDF 1.3)] ignored for widget annotations, see table 8.70
    8. Locked (PDF 1.4)
    9. ToggleNoView (PDF 1.5)
    10. LockedContents (PDF 1.7)

but I don't really get how to set them explicitly.

Best Answer

I think you can get the Print flag set if you (ab)use the pdfa option.

\usepackage[pdfa]{hyperref}

(see section "5.12 Option pdfa" of the hyperref manual).

This at least seems to work on my local installation (gs 9.25 as required. Without the pdfa option and running your gs command with -dPrinted the hyperlinks are dropped; with the pdfa option the hyperlinks are preserved).

Related Question