Append new page to pdf file

Hi there,

Am rewriting my codes in Modern Mode and I can’t find the equivalent tricks to some classic commands.
I like to compile some group of figures into a single pdf
Classic:

gmt […layer1…] -K > psfile # create psfile with layer 1
gmt […layer2…] -O >> psfile # add layer2 (close page 1)
gmt […layer3…] -K >> psfile # create a new page with layer3
gmt […layer4…] -O >> psfile # add layer 4 (close page2)

Is there a way to do that in Modern without having to create, merge and delete multiple pdfs? If there’s really no other solution… then how ?

Modern:

gmt begin
gmt figure Fig1
[…layer 1 and 2…]
gmt figure Fig2
[…layer 3 and 4…]
gmt end
~# a command to append Fig2 to Fig1 into Fig3 ?
merge Fig1 Fig2 > Fig3
rm -f Fig1 Fig2

Thanks,
Guillaume

1 Like

Here are two solutions I know:

  1. Generate many PS files in modern mode and use “gmt psconvert” to convert and merge them into a single PDF file.
gmt begin
gmt figure Fig1 ps
[…layer 1 and 2…]
gmt figure Fig2 ps
[…layer 3 and 4…]
gmt end
gmt psconvert *.ps -TF -Foutput
  1. Generated many PDF files and merge them using external tools. I usually use cpdf to merge PDF files.
gmt begin
gmt figure Fig1
[…layer 1 and 2…]
gmt figure Fig2
[…layer 3 and 4…]
gmt end
cpdf Fig1.pdf Fig2.pdf -o output.pdf
rm -f Fig1.pdf Fig2.pdf

There are several things one cannot esaily to in modern that gurus could do in classic. For instance, if you want to reuse a PostScript layer in many plots one could just make the layer separate:

gmt command-or-script … -O -K > common.ps

and then just use cat common.ps >> Fig1.ps, cat common.ps >> Fig2.ps, etc and do whatever. With you no longer having access to the PostScript low-level files you cannot do things that way anymore.
Likewise, GMT never really supported multi-page documents since plots are inherently a single page.

For an example of how you can do this in modern mode, see the last example on the gmt batch documentation where we create lots of PDF plots in parallel and then merge into a single PDF document.

The example you’re mentioning seem slightly more complicated than @seisman’s solution using psconvert.

I’m not there yet but I will try the batch module one day :slight_smile:

Sure, and perhaps I should update that example using @seisman’s cpdf program.

There’s nothing wrong with using psconvert to join pdfs together is there? I’ve been playing around with gmt batch and I just use this in my post.sh:

gmt psconvert ${BATCH_PREFIX}_*.pdf -TF -F${BATCH_PREFIX} -C-dAutoRotatePages=/None

(My -C flag there stops gs from rotating my (landscape-ish) pdfs, which it had been doing for some reason.)

Interesting, we certainly never read PDF files internally so I am surprised that works, but perhaps it does because with -TF we just pass the filenames to gs in this case.

I just followed the psconvert manual page, which said that “The -TF creates a multi-page PDF file from the list of input PS or PDF files.”

I should read the docs more often…

1 Like