I am trying to use ghostscript for its intended purpose, to interpret postscript files and output pngs. I would like a command that will convert a single specific page from a multi-page ps file to a single png. I have accomplished this in the case where the infile is a pdf file, but not when the infile is a ps.
This line will convert a ps into a file witch is comprised of the concatenated png files.
gs -dSAFER -dBATCH -sDEVICE=png256 -r96x96 -sOutputFile=out.png in.ps
This will take a single specific page from a pdf and convert it to a png.
gs -q -dSAFER -dBATCH -dFirstPage=2 -dLastPage=2 -sDEVICE=png256 -SOut=fileout.png in.pdf
Simply putting the dFirstPage=
flags in the post script input case does nothing. The output is the same as if the flags are not there.
You can do this with PostScript, regardless of whether the file is DSC compliant, provided you know which page you want.
A custom EndPage procedure will do the job. EndPage is called for every page in the job, and the procedure must return a boolean value, if true then the page is transmitted to the device for printing, if false then the page is consumed and discarded.
<<
/EndPage {
0 eq { %% reason code is 0 for showpage
5 eq { %% 5 is just an example, you would put your page# minus 1 here
true
}{
false
}ifelse
} {
pop %% remove the count of pages from the stack
false
}ifelse
}
>> setpagedevice
This tests the reason code to make sure it's 0 (showpage) then the number of executions of showpage so far to see if it's our target, if it is we put true on the stack and return. For any other condition we put false and return.
The count of pages is only reset when you exit the interpreter, so if you run two files back to back without restarting the interpreter then this will only work for the first one.
Unlike with PDF's, not all postscript files can have their pages accessed in a random order. Probably it is the case with the file yo are trying to process. (Otherwise the flags should be marked as "does not work for postscript" in the documentation).
I think the simplest way to go there is to convert your PS file to a PDF first, ad then extract the pages from the resulting PDF.
Since you are on Linux, you can use the helper script "ps2pdf" which uses ghostscript underneath (but in most distributions is part of a "ps-tools" or similarly named package) - or just figure out the gs
command line to make the conversion.
Another way would be to generate all pages up to the last one you want, and delete the first, unwanted ones.
This happens due to nature of postscript itself: a postscritp file is simply a computer program, which sets upa lot of internal states, renders a page and call the showpage
function - the next page then starts to be rendered - so, depending on the file, there is no way for an external tool to know which statements are part of the state for all the pages, and which are just used to render a page. Under newer specifciations, there are some optional data structures, inherited from the PDF spec, that can work for calling the correct code for independent pages, but few files will carry that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With