LaTeX dvipdf tip: -dFirstPage/-dLastPage doesn't work

Spoiler alert: it works with Ghostscript 9.09 but not Ghostscript 9.06. I'm writing this mostly to vent.

Here's the story.

Most of my books are written using the LaTeX formatting system. The latex command outputs a .dvi file, and there are programs that translate dvi to PostScript, pdf, HP LaserJet language, and so on.

I'm writing a script that generates one PDF file for each chapter. You'd think this would be easy, and it mostly is. There's some great advice here and here. (Both links are to Stack Exchange sites, btw)

My original plan was to call dvipdf then re-process the PDF using pdftk (The PDF ToolKit) or Ghostscript. Since Ghostscript was installed on my Mac, but pdftk wasn't, I started looking into using Ghostscript.

It turns out that dvipdf (the PDF generator) is a bash script that calls dvips (the PS generator) and pipes it to Ghostscript which converts it to a PDF. Rather than generating the PDF then re-processing it with Ghostscript, I could simply give Ghostscript the right options and it would skip the pages I didn't want in the final output. I would have to trick dvipdf into passing those options.

The options would look something like this:

gs -dFirstPage=10 -dLastPage=20

Good news! dvipdf is already set up to handle this situation. You can give it any Ghostscript options and they will be silently passed to Ghostscript. The code is quite elegant. Take a look.

However it didn't work as I had hoped. In fact, it didn't work at all. I just got the full PDF file.

I made a copy of dvipdf and put echo statements all over the place to see what was going on. The right options were being sent to Ghostscript.

To make a long story short, the "pdfwriter" driver in Ghostscript doesn't support page selection. It silently ignores -dFirstPage and -dLastPage. So I was on the right track but on the wrong field. Oh yeah, and I wasted 2 hours trying to figure this out.

I went to file a bug report/feature request with Ghostscript and, Good News! The latest release of Ghostscript has already included this feature. The bad news is this release is so new that it hasn't made its way to MacPorts. I'm confident that it will soon (I've notified the port maintainer).

So, this blog post is only useful if you have the same problem in the next few days. In a week or two this problem will disappear for anyone that is upgraded.

If this was urgent i would install pdftk which not only can accept first/last page ranges, but the "last" range can be specified as "pages from the end", which would be very useful to me. Ghostscript might have that feature to, but I won't know until I upgrade to 9.09.

I could experiment more, installed 9.09 from source and so on, but I should really get back to writing text, not code.

Posted by Tom Limoncelli

No TrackBacks

TrackBack URL: http://everythingsysadmin.com/cgi-bin/mt-tb.cgi/1632

3 Comments | Leave a comment

Have you tried the MacTex route?
http://bit.ly/162Fcf0
and Brew is another MacPorts type of system.
http://brew.sh

Looks like this issue is present in the windows version 9.15, converting postscript to pdf, be warned.

-dFirstPage/-dLastPage has *never* worked with Postscript intput, it has only *ever* worked with PDF input - the parameters were specifically handled by the Ghostscript PDF interpreter, and ignored by the Postscript interpreter.

The reason for this is because, whilst PDF is a random access file format, Postscript is a stream - thus, whilst in PDF you can search to page X in the file, and interpret just that, in Postscript you must interpret the entire stream start to finish.

The same functionality can be implemented using some Postscript programming (a search on StackOverflow should turn up what's required, as the question has been asked and answered several times).

Additionally, there is a project underway to handle the FirstPage/Lastpage functionality in the graphics library component of Ghostscript, thus making it language indepdendant - however, due to various on-going issues, it's too early to say for sure whether that will bear fruit.

Leave a comment

 
  • LISA16