coherentgraphics / cpdf-binaries

PDF Command Line Tools binaries for Linux, Mac, Windows
GNU Affero General Public License v3.0
593 stars 42 forks source link

How to perform a Cut and Stack N-Up? #68

Closed maxiride closed 1 year ago

maxiride commented 1 year ago

The Cut and Stack N-Up is the process of imposing an input pdf file so that the resulting PDF is an N-Up of the input but the pages are arranged in such a manner that they can be cut and stacked to obtain the original order.

An image is worth a thousand words in this case.

AH2XIBqP+G0eAAAAAElFTkSuQmCC

johnwhitington commented 1 year ago

Get the number of pages:

cpdf -pages cpdfmanual.pdf
96

Divide by four, and split:

cpdf -split cpdfmanual.pdf -chunk 24 -o chunk%%%.pdf

Now merge in collation mode:

cpdf -merge chunk*.pdf -collate -o collated.pdf

Now impose 2x2:

cpdf -impose-xy "2 2" collated.pdf -o out.pdf

I have only briefly tested this - but if it's not the right answer, it's close to it.

maxiride commented 1 year ago

Thanks for the quick response!

The steps seem to exactly obtain the desired result, now that I understand how to do it with cpdf I think the steps can be reproduced for any given rows and columns too.

I'll tinker a bit for the output paper size and margins between the "logical" sheets.

However, if I were to print in duplex the even numbered sheets (which would be the backside) will need to have the sequence mirrored in order for the correct back to be printed on the rear of it's front. I guess I can get how to rearrange the pages for this result but I gladly accept suggestions.

Even in this case a picture is worth more :)

IMG_20221016_204316

Edit - playing with the -impose-rtl somewhat achieved the desired result, however I'm not figuring out how to impose right to left only even pages.

johnwhitington commented 1 year ago

-rtl is the answer, but you won't be able to do it in one impose operation. You'll have to split the file back up into chunks before imposing half of them with -rtl and half of them without, and then merge the results. By careful naming of the chunks, you might be able to get it to merge back together with just *.pdf on the command line...

maxiride commented 1 year ago

For anyone stumbling upon this scenario in the feature, I eventually adopted the following strategy, which is a mix of the input provided by @johnwhitington that pointed me in the right direction and some Go code that can be easily reproduced in any language.

With a few lines of code, I compute the page sequence for the final cut and stack.

func stackNCutPageOrder(righe, colonne, pezzi int) []int {
    fogli := pezzi / (righe * colonne)
    celleFacciata := righe * colonne

    posizioneFoglio := 0
    foglioCorrente := 1
    var ordine []int
    for i := 0; i < pezzi; i++ {
        indice := posizioneFoglio*fogli + foglioCorrente
        ordine = append(ordine, indice)

        posizioneFoglio++
        if posizioneFoglio == celleFacciata {
            posizioneFoglio = 0
            foglioCorrente++
        }
    }

    return ordine
}

...and then call

cpdf.exe input.pdf <calcuated sequence>  -o out_ordered.pdf

cpdf.exe -impose-xy "2 2" out_ordered.pdf -o out_imposed.pdf    

In the scenario of a duplex printing, it's enough to first split the input file in its odd and even pages, repeat the process and merge collate. Please note the -impose-rtl used on the backside pages of the document.

cpdf.exe input.pdf odd  -o front.pdf

cpdf.exe input.pdf even -o back.pdf

cpdf.exe front(back).pdf <calcuated sequence>  -o front(back)_ordered.pdf

cpdf.exe -impose-xy "2 2" front_ordered.pdf -o front_imposed.pdf 

cpdf.exe -impose-xy "2 2" -impose-rtl front(back)_ordered.pdf -o back_imposed.pdf 

cpdf.exe -merge front_imposed.pdf back_imposed.pdf -collate -o merged_imposed.pdf