2012-09-25 188 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