views:

2877

answers:

5

Hello,

I am looking for a free tool that allows re-arranging pages of a PDF document and combining multiple pages per sheet. The first part (re-arranging) is easily solved by many tools (I use PyPDF).

The problem is with the second requirement: to combine two (or more) pages into single page. For example, take two pages (A and B), rotate them, scale and combine into a single page like this

------       ------            ------
|     |      |      |          |     |
|  A  |      |  B   |          | a   |
|     |      |      |          |     |
|     |      |      |  --->    ------
|     |      |      |          |     |        
|     |      |      |          | b   |        
|     |      |      |          |     |        
------       ------            ------

The solution needs to work on Linux and preferably on Windows too. I'm looking for either console application or library with Python or Perl bindings.

Edit there is pdfnup library that is supposed to perform exactly this kind of transformation, and is cross-platform, however I cannot use it due to a bug similar to this.

+2  A: 

On Linux, you can convert the PDF files to Postscript and use psnup. The exact way to invoke it depends on exactly how you want the pages to be put together, whether you want them rotated, what paper size(s) you want to use, etc. but it'll be something like this:

pdf2ps infile.pdf infile.pdf
psnup -2 infile.ps outfile.ps
ps2pdf outfile.ps outfile.pdf

Depending on what tools you have available, you might have a more efficient way to do this - psnup is certainly not the only way, but it's a relatively well-known program (on Linux anyway).

David Zaslavsky
A: 

Check the source code of PyPDF, especially the rotateClockwise() method. There must be a place where the content of a page is written. Insert a "q" operator (save state) and "cm" (with the correct parameters for a scaling matrix) before the content and a "Q" operator (restore state) afterwards.

See the PDF documentation for an explanation of operators and the structure of a page (scroll to the bottom for some useful links).

Don't forget to send a patch to PyPDF :)

[EDIT] You might also want to check the pdfjam sources which include a pdfnup command.

Aaron Digulla
+2  A: 

Check out this answer that uses Multivalent to impose PDF pages

danio
A: 

This is a perl function I use to grab a directory full of prn files from a 3rd party app and create a single merged pdf.

sub runMerged($)
{
    my($path) = @_;

    print "Generating merged PDFs for $path\n";

    my @files = sort(getFiles($path, ".prn\$"));
    if (scalar(@files))
    {
        open(MERGE, ">$path/merged.prn");
        for (my $i = 0; $i < scalar(@files); $i++)
        {
            print MERGE "^L\n" if ($i > 0);

            open(FN, "$path/" . $files[$i]);
            while (my $line = <FN>)
            {
                print MERGE $line;
            }
            close(FN);
        }

        chdir("$BASE_PATH/txt2pdf");
        print `./txt2pdf.pl $path/merged.prn`;
    }
}
+1  A: 

This is a summary of the tools I found for PDF (I wanted to find the equivalent of psup and psbook)

  • Create booklets: pdfbook, pdf-tools (command: pdfbklt)
  • Merge PDF files: pdfmerge, pdfjam (command: pdfjoin)
  • Rotate pages: pdfjam (command: pdf90)
  • Multiple pages per sheet: pdfjam (command: pdfnup)
  • Create posters (multiple sheets per page): pdfposter

From my package manager:

Create an A6 booklet:

pdfbook -2 -p a5 infile.pdf outfile.pdf

pdf-tools contains:

  • pdfbklt: create booklets
  • pdfrevert: Removes one layer of changes to a PDF file, trying to maximise the size of the output file (to account for linearised PDF).
  • pdfstamp: Adds the given string to the infile .pdf file at the given location, font and size.

There is also multivalent: http://multivalent.sourceforge.net/Tools/index.html