How to Batch Print Selected Pages from PDFs?
FedoraForum.org - Fedora Support Forums and Community
Results 1 to 9 of 9
  1. #1
    Join Date
    May 2010
    Posts
    197
    Linux Chrome 67.0.3396.99

    How to Batch Print Selected Pages from PDFs?

    I have a large number of PDF files that I need to batch print. Most of the files are 4-5 pages long, and I only want to print selected pages in each file.

    Optimally, I'd like to use a utility that can be controlled from the command line, to batch print pages 1 and 3 of say, 500 different files.

    Can anyone recommend a way to do this on Fedora?

    (I don't have a windows box)

  2. #2
    Join Date
    Dec 2007
    Location
    Australia
    Posts
    1,879
    Linux Pale Moon 28.1.0

    Re: How to Batch Print Selected Pages from PDFs?

    You could have a look at cpdf which I've used successfully in scripts for that sort of thing on a smaller scale.

  3. #3
    Join Date
    May 2010
    Posts
    197
    Linux Chrome 67.0.3396.99

    Re: How to Batch Print Selected Pages from PDFs?

    Thanks for the reply.

    To summarize, I'm trying to batch-print only pages 1 and 3 of many PDF documents. In a perfect world, I'd be able to select which pages to print from the input files (pages 1 and 3 only) and I'd also be able to reduce the output so that pages 1 and 3 are rendered on the same sheet of paper.

    Here;s what I've tried so far, with limited success:

    1. the bash "lp" built-in

    I have a 4-page document and I only want to print pages 1 and 3. I can batch print print pages 1 and 3 (full-size) without any problem. If that's all that anyone needs to do, the built-in lp command seems to be a good option. I used a command like this one:

    Code:
    $ lp inputfilename.pdf -d LaserJet -P 1,3 -o fit-to-page -o media=letter -o page-border=single

    Where lp fell short though, was when I tried to do two-up printing. lp has a command line parameter "-o number-up=x" that allows multiple pages to be reduced and printed on the same page. To do two-up printing, I tried the following command:

    Code:
    $ lp inputfilename.pdf -d LaserJet -P 1,3 -o fit-to-page -o media=letter -o page-border=single -o number-up=2

    This didn't work so well. The "number-up" command doesn't do quite what I expected. Instead of getting pages 1 and 3 printed together on a single page, I got pages 1 and 2 printed together on a single page, and pages 3 and 4 printed together on another single page. No combination of changes to the "-P 1,3" parameter seemed to be able to solve the problem of getting pages 1 and 3 from the document printed two-up on a single sheet of paper.

    As it turns out the "-P" parameter refers to the document's output pages, not to it's input pages, which makes the lp tool worthless in this application -- as soon as I tried to reduce the size of the pages I lost the ability to control which pages are selected for printing from the input file. I ended up directing a lot of output to the printer before I had to give up.

    2. cpdf

    cpdf is not in the Fedora repos, but it is available as a downloadable binary from this page. I tried the 64-bit x86-64 version.

    http://community.coherentpdf.com/

    cpdf was very easy to use, just download the binaries and run the program. One advantage of cpdf is that it was very easy to direct the output to a new PDF file, so that I didn't have to use my printer to do a lot of experimenting as I learned how to use the tool.

    The following command easily converted my PDF file from one-up to two-up formatting:

    Code:
    cpdf -twoup-stack -i input.pdf -o output.pdf

    This worked well enough, but I was still getting pages 1,2 printed two-up on the first page of the new PDF, and pages 3,4 printed two-up on the second page. What I really need is pages 1 and 3 printed two-up on a single PDF page. So far, no luck.

    The following commands all produced an identical result -- where the output was the same as the previous command -- pages 1,2 were printed two-up on the first page of the new PDF, and pages 3,4 were printed two-up on the second page:

    Code:
    cpdf -twoup-stack -i input.pdf 1,3    -o output.pdf                                                                      
    cpdf -twoup-stack -i input.pdf 1,2    -o output.pdf
    cpdf -twoup-stack -i input.pdf odd    -o output.pdf
    cpdf -twoup-stack -i input.pdf 1-3odd -o output.pdf
    So far I haven't had any luck getting cpdf to print pages 1 and 3 to a new PDF file if I use two-up printing to place them on the same page. It looks like cpdf is behaving in the same way that lp behaves, in that it's not using the specified input page range to control which pages are selected from the input file.

    The cpdf documentation clearly states that the page range parameter refers to the pages selected from the input file, though it is clearly not limiting the input to the odd numbered pages whenever I try to do two-up printing. Maybe this is a bug. Maybe I just don't know what I'm doing.

    Any ideas? I really need to find a way to control which input pages get printed, optimally with the ability to do two-up printing, so that pages 1 and 3 of my original document get reduced for printing on one output page. So far, no luck... any attempt at two-up printing botches the job execution.

    Thanks.

  4. #4
    Join Date
    May 2010
    Posts
    197
    Linux Chrome 67.0.3396.99

    Re: How to Batch Print Selected Pages from PDFs?

    Update:

    It is possible to get what I need using CPDF, as long as I'm willing to break up the job into multiple steps.

    First, extract pages 1,3 of the input pdf to a new document. This document properly contains only pages 1 and 3 from the original document as full-size pages:
    Code:
    cpdf -i input.pdf odd -o odd.pdf
    
    or 
    
    cpdf -i input.pdf 1,3 -o odd.pdf

    Second, convert the odd.pdf file to a new PDF file in which the two pages are reduced to fit on a single page:

    Code:
    cpdf -twoup-stack -i odd.pdf -o two-up.pdf

    Third, if you want the final PDF to be rotated 90 degrees, that has to be done in a separate step, otherwise adding "-rotate 90" to the previous command will botch the two-up rendering; you have to do two-up rendering in a separate process, and rotate after you've finished.

    Code:
    cpdf -twoup-stack -i two-up.pdf -rotate 90 -o two-up-rotated.pdf

    Ultimately I was able to succeed in proof of concept using CPDF, though I had to break up the job into separate tasks using intermediate temp files to get the job done. If there is a more direct way of using CPDF to get things done in one step, please let me know what I'm missing.

    Thanks for the recommendation of CPDF. It helped.
    Last edited by bob p; 7th November 2018 at 07:46 PM.

  5. #5
    Join Date
    May 2010
    Posts
    197
    Linux Chrome 67.0.3396.99

    Re: How to Batch Print Selected Pages from PDFs?

    Quote Originally Posted by bob p
    If there is a more direct way of using CPDF to get things done in one step, please let me know what I'm missing.
    Answering my own question, here's a winning one-liner:

    Code:
     cpdf -i input.pdf odd AND cpdf -twoup-stack AND cpdf -rotate 90 -o output.pdf
    piping the output (see the manual) or using the AND parameter results in avoiding the intermediate files, which results in less disk-thrashing and faster exec on a really big job.

    thanks again.

  6. #6
    Join Date
    Dec 2007
    Location
    Australia
    Posts
    1,879
    Linux Pale Moon 28.1.0

    Re: How to Batch Print Selected Pages from PDFs?

    Thanks for all that most interesting work ... glad it helped.

  7. #7
    Join Date
    Nov 2006
    Location
    Detroit
    Posts
    6,734
    Linux (Fedora) Firefox 63.0

    Re: How to Batch Print Selected Pages from PDFs?

    Quote Originally Posted by bob p
    I have a large number of PDF files that I need to batch print. Most of the files are 4-5 pages long, and I only want to print selected pages in each file.
    You can use Ghostscript (which is installed by default in Fedora).

    For example, if you want to print only pages 1 and 3 from input.pdf:
    Code:
    gs -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dSAFER -sPageList=1,3 -sOutputFile=output.pdf input.pdf
    That saves pages 1 and 3 to output.pdf, which you can then print (lots of ways to do that).
    OS: Fedora 28 x86_64 | Machine: HP Pavilion a6130n | CPU: AMD 64 X2 Dual-Core 5000+ 2.6GHz | RAM: 7GB PC5300 DDR2 | Disk: 400GB SATA | Video: ATI Radeon HD 4350 512MB | Sound: Realtek ALC888S | Ethernet: Realtek RTL8201N

  8. #8
    Join Date
    Jan 2010
    Posts
    7,357
    Linux Chrome 70.0.3538.77

    Re: How to Batch Print Selected Pages from PDFs?

    Thanks Rupert, I didn't know that one but will be very handy for me, on the BSDs as well as Linux.

  9. #9
    Join Date
    Dec 2007
    Location
    Australia
    Posts
    1,879
    Linux Pale Moon 28.1.0

    Re: How to Batch Print Selected Pages from PDFs?

    What smr said! Very neat RupertPupkin.

Similar Threads

  1. Replies: 0
    Last Post: 10th June 2010, 06:49 AM
  2. Batch reorder PDF pages (via pdftex)
    By FFUser in forum Using Fedora
    Replies: 8
    Last Post: 9th April 2010, 04:26 PM
  3. Replies: 1
    Last Post: 30th September 2008, 04:56 PM
  4. print multiple pages to pdf
    By koruthaiolos in forum Using Fedora
    Replies: 2
    Last Post: 30th March 2007, 05:21 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •