CUPS相關(guān)知識(shí),并嘗試搭建驅(qū)動(dòng)開(kāi)發(fā)環(huán)境。由于目前沒(méi)有實(shí)體打印機(jī),使用cups-pdf打印機(jī)代替。
開(kāi)發(fā)環(huán)境:
Linux 4.4.0-31-generic #50~14.04.1-Ubuntu SMP Wed Jul 13 01:07:32 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
環(huán)境搭建目標(biāo):
為方便開(kāi)發(fā)調(diào)試(需要看源碼)都采用編譯安裝
1. 編譯安裝CUPS及相關(guān)依賴(lài)
2. 編譯安裝CUPS-PDF打印機(jī)
3. 嘗試打印
編譯安裝過(guò)程:
1.下載并安裝cups
1.1下載cups
到git上https://github.com/apple/cups,找到最新的cups版本2.2.9,直接下載
https://github.com/apple/cups/releases/download/v2.2.9/cups-2.2.9-source.tar.gz
1.2解壓帶源碼的cups包
tar xzvf cups-2.2.9-source.tar.gz
得到目錄
cups-2.2.9
1.3根據(jù)INSTALL.md進(jìn)行安裝
根據(jù)要求安裝依賴(lài)庫(kù)
sudo apt-get install autoconf build-essential libavahi-client-dev libgnutls28-dev libkrb5-dev libnss-mdns libusb-1.0-0-dev zlib1g-dev
sudo apt-get install libpam-dev
apt-get install libsystemd-daemon-dev
1.4執(zhí)行configure
./configure
一般這一步會(huì)出現(xiàn)多次失敗,每一次都會(huì)提示系統(tǒng)缺少的依賴(lài)庫(kù),使用apt-cache search xxx找到相應(yīng)的依賴(lài)庫(kù)進(jìn)行安裝就可以了。configure成功之后就可以編譯了。其中,有一個(gè)很重要的依賴(lài)庫(kù)GNUTLS本身又依賴(lài)很多庫(kù),需要折騰一番。
1.5執(zhí)行make
./make
這一步也有可能編譯失敗,如果系統(tǒng)中安裝有多個(gè)libcups.so有可能默認(rèn)找到的庫(kù)不正確,需要進(jìn)行調(diào)整,確保編譯通過(guò)
1.6執(zhí)行make check
./make check
檢查編譯結(jié)果
1.7執(zhí)行make install
./make install
完成安裝
1.8 cups安裝完了?現(xiàn)在cups本身算是安裝上了,但是必須要安裝GPL Ghostscript和Open Printing CUPS filters。
2. 編譯GNUTLS庫(kù)
2.1 Gnutls庫(kù)主頁(yè)
https://www.gnutls.org/manual/gnutls.html
2.2 Gnutls下載頁(yè)
https://www.gnupg.org/ftp/gcrypt/gnutls/v3.6
2.3 下載鏈接
https://www.gnupg.org/ftp/gcrypt/gnutls/v3.6/gnutls-3.6.9.tar.xz
解壓:
xd -d gnutls-3.6.9.tar.xz
tar xvf gnutls-3.6.9.tar
2.4 安裝gnutls依賴(lài)庫(kù)
GnuTLS依賴(lài)nettle、gmplib庫(kù),還有asn.1庫(kù),以及p11-kit庫(kù)等,根據(jù)系統(tǒng)已經(jīng)安裝的軟件不同,遇到依賴(lài)也不同。可以通過(guò)運(yùn)行./configure查看缺少的依賴(lài)庫(kù)
2.4.1 安裝nettle
下載頁(yè)
https://ftp.gnu.org/gnu/nettle/
下載nettle 3.4.1
https://ftp.gnu.org/gnu/nettle/nettle-3.4.1.tar.gz
解壓
tar xzvf nettle-3.4.1.tar.gz
cd nettle-3.4.1
./configure --enable-mini-gmp
./make
./make install
注意,必須加上--enable-mini-gmp,要不然編譯出來(lái)缺少一個(gè)庫(kù)
2.4.2 安裝gmlib
https://gmplib.org/download/gmp/gmp-6.1.2.tar.lz
解壓
lzip -d gmp-6.1.2.tar.lz
tar xvf gmp-6.1.2.tar
cd gmp-6.1.2
./configure
./make
./make check
./make install
2.4.3 安裝p11-kit和asn.1
apt-get install libp11-kit-dev
apt-get install libtasn1-6-dev
2.4.4 編譯安裝gnutls
cd gnutls-3.6.8
./configure
./make check install
3.安裝Open Printing CUPS filters
Cups和Open Printer CUPS filters的關(guān)系是,早期版本他們是一體的,后來(lái)Open Printer CUPS filters單獨(dú)分出來(lái)維護(hù),CUPS只做Linux打印系統(tǒng)。更多的filters和backends都包括在Open Printer CUPS filters包中。
3.1 下載Open Printing CUPS filters
git地址:
https://github.com/OpenPrinting/cups-filters
下載包:
https://github.com/OpenPrinting/cups-filters/releases/download/release-1-25-1/cups-filters-1.25.1.tar.gz
解壓
tar xzvf cups-filters-1.25.1.tar.gz
cd cups-filters-1.25.1
3.1.1 生成configure
./autogen.sh
生成對(duì)應(yīng)的configure
3.1.2 執(zhí)行configure和編譯
./configure
這一步會(huì)提示各種需要的包,根據(jù)提示進(jìn)行安裝即可。
./make
./make install
4.安裝GPL Ghostscript
下載包
https://github.com/ArtifexSoftware/ghostpdl-downloads/releases/download/gs925/ghostscript-9.25.tar.gz
解壓
tar xzvf ghostscript-9.25.tar.gz
cd ghostscript-9.25
./configure
make
make install
特別注意,如果不使用編譯安裝Ghostscript,而使用apt-get install安裝的話,很可能出現(xiàn)因?yàn)榘惭bGhostscript而覆蓋原有的libcups.so包,導(dǎo)致其中未包含cups.2.2.9中的函數(shù)。建議如果是要自己開(kāi)發(fā)的話,這一步要編譯安裝。
5.安裝cups-pdf驅(qū)動(dòng)
網(wǎng)址:https://www.cups-pdf.de/
5.1下載包
https://www.cups-pdf.de/src/cups-pdf_3.0.1.tar.gz
解壓tar xzvf cups-pdf_3.0.1.tar.gz
cd cups-pdf-3.0.1
5.2編譯
cd src
gcc -O9 -s cups-pdf.c -o cups-pdf -lcups
5.3拷貝到指定位置
編譯成功之后,得到可執(zhí)行文件cups-pdf
ls -al
-rwxr-xr-x 1 root root? 138312? 7月 25 14:53 cups-pdf*
-rw-r--r-- 1 1133 staff? 35765? 7月 25 14:53 cups-pdf.c
-rw-r--r-- 1 1133 staff? 4997? 2月 24? 2017 cups-pdf.h
將cups-pdf拷貝到cups的backends目錄
cp -rf cups-pdf /usr/lib/cups/backend
5.4拷貝ppd
將CUPS-PDF_opt.ppd拷貝到/usr/share/cups/model
使用lpinfo -m命令查看
lsb/usr/cupsfilters/Fuji_Xerox-DocuPrint_CM305_df-PDF.ppd Fuji Xerox
drv:///sample.drv/dymo.ppd Dymo Label Printer
drv:///sample.drv/epson9.ppd Epson 9-Pin Series
drv:///sample.drv/epson24.ppd Epson 24-Pin Series
drv:///generic-brf.drv/gen-brf.ppd Generic Braille embosser, 1.0
CUPS-PDF_opt.ppd Generic CUPS-PDF Printer (w/ options)
drv:///cupsfilters.drv/pwgrast.ppd Generic IPP Everywhere Printer
6.配置打印機(jī)
lpadmin -p cups-pdf -E -v cups-pdf:/ -m CUPS-PDF_opt.ppd
查看打印機(jī)
lpstat -a
cups-pdf 自從 2019年07月24日 星期三 17時(shí)59分46秒 開(kāi)始接受請(qǐng)求
嘗試打印
lpr -P cups-pdf /etc/passwd
查看打印文件
ls -al /var/spool/cups-pdf/root/passwd__l_cups-pdf_root.pdf
-rw------- 1 root root 13614? 7月 29 14:52 /var/spool/cups-pdf/root/passwd__l_cups-pdf_root.pdf
7.Debug編譯和查看日志
開(kāi)發(fā)需要看日志,在編譯cups時(shí),可以添加相應(yīng)的debug選項(xiàng),這樣就可以成功輸出日志。
cups日志在這個(gè)目錄:
ls -al /var/log/cups
total 1256
drwxrwxrwx? 2 root root? ? ? 4096? 7月 29 14:36 ./
drwxrwxrwx 14 root syslog? ? 4096? 7月 29 14:36 ../
-rw-r--r--? 1 root lp? ? ? ? 763? 7月 29 14:57 access_log
-rw-r--r--? 1 root lp? ? ? 125231? 7月 29 14:57 error_log
-rwxrwxrwx? 1 root adm? ? ? ? ? 0? 7月 22 14:47 page_log*