10

I'm using Ghostscript to convert my PDF files to JPEGs with Ghostscript which works great.

For my output images I'm using %03d in the file name, so the file names come out 001, 002 ... and so on according to the page numbers.

But i want in some case the numbers to start from an higher number.

For example I process a file with two pages so the output images are page001.jpg, page002.jpg

Now I want to process another PDF and instead of replacing those files, I want to create page003.jpg, page004.jpg.

How can this be done?

This is my full command line I'm using now:

'C:\gs\gs9.14\bin \gswin64c -dNOPAUSE -sDEVICE=png16m \
  -sOutputFile=page-%03d.jpg -r100x100 -q' . $pdf_file. '-c quit'
Kurt Pfeifle
  • 86,724
  • 23
  • 248
  • 345
Daniel Katzan
  • 526
  • 1
  • 7
  • 29
  • Y not create a batch file to output files TmpPage-001.jpg TmpPage-002.jpg ... and then in the next line rename those files to the next filename required in the series ... or do i assume that you do not have the luxury to create temporary files and the output should be directly from ghostscript – protonx80 Oct 03 '21 at 15:12

2 Answers2

7

Here is a workaround trick, that you could use:

gswin64c.exe                  ^
   -sDEVICE=png16m            ^
   -sOutputFile=page-%03d.jpg ^
   -r100x100                  ^
   -c "showpage showpage"     ^
   -f filename.pdf

The -c "showpage showpage" inserts two empty pages into the output. The output files will be named

page-001.jpg + page-002.jpg + page-003.jpg + page-004.jpg

So the first two are white-only JPEGs and should be deleted afterwards.

You can extend this command with any number of empty pages you want.


Update

Of course, if you know in advance that you want to convert several different PDF files to images where you want the counting for a new PDF to continue exactly from where the last PDF ended, you could do this:

gswin64c.exe                  ^
   -sDEVICE=jpeg              ^
   -sOutputFile=page-%03d.jpg ^
   -r100x100                  ^
   -f file1.pdf               ^
   -f file2.pdf               ^
   -f file3.pdf               ^
   -f [...]

BTW, your original command requests .jpg file suffixes, while the Ghostscript device is png16m. This doesn't match. Initially I blindly copied your command, but now I've corrected it.

Kurt Pfeifle
  • 86,724
  • 23
  • 248
  • 345
  • it doesnt really serv my purpose, cause while processing the first file, i dont know if a second file to process will come, i get the second file later, and only if he has the same id - he needs to continiu the first one. what i did for now (which isnt very efficint) is when i get a second file, i combine it with the first one, and the process the whole pdf to get the images – Daniel Katzan Jan 05 '15 at 14:41
  • ok, so maybe i can use your suggestion to convert the second pdf, starting with a few blank pages, so i need do it in a different folder, and then only move the wanted pages, maybe there is a paramter i can use to create the images in the same folder withour running over any already existing files with that name? that way i could do it in the same folder and the blank pages wont actually be created? btw can u point me to a documantion of all the poosible paramters? i find it a bit hard to find info abput the command, thx – Daniel Katzan Jan 05 '15 at 19:44
  • @DanielKatzan: Current 'head'/development-version docs: [General usage](http://git.ghostscript.com/?p=ghostpdl.git;a=blob_plain;f=gs/doc/Use.htm;hb=HEAD), [PDF output](http://git.ghostscript.com/?p=ghostpdl.git;a=blob_plain;f=gs/doc/Ps2pdf.htm;hb=HEAD), [PostScript output](http://git.ghostscript.com/?p=ghostpdl.git;a=blob_plain;f=gs/doc/Ps2ps2.htm;hb=HEAD) and [Image output](http://git.ghostscript.com/?p=ghostpdl.git;a=blob_plain;f=gs/doc/Devices.htm;hb=HEAD). – Kurt Pfeifle Jan 05 '15 at 20:14
  • @DanielKatzan: Released version 9.15: [General usage](http://git.ghostscript.com/?p=ghostpdl.git;a=blob_plain;f=gs/doc/Use.htm;hb=refs/heads/gs915), [PDF output](http://git.ghostscript.com/?p=ghostpdl.git;a=blob_plain;f=gs/doc/Ps2pdf.htm;hb=refs/heads/gs915), [PostScript output](http://git.ghostscript.com/?p=ghostpdl.git;a=blob_plain;f=gs/doc/Ps2ps2.htm;hb=refs/heads/gs915) and [Image output](http://git.ghostscript.com/?p=ghostpdl.git;a=blob_plain;f=gs/doc/Devices.htm;hb=refs/heads/gs915). – Kurt Pfeifle Jan 05 '15 at 20:15
  • -dNOPAUSE parameter could be added to disable interactive prompt if the page in PDF file is > 1. – Nanang F. Rozi May 06 '20 at 04:21
0

You cannot do that with the standard version of Ghostscript, the output file numbers are given as the emitted page number (so if you had a 10 page file, with /NumCOpies 2, you would get files numbered 0 to 19).

Of course, you can process the two files on the same command line, I think that will give you the second file with page numbers beginning where the first set left off.

Otherwise you will have to modify the source code of the Ghostscript device.

KenS
  • 30,202
  • 3
  • 34
  • 51