2

I generate a PDF from a set of PNG files using this command:

convert --  $(ls -v -- src/*.png) out/book.pdf

where there're some files with names like -03.png, which I need to have smaller page numbers than others. But I get a PDF which has -01 having page number 1, -02 number 2, etc., and 01 starts from page number 6.

The PDF is a scanned book, which has some elements like table of contents etc. which aren't included in page numbering. I remember to have seen some PDFs which have special page numbers like vii before normal Arabic numbers start.

I've tried using -scene -5 to add an offset to page numbers, but this didn't change the result.

So what should I instead do to make page "01.png" have page number 1, etc., and previous ones have some other numbers (negative or Latin, anything) and appear at the beginning of the document?

Ruslan
  • 15,183
  • 5
  • 55
  • 110

1 Answers1

2

First, you want to sort files numerically counting optional minus sign, which you won't do with command you show.

Second, you talk about PageLabels for PDF pages, which you can add using Ghostscript and pdfmark operator.

Try this command:

ls src/*.png | \
sort -n | \
convert @- pdf:- | \
gs \
  -sDEVICE=pdfwrite \
  -o out/book.pdf \
  -c '[{Catalog}<</PageLabels<</Nums[0<</P(-3)>>1<</P(-2)>>2<</P(-1)>>3<</S/D>>]>>>>/PUT pdfmark' \
  -f -

It's for 3 pages -3, -2 and -1, followed by any number of pages labelled 1, 2, 3 etc. Modify according to your needs.

user2846289
  • 1,935
  • 11
  • 15
  • But `sort -t"-" -k2r` does. – Ruslan Nov 10 '13 at 07:54
  • @Ruslan, well I checked before posting, it works for me. [Here](http://stackoverflow.com/questions/4856030/linux-command-sort-according-to-fields-numerical-value) they suggest using `LC_ALL=C sort -n` – user2846289 Nov 10 '13 at 08:00
  • Can't understand where `-c` gs option is documented. What did you use to define your command? I'm trying to customize it to continue paging from label 3 instead of 1 after custom labels. – Ruslan Nov 10 '13 at 10:21
  • Why, [`-c`](http://ghostscript.com/doc/current/Use.htm) is Postscript code to be interpreted. [Syntax for PageLabels](http://www.adobe.com/devnet/pdf/pdf_reference.html) is, to put it simple, index followed by dictionary, `/P` is string prefix, `/S` is numbering style (`/D` being decimal, which auto-increments for following pages), all keys optional. I don't quite understand last comment, do you need, say, -3,-2,-1,3,4, etc.? – user2846289 Nov 10 '13 at 10:52
  • Yes, I need "..., vii, viii, 3, 4, ...". That's how pages are numbered in the book. For now I implemented it just making an explicit /P for all remaining pages with $(for ... ) loop, but would be good to know a nicer way to do this. – Ruslan Nov 10 '13 at 11:10
  • I see. Try `-c '[{Catalog}<>8<>]>>>>/PUT pdfmark'`, `/r` is lower-case roman for 0..7 pages, then decimal for 8..etc. starting from 3. – user2846289 Nov 10 '13 at 11:18