星期四, 3月 06, 2014

PDF加頁碼

要搞定PDF加頁碼

先用libreoffice製作頁碼空白檔案,然後輸出pdf    numbers.pdf

然後用這邊看到的bash去跑
http://forums.debian.net/viewtopic.php?t=30598




#!/bin/sh

# we'll hide the work in a temporary directory
mkdir tmp_num
cp numbers.pdf tmp_num/.
cp newbook.pdf tmp_num/.
cd tmp_num/

# burst newbook into its component pages and extract total pages
pdftk newbook.pdf burst output book_%04d.pdf 
cat doc_data.txt | grep NumberOfPages > nu_pages.txt
nu_pages=`mawk '{print $2}' nu_pages.txt`

# burst the page number file into its component pages
pdftk numbers.pdf burst output nums_%04d.pdf 

# no page number on the first page
cp book_0001.pdf fin_0001.pdf

# start converting pages from page 2
x=2 # initialize x

# place the page numbers on each page of newbook
while [ "$x" -le "$nu_pages" ]; do

     if [ "$x" -lt 10 ]; then
    pdftk book_000"$x".pdf background nums_000"$x".pdf output fin_000"$x".pdf
         echo "Finished page $x of $nu_pages."
         x=$(($x+1))

     elif [ "$x" -lt 100 ]; then
    pdftk book_00"$x".pdf background nums_00"$x".pdf output fin_00"$x".pdf
         echo "Finished page $x of $nu_pages."
         x=$(($x+1))

     elif [ "$x" -lt 1000 ]; then
    pdftk book_0"$x".pdf background nums_0"$x".pdf output fin_0"$x".pdf
         echo "Finished page $x of $nu_pages."
         x=$(($x+1))

     fi
done


# create the new PDF file and move it to the original directory
pdftk  fin_*.pdf cat output finbook.pdf
mv finbook.pdf ../.

# clean up the mess and exit
# cd ..
# rm tmp_num/*
# rmdir tmp_num

echo " "
echo "All done! Have fun!"
echo " "
exit


但pdftk burst梅子的大檔有java的問題,
 
所以又寫別的bash   http://www.alecjacobson.com/weblog/?p=3457
去burst PDF
/bash
if [ $# -eq 0 ]
  then
    echo "Usage:"
    echo "  pdftkburst input.pdf output-%04d.pdf"
    return 1
fi

NUM_PAGES=`pdfinfo $1 | grep Pages: | sed -e "s/ *Pages: *//g"`
#echo "NUM_PAGES: $NUM_PAGES."
for i in $(seq 1 ${NUM_PAGES})
do
  #echo "printf \"$2\" $i"
  PAGE_NAME=`printf "$2" $i`
  pdftk $1 cat $i output $PAGE_NAME
  #echo "Creating $PAGE_NAME"


後來輸出之後發現頁碼會被圖片壓住
所以試著將背景對調,頁碼放在前面
總算搞定

沒有留言: