1

i'm hoping to use qpdf for this.

I'm printing lots of small files and need to print them double sided, so I merge, say, 20 documents and wind up with a single 200 page pdf. I then can let the printer print, even pages reversed, then flip the stack over and put it back into the printer and print the odd ones, so we're using both sides of the paper.

my question is how i can detect and add a single blank page to the end of any document that has an odd number of pages; that way, when i do double sided printing, each document is completely separate from the others, rather than just printing on the back of a finished document.

iateadonut
  • 1,951
  • 21
  • 32

3 Answers3

1

If you have an odd number of pages, just call

cpdf -pad-multiple

with the even number one larger than the odd number. For example, for 19 pages, run

cpdf -pad-multiple 20 in.pdf -o out.pdf

You can get the number of pages with cpdf -pages.

johnwhitington
  • 2,308
  • 1
  • 16
  • 18
1

Currently using FOSS qpdf 10.6.3 this is possible in windows by something like (note %% is for use in a batch.cmd)

for /f %%N in ('qpdf --show-npages in.pdf') do set VAR=%%N& set /a num=2*(%%N/2)+1& if .%num%.==.%var%. qpdf in.pdf --pages . blankA4P.pdf -- out.pdf

Note the integer maths is rounding odds down so 31 = 30+1 is a match but 32 = 32+1 will not match.

Without checking the dimensions of in.pdf is not easy to know if blankA4P.pdf is required so best to either get last page dimensions for a matching prepared page or batch apply each shape in groups.

using cpdf (as mentioned in previous answer) we could build a blank on demand along the lines of cpdf -create-pdf -create-pdf-pages 1 -o blank.pdf and use a page size, However cpdf has the even better option for OP case so simplest is cpdf -pad-multiple 2 in.pdf -o out.pdf as 1st hinted by johnwhittington

So now we have a perfect short one line solution. however cpdf is not FOSS

I have queried with qpdf if there may be a simpler way with just qpdf so watch this space https://github.com/qpdf/qpdf/issues/753

K J
  • 8,045
  • 3
  • 14
  • 36
-1

I will continue looking for a way to streamline this further because I would love a simpler workflow, but here is what I use to ensure I do not have two articles sharing one sheet of paper.

pacman::p_load(tidyverse, pdftools, qpdf)

# some prep
directory <- "Some/FileFolder/Path"
filelist <- (paste(directory, "/",
                   list.files(directory,
                              pattern = "*.pdf"), sep = ""))

# bust apart all pdf to inspect
my_pages <- as.list(lapply(filelist, pdf_split))
page_summ <- cbind.data.frame(filelist,
                              lengths(my_pages)) %>%
  rename(filename = 1,
         pages = 2) %>%
  mutate(is_odd = pages %%2==1)


# separate into odd and even sets
odd_docs <- page_summ %>%
  filter(is_odd == TRUE)

even_docs <- page_summ %>%
  filter(is_odd == FALSE)

# I could not find an R process for adding a page to PDFs.
# For now, I will add a buffer page to docs via a PDF program.
# Once you are satisfied with your even_docs subset, pdf_combine

tocombine <- as.data.frame(even_docs$filename)

lapply(tocombine, pdf_combine)

This auto-generates the combo file into the previously defined directory. The new file name is not able to be set using "output = " within lapply(). Look for new file name = "firstnameintocombine_combined.pdf".