2012-09-25 187 views
2

我的朋友問這個問題,他使用的是Mac,無法使用PdfLatex工作(沒有開發CD,相關here)。反正我的第一個想法:Unix:將PDF文件和圖像合併爲PDF文件?

  • $ PDFTK次數1.pdf 2.pdf 3.PDF貓輸出123.pdf [僅限PDF文件]
  • $轉換1.png 2.png myfile.pdf [僅圖像]

現在我不知道沒有乳膠或iPad的音符,再加如何將圖像和PDF -files結合起來。那麼我怎樣才能在Unix中結合pdf文件和圖像呢?

+0

謝謝你hhh!答案在Apple默認命令行中:http://stackoverflow.com/questions/4778635/merging-png-images-into-one-pdf-file-in-unix –

回答

1

您可以運行一個循環,識別PDF和圖像,並使用ImageMagick將圖像轉換爲PDF。完成後,您可以使用pdftk進行組裝。

這是一個Bash腳本。

#!/bin/bash 

# Convert arguments into list 
N=0 
for file in $*; do 
     files[$N]=$file 
     N=$[ $N + 1 ] 
done 
# Last element of list is our destination filename 
N=$[ $N - 1 ] 
LAST=$files[$N] 
unset files[$N] 
N=$[ $N - 1 ] 
# Check all files in the input array, converting image types 
T=0 
for i in $(seq 0 $N); do 
     file=${files[$i]} 
     case ${file##*.} in 
       jpg|png|gif|tif) 
         temp="tmpfile.$T.pdf" 
         convert $file $temp 
         tmp[$T]=$temp 
         uses[$i]=$temp 
         T=$[ $T + 1 ] 
         # Or also: tmp=("${tmp[@]}" "$temp") 
       ;; 
       pdf) 
         uses[$i]=$file 
       ;; 
     esac 
done 
# Now assemble PDF files 
pdftk ${uses[@]} cat output $LAST 
# Destroy all temporary file names. Disabled because you never know :-) 
echo "I would remove ${tmp[@]}" 
# rm ${tmp[@]} 
1