1

I have two PDF files, each has the same number of pages. I want a PDF file with the same number of pages, and each page is a superimposition of the pages of the same number in the two source files. As in, page 1 file 1 + page 1 file 2 = resulting page 1, etc.

With pdftk I can do it in a simple way:

pdftk file1.pdf multibackgroud file2.pdf output file3.pdf

However, I now need to do this on a Fedora machine, and Fedora does not have pdftk. And I can't build it because libgcj abd gcc-java are also absent.

As a last resort I can create an OpenSUSE VM, as OpenSUSE has pdftk. But perhaps there is a tool that can do the same job and is available on Fedora?

(I found CoherentPDF but it is licensed "not for commercial use", which is not something workable for me now).

  • There is a github bug on the lack of pdftk which gives alternate install sources for it, which may or may not be what you want (and may or may not work, since it focused on CentOS7). – Ulrich Schwarz Jun 25 '18 at 5:07
2

I don't know if there is a pure command line solution for this, but PyPDF2 can do the trick! I just adapted a Gist I found to fit your needs, in case you aren't familiar with programming Python.

The following code is also available as a Github Gist.

Don't forget to do a sudo dnf install python3-PyPDF2 and to change the filenames.

#!/bin/python3


from PyPDF2 import PdfFileReader, PdfFileWriter
from PyPDF2.pdf import PageObject

# Theses files are just for testing, no point in merging these
reader = PdfFileReader(open("Nextcloud Manual.pdf",'rb'))

# this defines the output page format (relevant if not the same)
sup_reader = PdfFileReader(open("Cplusplus.pdf",'rb'))

writer = PdfFileWriter()

for pageNo in range(min(reader.getNumPages(), sup_reader.getNumPages())):
    print("Merging page:", pageNo)
    invoice_page = reader.getPage(pageNo)
    sup_page = sup_reader.getPage(pageNo)
    translated_page = PageObject.createBlankPage(None, sup_page.mediaBox.getWidth(), sup_page.mediaBox.getHeight())
    translated_page.mergeScaledTranslatedPage(sup_page, 1, 0, 0)

    translated_page.mergePage(invoice_page)

    writer.addPage(translated_page)

with open('out.pdf', 'wb') as f:
    writer.write(f)

Edit: Note that this silently ignores exceeding pages of the PDF which has more pages. I could provide a better solution for that if needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.