Question

Having some issues using pdftk.

I'm trying to get the second page of every PDF in a specified directory and combine them into one output file.

Note: the PDF files in question were created with wkhtmltopdf.

I change into the directory and run the following:

mkdir trimmed-combined
for i in *pdf ; do
  pdftk “$i” cat 2 output “trimmed-combined/trimmed-combined1.pdf” ;
done

I'm receiving the following error:

Unexpected Exception in open_reader()
java.lang.IllegalArgumentException: null filename passed into RandomAccessFileOrArray()
   at pdftk.com.lowagie.text.pdf.RandomAccessFileOrArray.<init>(pdftk)
   at pdftk.com.lowagie.text.pdf.RandomAccessFileOrArray.<init>(pdftk)
   at pdftk.com.lowagie.text.pdf.PRTokeniser.<init>(pdftk)
   at pdftk.com.lowagie.text.pdf.PdfReader.<init>(pdftk)
   at pdftk.com.lowagie.text.pdf.PdfReader.<init>(pdftk)
Error: Failed to open PDF file: 
   “??

The files are definitely there so I'm not sure why they aren't being read properly.

No correct solution

OTHER TIPS

I've used your original command (which I only re-formatted for readability):

mkdir trimmed-combined
for i in *pdf ; do
  pdftk “$i” cat 2 output “trimmed-combined/trimmed-combined1.pdf” ;
done

I'm getting the exact same errors as you did...

But as already @MartinSchröder observed in a comment, and as I noticed when editing your OP for improved readability, the quotes you used are:

$  unicode “”

  U+201C LEFT DOUBLE QUOTATION MARK
  UTF-8: e2 80 9c  UTF-16BE: 201c  Decimal: &#8220;
  “
  Category: Pi (Punctuation, Initial quote)
  Bidi: ON (Other Neutrals)

  U+201D RIGHT DOUBLE QUOTATION MARK
  UTF-8: e2 80 9d  UTF-16BE: 201d  Decimal: &#8221;
  ”
  Category: Pf (Punctuation, Final quote)
  Bidi: ON (Other Neutrals)

I assume you have copied'n'pasted (some parts of) this command from a HTML-email, or from a PDF document, or from some web page which used "smart" quotes. These quotes will not work in a shell as intended. You need to use the ASCII quotes:

$  unicode \"

  U+0022 QUOTATION MARK
  UTF-8: 22  UTF-16BE: 0022  Decimal: &#34;
  "
  Category: Po (Punctuation, Other)
  Bidi: ON (Other Neutrals)

So change your command to this, and everything will work as expected:

mkdir trimmed-combined
for i in *pdf ; do
  pdftk "$i" cat 2 output "trimmed-combined/trimmed-combined1.pdf" ;
done
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top