pdfghostscriptpdf-manipulation

Stack PDF images: single page output


How can I stack PDF images (vertically) into a single page output PDF? I.e.:

|-----|
|  1  |
|  2  |
| ... |
|-----|

(See example below.) What I am looking for is a PDF equivalent of this tool that stacks SVG graphics.


Note that this is distinctly different from a multi-page combination

|-----|
|  1  |
|-----|
|  2  |
|-----|
| ... |
|-----|

which one would obtain using

gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=merged.pdf 1.pdf 2.pdf ...

(See this answer.)


Example

My goal is perfectly described by @KenS in the comments: I take pages 1 and 2, both of which are 612x792 points, which should become a PDF with a single page containing the marking content from page 1 at the top and the marking content from page 2 at the bottom. The size if this page should this page be 612x1584.

More visually:

enter image description here


Solution

  • OK so the PostScript program from this answer will, I think, do the job. The way this works is that you set up the Ghostscript media size to be what you want the final output to look like, then you simply run the program through Ghostscript passing GS the name of the PDF file.

    The program gets the current media size, and then attempts to fit the pages from the PDF onto that media. Obviously I don't have your test file but I believe if you set up GS to have media 612x1584 and then run it, then GS will decide that the pages fit best unscaled and unrotated. If that's not the case I'd need to see an example to figure out why.

    Assuming you copy the program from the answer, and save it with the name 2-up.ps, the usage is in the comments at the start of the program:

    % usage: gs -dNODISPLAY -sFile=____.pdf [-dVerbose] 2-up.ps
    

    So you would need something like:

    gs -dNODISPLAY -dDEVICEWIDTHPOINTS=612 -dDEVICEHEIGHTPOINTS=1584 -dFIXEDMEDIA -sDEVICE=pdfwrite -sOutputFile=out.pdf -sFile=<insert your full path and filename here> 2-up.ps
    

    That will take the original PDF file (defined by -sFile), and try to create a 2-up representation of it, writing the output to a new PDF file.

    Note the comments; this doesn't attempt to preserve metadata like hyperlinks, because these are page-based and will be wrong when the pages are renumbered) and will only work with the current PDF interpreter in Ghostscript. It won't work with any other PostScript interpreter because the program uses internals of the Ghostscript PDF interpreter that it isn't really supposed to meddle with.

    Oh, and the program assumes that all the pages in the PDF file are the same size, the size of the first page.

    We're supposed to be adding more (better) support for imposition in Ghostscript in a future release.