I use gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -dFirstPage=$2 -dLastPage=$3 -sOUTPUTFILE=$4 $1 in a script to extract pages from a pdf file with Ghostscript 9.07. I obtain a lot of warnings (as in the first row in the following output) and one error:

GPL Ghostscript 9.07:    **** Warning: Outline has invalid link that was discarded.    
GPL Ghostscript 9.07: ERROR: A pdfmark destination page 4 points beyond the last page 3.

The resulting PDF (consisting of the extracted pages) is fine, however. I am wondering why I obtain this error and the warnings although I chose the option -q for quiet. I did some search on that and found, for example, this, but the PDF file is already generated with hypertexnames=false and so the suggested solution there does not work in my case (Ubuntu 13.04).

Just to add: The .pdf file is generated via pdflatex from



\tikz[remember picture, overlay]\node at (current page.south)[rectangle, fill, color=gray]{};
foo bar 

If you comment out parts after \begin{document}, gs suddenly has neither warnings nor errors, so it has to do with the way the PDF file is generated.

  • Please use qpdf or pdftk or pdfjam for page extraction and not GhostScript - gs is designed for other things. May 2 '13 at 8:45
  • I tried (extensive tests) with pdftk and pdfjam. Neither can preserve links correctly and both returned significantly larger file sizes than gs... May 2 '13 at 15:48

Package hyperref adds bookmarks (outlines). If only some pages are extracted, then there might be pages that have bookmark entries (e.g. by \section{bar}). However ghostscript does not reorganize the bookmark tree (non-trivial in general). It only makes a poor job of copying the tree, removing some invalid entries. Such page subset documents are not a problem for printing purposes. If you want need the interactive features (bookmarks, links), then it is probably better to generate it directly via TeX.

Update for the error message.

Example test file:




Reference to first section~\ref{sec:first} and second

\section{First section}

\section{Second section}


The document contains four pages and three destinations:

  • Doc-Start is set by hyperref at the start of the document,
  • section.1 on page 3 by \section{First section} with label sec:first,
  • section.2 on page 4 by \section{Second section} with label sec:second.

The links \ref{sec:first} and \ref{second} use the latter two destinations as link target.

Now we remove the first page:

$ ps2pdf -dFirstPage=2 test.pdf test-new.pdf

Now the link \ref{sec:first} still points to page 3, but this is page 4 of the orignal file test.pdf. It should now link to the section on page 2.

Also the destination for \ref{sec:second} uses the wrong page number. Ghostscript forgets that the first page is removed and wants to link to the fourth page, but the new document has three pages only. Thus Ghostscript complains:

GPL Ghostscript 9.05: ERROR: A pdfmark destination page 4 points beyond the last page 3.

Therefore I consider this as bug in Ghostscript.

A better job does pdftk:

$ pdftk test.pdf cat 2-4 output test-new.pdf

Here the links are correct. However the bookmarks/outlines are removed. (But the bookmarks of Ghostscript are not much usable, because there destinations uses the wrong pages as shown above.) I consider it as bug in ghostscript. that it gets confused, if the first page (-dFirstPage) is larger than 1. It seems to take the page number of the original document for a destination (link target for \ref)

  • Thanks, Heiko. I still don't understand why a) the error is an error and not a warning and b) why it is not suppressed by -q, but your post has helped me in putting it in the correct context. Apr 27 '13 at 20:56
  • @MariusHofert Answer updated. Apr 27 '13 at 21:27
  • Dear Heiko. Thanks for the update. I filed a bug report for Ghostscript. Apr 27 '13 at 22:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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