一、系统环境:Centos7 x64

uname -rvms

二、Office转PDF:Unoconv

1. 说明

unoconv是一个python脚本,再使用的时候需要用到libreo?ce-pyuno

2. 环境

yum install unzip
yum install -y libreoffice-pyuno
yum remove -y libreoffice* openoffice*  # 去除因安装libreo?ce-pyuno带来的低版本libreoffice

3. libreoffice和unoconv下载

# 通过以下地址下载官方libreOffice_6.6.0
https://www.libreoffice.org/donate/dl/rpm-x86_64/6.4.0/zh-CN/LibreOffice_6.4.0_Linux_x86-64_rpm.tar.gz
# 通过以下地址下载官方unoconv_0.8.2
https://github.com/unoconv/unoconv/archive/0.8.2.zip

4. 安装

# libreoffice上传至服务器后解压
tar zxvf LibreOffice_6.4.0_Linux_x86-64_rpm.tar.gz
# 把所有rpm包都安装上,包括依赖
yum install LibreOffice_6.4.0.3_Linux_x86-64_rpm/RPMS/*.rpm -y
# unoconv上传至服务器后解压至安装
unzip unoconv-0.8.2.zip
cd unoconv-0.8.2/
make install

5. 安装转换检验

unoconv --version # 版本查看
# LibreO?ce版本应该是6.4,unoconv版本应该是0.8.2
# 检验是否可以转换文档
unoconv -f pdf -o /root/output.pdf /root/input.docx   # 1.pdf为转换后的文件,1.docx为需要转换的文件
unoconv -f pdf -e PageRange=2-2 -o /root/output.pdf /root/input.docx  # 只转换第2页

6. 中文问题解决

此时可能转出来的文档为空,或者中文乱码,解决方法如下

yum install fontconfig mkfontscale -y
mkdir -p /usr/share/fonts/win
# 然后将windows下的字体文件上传到此目录
# 执行命令使之生效
cd /usr/share/fonts/win
mkfontscale
mkfontdir
sudo fc-cache -f

三、PDF打水印:Imagemagick

1. 说明

Imagemagick是一个图象处理软件。它可以编辑、显示包括JPEG、TIFF、PNM、PNG、GIF和Photo CS在内的绝大多数当今最流行的图象格式。同时可以为各类图片打水印。

2. 环境

yum install -y libjpeg* libpng* libtiff* libungif* freetype zlib
yum install -y ghostscript # 不装转换的时候会找不到文件路径

3. 下载

需要从官网下载2个rpm安装包,下载地址

https://imagemagick.org/download/linux/CentOS/x86_64/ImageMagick-7.0.9-25.x86_64.rpm
https://imagemagick.org/download/linux/CentOS/x86_64/ImageMagick-libs-7.0.9-25.x86_64.rpm

4. 安装

# 将2个rpm包上传至服务,执行以下命令
yum install ImageMagick-libs-7.0.9-25.x86_64.rpm
yum install ImageMagick-7.0.9-25.x86_64.rpm

5. 安装检验打水印

convert -density 300 -depth 8 -quality 85 1.pdf re.png # pdf转png
convert 1.png -font /usr/share/fonts/win/simsun.ttc -density 300 -depth 8 -quality 85 -gravity center -fill "rgba(255, 0, 0, 0.6)" -pointsize 100 -draw "text 0,0 '南湾科技'" re.pdf # png打水印转pdf

pdf直接加水印,单效果比较差

convert a.pdf -font /usr/share/fonts/win/simsun.ttc -gravity center -fill "rgba(255, 0, 0, 0.6)" -pointsize 100 -draw "text 5,5 'ContentHub'" re.pdf

批量图片合并为pdf

convert "*.{png,jpeg,jpg}" -quality 100 re.pdf

打水印示例

# 居中
convert re-0.png -size 1000x1469 -font /usr/share/fonts/win/simsun.ttc -density 300 -depth 8 -quality 85 -gravity center -fill "rgba(41, 36, 33, 0.4)" -pointsize 30 -draw "text 0,-450 'aaaaaa'" -draw "text 0,-300 'bbbbbb'" -draw "text 0,-150 'cccccc'" -draw "text 0,150 'dddddd'" -draw "text 0,300 'eeeeee'" long.jpg
# 靠左
convert re-0.png -size 1000x1469 -font /usr/share/fonts/win/simsun.ttc -density 300 -depth 8 -quality 85 -fill "rgba(41, 36, 33, 0.4)" -pointsize 30 -draw "text 400,1250 'aaaaaa'" -draw "text 400,1400 'bbbbbb'" -draw "text 400,1550 'cccccc'" -draw "text 400,1850 'dddddd'" -draw "text 400,2000 'eeeeee'" long.jpg
# 居中倾斜30度
convert input.png -size 1000x1469 -font /usr/share/fonts/win/simsun.ttc -density 300 -depth 8 -quality 85 -gravity center -fill "rgba(41, 36, 33, 0.4)" -pointsize 30 -draw "rotate -30 text 0,-450 'aaaaaa'" -draw "rotate -30 text 0,-300 'bbbbbb'" -draw "rotate -30 text 0,-150 'cccccc'" -draw "rotate -30 text 0,150 'dddddd'" -draw "rotate -30 text 0,300 'eeeeee'" output.pdf