PDFをリンクしますが、PDFを偶数ページに展開します。

PDFをリンクしますが、PDFを偶数ページに展開します。

複数のPDFをリンクしたいのですが、印刷目的で奇数ページの各文書に空白のページを追加したいと思います。 PDFTKを使用してこれを実行できますか?

答え1

これは、LaTeXを使用して現在のディレクトリ内のすべてのPDFファイルを繰り返し、1つのPDFにリンクするシンプルな小さなスクリプトです。ページ数が奇数のPDFには、その後に空白のページが追加されます。

#!/bin/bash

cat<<EoF > all.tex
\documentclass{article}
\usepackage{pdfpages}
\begin{document}
EoF

## rename the PDFs to something safe
c=0;
for f in *pdf
do
        ## Link the PDF with a safe name
        ln -s "$f" "$c".pdf
        ## Include the PDF in the tex file
        printf '\includepdf[pages=-]{%s.pdf}\n' "$c" >> all.tex;
        ## Get the number of pages
        pages=$(pdfinfo "$c".pdf | grep -oP '^Pages:\s*\K\d+')
        ## Add an empty page if they are odd
        [[ $(expr "$pages" % 2) != 0 ]] && 
            printf '%s\n' "\newpage\null" >> all.tex

        ((c++));
done

printf '\\end{document}' >> all.tex;
pdflatex all.tex

これはLaTeXなので、あらゆる種類の追加操作を実行できます。たとえば、各PDFをクリック可能な目次を含む独自のセクションに配置できます。

#!/bin/bash

cat<<EoF > all.tex
\documentclass{article}
\usepackage{pdfpages}
\usepackage[colorlinks=true,linkcolor=blue,linktoc=page]{hyperref}
\begin{document}
\tableofcontents
\newpage
EoF
## rename the PDFs to something safe
c=0;
for f in *pdf
do
        ## Link the PDF with a safe name
        ln -s "$f" "$c".pdf
        ## Include the PDF in the tex file
        cat<<EoF >> all.tex
\section{${f//.pdf}}
\includepdf[pages=-]{$c.pdf}
EoF
        ## Get the number of pages
        pages=$(pdfinfo "$c".pdf | grep -oP '^Pages:\s*\K\d+')
        ## This time, because each PDF has its own section title
        ## we want to add a blank page to the even numbered ones
        [[ $(expr "$pages" % 2) = 0 ]] && 
            printf '%s\n' "\newpage\null\newpage" >> all.tex

        ((c++));
done

printf '\\end{document}' >> all.tex;
## Need to run it twice to build the ToC
pdflatex all.tex; pdflatex all.tex;

答え2

確かに。ただ空白のページを作成してください(たとえば)

echo "" | ps2pdf -sPAPERSIZE=a4 - blank.pdf

blank.pdf奇数ページのすべての文書に追加されます。例えば

pdftk \
BLANK=blank.pdf \
A=foo1.pdf \
B=foo2.pdf \
C=foo3.pdf \
cat A BLANK B BLANK C \
output bar.pdf

答え3

私のような未来の放浪者のために。 @Faheem Mithaの答えを次のように組み合わせたスクリプトを作成しました。ここ

#!/bin/sh
# USAGE: ./concat-pdf.sh *.pdf output-file.pdf

ALL_FILES=""
BLANK_FILE="/tmp/blank.pdf"

for OUTPUT_FILE; do true; done
if test -f "$OUTPUT_FILE"; then
    echo "$OUTPUT_FILE already exists"
    exit 1
fi

echo "" | ps2pdf -sPAPERSIZE=a4 - $BLANK_FILE
for PDF_FILE; do
    if [ "$PDF_FILE" != "$OUTPUT_FILE" ]; then
        pages=$(strings < "$PDF_FILE" | sed -n 's|.*Count -\{0,1\}\([0-9]\{1,\}\).*|\1|p' | sort -rn | head -n 1)
        ALL_FILES="$ALL_FILES \"$PDF_FILE\""
        [ $((pages%2)) -ne 0 ] && ALL_FILES="$ALL_FILES $BLANK_FILE"
    fi
done

echo "pdftk $ALL_FILES cat output \"$OUTPUT_FILE\""

pdftk $ALL_FILES cat output "$OUTPUT_FILE"

rm -f $BLANK_FILE

答え4

UbuntuでPdftkを使用してPDFファイルのすべてのページの後に空のページを作成するために実行した手順は次のとおりです。

1-pdftkのインストール(まだインストールされていない場合)

sudo apt-get install pdftk

2- Blank.pdfという空のPDFファイルを作成します。 ImageMagickパッケージの変換コマンドを使用して空の画像を作成し、PDFに変換できます。次のコマンドを実行します。

sudo apt install imagemagick
convert -size 595x842 xc:white blank.jpg
convert blank.jpg blank.pdf

セキュリティポリシーエラーが発生した場合は、root権限でテキストエディタを使用してpolicy.xmlファイルを開きます。たとえば、次のコマンドを使用してナノエディタで開くことができます。

sudo nano /etc/ImageMagick-6/policy.xml

PDFの権限属性の値を[なし]から[読み取り]に変更します。修正された行は次のようになります。

<policy domain="coder" rights="read|write" pattern="PDF" />

3- PDFファイルがあるディレクトリに移動し、次のコマンドを使用します。

pdftk input.pdf burst output burst_output%d.pdf
pdftk $(for f in burst_output*.pdf; do echo -n "$f blank.pdf "; done) cat output final.pdf

「入力」の代わりに入力ファイルの名前を入力してください。

これでFinal.pdfが用意されました!その後、分割されたページを削除できます。

関連情報