0

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

Martin Schröder
  • 3,086
  • 3
  • 38
  • 69
Tom de Geus
  • 4,312
  • 2
  • 22
  • 51
  • 1
    I'm sorry but I don't really understand what you are trying to achieve exactly. It doesn't help that you refer to PDF **images** in your question. I don't think you mean images at all, it sounds more like you are talking about PDF page contents. It sounds like you want some kind of imposition, but you don't say what you want as the output. Eg if I take pages 1 and 2, both of which are 612x792 points, do you want 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 ? What size should this page be ? 612x1584 ? – KenS Jun 28 '18 at 18:27
  • There's a basic imposition program in my answer to this question https://stackoverflow.com/questions/30545382/ghostscript-for-pdf-how-to-fit-4-pages-into-1/30546987#30546987 – KenS Jun 28 '18 at 18:29
  • @KenS Thanks for you comment. What you describe is exactly what I want. I have updated the question to reflect this. With respect to you script: sorry I feel like a new-bee, but how should I use it? – Tom de Geus Jun 29 '18 at 07:28
  • Ah right yes, sorry. That's a PostScript program. Let me write this up as an answer, there isn't room in the comments – KenS Jun 29 '18 at 09:24

1 Answers1

2

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.

KenS
  • 27,930
  • 2
  • 31
  • 42
  • Thanks a lot! I do get an error though `Error: /undefined in OriginYTx`. – Tom de Geus Jun 29 '18 at 11:03
  • Hmm well that sounds like a bug in the PostScript program, which is kind of surprising, perhaps your example takes a code path that hasn't been exercised in there before. I'm afraid I'd need to see an example before I could fix it. – KenS Jun 29 '18 at 11:56
  • I have shared the example in this [temporary repository](https://github.com/tdegeus/stackoverflow). It includes the command I executed in the `readme.md` – Tom de Geus Jun 29 '18 at 13:07
  • OK so there's a typo in the program, line 244 reads /OriginYTy PDFHeight def and that should be /OriginYTx, not /OrigtinYTy. However, that's not going to be enough to make this work for you. The program expects all the pages to be in one file, you can't set -sFile= twice, all that does is overwrite the first one with the second one. You'll have to get all the input into a single file before this will be usable for you. – KenS Jun 29 '18 at 13:26
  • I did gs -sDEVICE=pdfwrite -sOutputFile=new.pdf 1.pdf 2.pdf and then used the newly created new.pdf as the input file in your command line, which worked as expected, after the typo was fixed. – KenS Jun 29 '18 at 13:31
  • Thanks, fixing the type-o removes the issue. On the final command `gs -dNODISPLAY -dDEVICEWIDTHPOINTS=460 -dDEVICEHEIGHTPOINTS=690 -dFIXEDMEDIA -sDEVICE=pdfwrite -sOutputFile=out.pdf -sFile=new.pdf 2-up.ps` what happens is that just a `gs` session is started, but not file is written. (Nothing happens, the shell just stays on `GS>`). – Tom de Geus Jun 29 '18 at 14:17
  • Add -dBATCH or just type quit. Oh you might want to add -dNOPAUSE too if you run files with lots of pages, otherwise it'll expect you to press return after every output page. The pdfwrite device doesn't write its output until the device is closed (which normally only happens when you exit the program). That's why you can send multiple input files and get one output file in normal operation. – KenS Jun 29 '18 at 14:38