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;

おすすめ記事