最近这段时间从Android转入了c++服务端开发,接了一个任务,在 centos 6.5下把pdf 、文本文件、图片等转换为 tiff格式(传真需要的格式),因此研究了一番linux下cups打印原理与系统,现在基本摸清,可以把任意pdf和图片转换为tiff,linux下utf8的文本文档 如果有中文,则中文会叠在一起,windows下gbk的中文无法打印,有知道的兄弟告诉我一下。
流程:
pdf、图片、文本 -----------------------------------> PostScript ---------------------------------->tiff
以上流程利用了标准linux打印的基本流程。
配置文件:
/etc/cups/cupsd.conf :cups服务配置文件
MaxLogSize 0
#
# "$Id: cupsd.conf.in 8805 2009-08-31 16:34:06Z mike $"
#
# Sample configuration file for the CUPS scheduler. See "man cupsd.conf" for a
# complete description of this file.
#
# Log general information in error_log - change "warn" to "debug"
# for troubleshooting...
LogLevel warn
# Administrator user group...
SystemGroup sys root
# Only listen for connections from the local machine.
Listen localhost:631
Listen /var/run/cups/cups.sock
# Show shared printers on the local network.
Browsing On
BrowseOrder allow,deny
BrowseAllow all
BrowseLocalProtocols CUPS dnssd
# Default authentication type, when authentication is required...
DefaultAuthType Basic
# Restrict access to the server...
<Location />
Order allow,deny
</Location>
# Restrict access to the admin pages...
<Location /admin>
Order allow,deny
</Location>
# Restrict access to configuration files...
<Location /admin/conf>
AuthType Default
Require user @SYSTEM
Order allow,deny
</Location>
# Set the default printer/job policies...
<Policy default>
# Job-related operations must be done by the owner or an administrator...
<Limit Send-Document Send-URI Hold-Job Release-Job Restart-Job Purge-Jobs Set-Job-Attributes Create-Job-Subscription Renew-Subscription Cancel-Subscription Get-Notifications Reprocess-Job Cancel-Current-Job Suspend-Current-Job Resume-Job CUPS-Move-Job CUPS-Get-Document>
Require user @OWNER @SYSTEM
Order deny,allow
</Limit>
# All administration operations require an administrator to authenticate...
<Limit CUPS-Add-Modify-Printer CUPS-Delete-Printer CUPS-Add-Modify-Class CUPS-Delete-Class CUPS-Set-Default CUPS-Get-Devices>
AuthType Default
Require user @SYSTEM
Order deny,allow
</Limit>
# All printer operations require a printer operator to authenticate...
<Limit Pause-Printer Resume-Printer Enable-Printer Disable-Printer Pause-Printer-After-Current-Job Hold-New-Jobs Release-Held-New-Jobs Deactivate-Printer Activate-Printer Restart-Printer Shutdown-Printer Startup-Printer Promote-Job Schedule-Job-After CUPS-Accept-Jobs CUPS-Reject-Jobs>
AuthType Default
Require user @SYSTEM
Order deny,allow
</Limit>
# Only the owner or an administrator can cancel or authenticate a job...
<Limit Cancel-Job CUPS-Authenticate-Job>
Require user @OWNER @SYSTEM
Order deny,allow
</Limit>
<Limit All>
Order deny,allow
</Limit>
</Policy>
# Set the authenticated printer/job policies...
<Policy authenticated>
# Job-related operations must be done by the owner or an administrator...
<Limit Create-Job Print-Job Print-URI>
AuthType Default
Order deny,allow
</Limit>
<Limit Send-Document Send-URI Hold-Job Release-Job Restart-Job Purge-Jobs Set-Job-Attributes Create-Job-Subscription Renew-Subscription Cancel-Subscription Get-Notifications Reprocess-Job Cancel-Current-Job Suspend-Current-Job Resume-Job CUPS-Move-Job CUPS-Get-Document>
AuthType Default
Require user @OWNER @SYSTEM
Order deny,allow
</Limit>
# All administration operations require an administrator to authenticate...
<Limit CUPS-Add-Modify-Printer CUPS-Delete-Printer CUPS-Add-Modify-Class CUPS-Delete-Class CUPS-Set-Default>
AuthType Default
Require user @SYSTEM
Order deny,allow
</Limit>
# All printer operations require a printer operator to authenticate...
<Limit Pause-Printer Resume-Printer Enable-Printer Disable-Printer Pause-Printer-After-Current-Job Hold-New-Jobs Release-Held-New-Jobs Deactivate-Printer Activate-Printer Restart-Printer Shutdown-Printer Startup-Printer Promote-Job Schedule-Job-After CUPS-Accept-Jobs CUPS-Reject-Jobs>
AuthType Default
Require user @SYSTEM
Order deny,allow
</Limit>
# Only the owner or an administrator can cancel or authenticate a job...
<Limit Cancel-Job CUPS-Authenticate-Job>
AuthType Default
Require user @OWNER @SYSTEM
Order deny,allow
</Limit>
<Limit All>
Order deny,allow
</Limit>
</Policy>
#
# End of "$Id: cupsd.conf.in 8805 2009-08-31 16:34:06Z mike $".
#
/etc/cups/printers.conf :具体打印机的配置文件,其中的MDSFaxServer是我自己创建的虚拟打印机,其中加粗的地方需要注意
Filter 用来指出对于相应的MIME type,该找到哪个filter转换它。(filter是cups的重要组件,完成各种格式的转换)
DeviceURI 用来指出/usr/lib/cups/backend下的下一步处理的程序,这里指向下面的/usr/lib/cups/backend/mdsfaxserver脚本
<Printer MDSFaxServer>
Info MDS Fax Server
<strong>MakeModel Generic PostScript Printer Foomatic/Postscript (recommended)
DeviceURI mdsfaxserver:/</strong>
State Idle
StateTime 1418957192
Type 8433692
<strong>Filter application/vnd.cups-raw 0 -
Filter application/vnd.cups-postscript 100 foomatic-rip
Filter application/vnd.cups-pdf 0 foomatic-rip</strong>
Accepting Yes
Shared No
JobSheets none none
QuotaPeriod 0
PageLimit 0
KLimit 0
OpPolicy default
ErrorPolicy retry-job
</Printer>
/etc/cups/ppd/MDSFaxServer.ppd
ppd文件的作用相当于打印机驱动程序,指定了打印时的具体参数,例如页面参数,用什么filter转换。
打印机的ppd文件或者具体驱动可以到http://www.linuxfoundation.org/ 下载
*PPD-Adobe: "4.3"
*%
*% For information on using this, and to obtain the required backend
*% script, consult http://www.openprinting.org/
*%
*% This file is published under the GNU General Public License
*%
*% PPD-O-MATIC (4.0.0 or newer) generated this PPD file. It is for use with
*% all programs and environments which use PPD files for dealing with
*% printer capability information. The printer must be configured with the
*% "foomatic-rip" backend filter script of Foomatic 4.0.0 or newer. This
*% file and "foomatic-rip" work together to support PPD-controlled printer
*% driver option access with all supported printer drivers and printing
*% spoolers.
*%
*% To save this file on your disk, wait until the download has completed
*% (the animation of the browser logo must stop) and then use the
*% "Save as..." command in the "File" menu of your browser or in the
*% pop-up manu when you click on this document with the right mouse button.
*% DO NOT cut and paste this file into an editor with your mouse. This can
*% introduce additional line breaks which lead to unexpected results.
*%
*% You may save this file as 'Generic-PostScript_Printer-Postscript.ppd'
*%
*%
*FormatVersion: "4.3"
*FileVersion: "1.1"
*LanguageEncoding: English
*LanguageVersion: ISOLatin1
*PCFileName: "POSTSCRI.PPD"
*Manufacturer: "Generic"
*Product: "(PostScript Printer)"
*cupsVersion: 1.0
*cupsManualCopies: True
*cupsModelNumber: 2
*cupsFilter: "application/vnd.cups-postscript 100 foomatic-rip"
*cupsFilter: "application/vnd.cups-pdf 0 foomatic-rip"
*%pprRIP: foomatic-rip other
*ModelName: "Generic PostScript Printer"
*ShortNickName: "Generic P.S. Printer Postscript"
*NickName: "Generic PostScript Printer Foomatic/Postscript (recommended)"
*PSVersion: "(3010.000) 550"
*PSVersion: "(3010.000) 651"
*PSVersion: "(3010.000) 652"
*PSVersion: "(3010.000) 653"
*PSVersion: "(3010.000) 704"
*PSVersion: "(3010.000) 705"
*PSVersion: "(3010.000) 800"
*PSVersion: "(3010.000) 815"
*PSVersion: "(3010.000) 850"
*PSVersion: "(3010.000) 860"
*PSVersion: "(3010.000) 861"
*PSVersion: "(3010.000) 862"
*PSVersion: "(3010.000) 863"
*PSVersion: "(3010.000) 864"
*PSVersion: "(3010.000) 870"
*LanguageLevel: "3"
*ColorDevice: True
*DefaultColorSpace: RGB
*FileSystem: False
*Throughput: "1"
*LandscapeOrientation: Plus90
*TTRasterizer: Type42
*1284DeviceID: "DRV:DPostscript,R1,M0,F1,P0,TP;"
*driverName Postscript: ""
*driverType P/PostScript: ""
*driverUrl: "http://partners.adobe.com/public/developer/ps/index_specs.html"
*driverObsolete: False
*driverManufacturerSupplied: False
*driverFreeSoftware: True
*HWMargins: 18 36 18 36
*VariablePaperSize: True
*MaxMediaWidth: 100000
*MaxMediaHeight: 100000
*NonUIOrderDependency: 100 AnySetup *CustomPageSize
*CustomPageSize True: "pop pop pop
<</PageSize [ 5 -2 roll ] /ImagingBBox null>>setpagedevice"
*End
*ParamCustomPageSize Width: 1 points 36 100000
*ParamCustomPageSize Height: 2 points 36 100000
*ParamCustomPageSize Orientation: 3 int 0 0
*ParamCustomPageSize WidthOffset: 4 points 0 0
*ParamCustomPageSize HeightOffset: 5 points 0 0
*FoomaticIDs: Generic-PostScript_Printer Postscript
*FoomaticRIPCommandLine: "cat%A%B%Z"
*FoomaticRIPNoPageAccounting: True
*OpenGroup: General/General
*OpenUI *PageSize/Page Size: PickOne
*OrderDependency: 100 AnySetup *PageSize
*DefaultPageSize: Letter
*PageSize Letter/US Letter: "<</PageSize[612 792]/ImagingBBox null>>setpagedevice"
*PageSize A4/A4: "<</PageSize[595 842]/ImagingBBox null>>setpagedevice"
*PageSize 11x17/11x17: "<</PageSize[792 1224]/ImagingBBox null>>setpagedevice"
*PageSize A3/A3: "<</PageSize[842 1191]/ImagingBBox null>>setpagedevice"
*PageSize A5/A5: "<</PageSize[421 595]/ImagingBBox null>>setpagedevice"
*PageSize B5/B5 (JIS): "<</PageSize[516 729]/ImagingBBox null>>setpagedevice"
*PageSize Env10/Envelope #10: "<</PageSize[297 684]/ImagingBBox null>>setpagedevice"
*PageSize EnvC5/Envelope C5: "<</PageSize[459 649]/ImagingBBox null>>setpagedevice"
*PageSize EnvDL/Envelope DL: "<</PageSize[312 624]/ImagingBBox null>>setpagedevice"
*PageSize EnvISOB5/Envelope B5: "<</PageSize[499 709]/ImagingBBox null>>setpagedevice"
*PageSize EnvMonarch/Envelope Monarch: "<</PageSize[279 540]/ImagingBBox null>>setpagedevice"
*PageSize Executive/Executive: "<</PageSize[522 756]/ImagingBBox null>>setpagedevice"
*PageSize Legal/US Legal: "<</PageSize[612 1008]/ImagingBBox null>>setpagedevice"
*CloseUI: *PageSize
*OpenUI *PageRegion: PickOne
*OrderDependency: 100 AnySetup *PageRegion
*DefaultPageRegion: Letter
*PageRegion Letter/US Letter: "<</PageSize[612 792]/ImagingBBox null>>setpagedevice"
*PageRegion A4/A4: "<</PageSize[595 842]/ImagingBBox null>>setpagedevice"
*PageRegion 11x17/11x17: "<</PageSize[792 1224]/ImagingBBox null>>setpagedevice"
*PageRegion A3/A3: "<</PageSize[842 1191]/ImagingBBox null>>setpagedevice"
*PageRegion A5/A5: "<</PageSize[421 595]/ImagingBBox null>>setpagedevice"
*PageRegion B5/B5 (JIS): "<</PageSize[516 729]/ImagingBBox null>>setpagedevice"
*PageRegion Env10/Envelope #10: "<</PageSize[297 684]/ImagingBBox null>>setpagedevice"
*PageRegion EnvC5/Envelope C5: "<</PageSize[459 649]/ImagingBBox null>>setpagedevice"
*PageRegion EnvDL/Envelope DL: "<</PageSize[312 624]/ImagingBBox null>>setpagedevice"
*PageRegion EnvISOB5/Envelope B5: "<</PageSize[499 709]/ImagingBBox null>>setpagedevice"
*PageRegion EnvMonarch/Envelope Monarch: "<</PageSize[279 540]/ImagingBBox null>>setpagedevice"
*PageRegion Executive/Executive: "<</PageSize[522 756]/ImagingBBox null>>setpagedevice"
*PageRegion Legal/US Legal: "<</PageSize[612 1008]/ImagingBBox null>>setpagedevice"
*CloseUI: *PageRegion
*DefaultImageableArea: Letter
*ImageableArea Letter/US Letter: "18 36 594 756"
*ImageableArea A4/A4: "18 36 577 806"
*ImageableArea 11x17/11x17: "18 36 774 1188"
*ImageableArea A3/A3: "18 36 824 1155"
*ImageableArea A5/A5: "18 36 403 559"
*ImageableArea B5/B5 (JIS): "18 36 498 693"
*ImageableArea Env10/Envelope #10: "18 36 279 648"
*ImageableArea EnvC5/Envelope C5: "18 36 441 613"
*ImageableArea EnvDL/Envelope DL: "18 36 294 588"
*ImageableArea EnvISOB5/Envelope B5: "18 36 481 673"
*ImageableArea EnvMonarch/Envelope Monarch: "18 36 261 504"
*ImageableArea Executive/Executive: "18 36 504 720"
*ImageableArea Legal/US Legal: "18 36 594 972"
*DefaultPaperDimension: Letter
*PaperDimension Letter/US Letter: "612 792"
*PaperDimension A4/A4: "595 842"
*PaperDimension 11x17/11x17: "792 1224"
*PaperDimension A3/A3: "842 1191"
*PaperDimension A5/A5: "421 595"
*PaperDimension B5/B5 (JIS): "516 729"
*PaperDimension Env10/Envelope #10: "297 684"
*PaperDimension EnvC5/Envelope C5: "459 649"
*PaperDimension EnvDL/Envelope DL: "312 624"
*PaperDimension EnvISOB5/Envelope B5: "499 709"
*PaperDimension EnvMonarch/Envelope Monarch: "279 540"
*PaperDimension Executive/Executive: "522 756"
*PaperDimension Legal/US Legal: "612 1008"
*OpenUI *InputSlot/Media Source: PickOne
*OrderDependency: 120 AnySetup *InputSlot
*DefaultInputSlot: Default
*InputSlot Default/Default: "<</ManualFeed false>>setpagedevice"
*InputSlot Upper/Upper Tray: "<</ManualFeed false /MediaPosition 0>>setpagedevice"
*InputSlot Lower/Lower Tray: "<</ManualFeed false /MediaPosition 1>>setpagedevice"
*InputSlot Multipurpose/Multipurpose Tray: "<</ManualFeed false /MediaPosition 3>>setpagedevice"
*InputSlot Manual/Manual Feeder: "<</ManualFeed true /MediaPosition 3>>setpagedevice"
*CloseUI: *InputSlot
*OpenUI *Duplex/Double-Sided Printing: PickOne
*OrderDependency: 130 AnySetup *Duplex
*DefaultDuplex: None
*Duplex DuplexNoTumble/Long Edge (Standard): "<</Duplex true /Tumble false>>setpagedevice"
*Duplex DuplexTumble/Short Edge (Flip): "<</Duplex true /Tumble true>>setpagedevice"
*Duplex None/Off: "<</Duplex false>>setpagedevice"
*CloseUI: *Duplex
*OpenUI *Resolution/Resolution: PickOne
*OrderDependency: 90 AnySetup *Resolution
*DefaultResolution: 600x600dpi
*Resolution 150x150dpi/150x150 DPI: "<</HWResolution[150 150]>>setpagedevice"
*Resolution 300x300dpi/300x300 DPI: "<</HWResolution[300 300]>>setpagedevice"
*Resolution 600x600dpi/600x600 DPI: "<</HWResolution[600 600]>>setpagedevice"
*Resolution 1200x600dpi/1200x600 DPI: "<</HWResolution[1200 600]>>setpagedevice"
*Resolution 1200x1200dpi/1200x1200 DPI: "<</HWResolution[1200 1200]>>setpagedevice"
*Resolution 2400x600dpi/2400x600 DPI: "<</HWResolution[2400 600]>>setpagedevice"
*Resolution 2400x1200dpi/2400x1200 DPI: "<</HWResolution[2400 1200]>>setpagedevice"
*Resolution 2400x2400dpi/2400 DPI: "<</HWResolution[2400 2400]>>setpagedevice"
*CloseUI: *Resolution
*CloseGroup: General
*% Generic boilerplate PPD stuff as standard PostScript fonts and so on
*DefaultFont: Courier
*Font AvantGarde-Book: Standard "(001.006S)" Standard ROM
*Font AvantGarde-BookOblique: Standard "(001.006S)" Standard ROM
*Font AvantGarde-Demi: Standard "(001.007S)" Standard ROM
*Font AvantGarde-DemiOblique: Standard "(001.007S)" Standard ROM
*Font Bookman-Demi: Standard "(001.004S)" Standard ROM
*Font Bookman-DemiItalic: Standard "(001.004S)" Standard ROM
*Font Bookman-Light: Standard "(001.004S)" Standard ROM
*Font Bookman-LightItalic: Standard "(001.004S)" Standard ROM
*Font Courier: Standard "(002.004S)" Standard ROM
*Font Courier-Bold: Standard "(002.004S)" Standard ROM
*Font Courier-BoldOblique: Standard "(002.004S)" Standard ROM
*Font Courier-Oblique: Standard "(002.004S)" Standard ROM
*Font Helvetica: Standard "(001.006S)" Standard ROM
*Font Helvetica-Bold: Standard "(001.007S)" Standard ROM
*Font Helvetica-BoldOblique: Standard "(001.007S)" Standard ROM
*Font Helvetica-Narrow: Standard "(001.006S)" Standard ROM
*Font Helvetica-Narrow-Bold: Standard "(001.007S)" Standard ROM
*Font Helvetica-Narrow-BoldOblique: Standard "(001.007S)" Standard ROM
*Font Helvetica-Narrow-Oblique: Standard "(001.006S)" Standard ROM
*Font Helvetica-Oblique: Standard "(001.006S)" Standard ROM
*Font NewCenturySchlbk-Bold: Standard "(001.009S)" Standard ROM
*Font NewCenturySchlbk-BoldItalic: Standard "(001.007S)" Standard ROM
*Font NewCenturySchlbk-Italic: Standard "(001.006S)" Standard ROM
*Font NewCenturySchlbk-Roman: Standard "(001.007S)" Standard ROM
*Font Palatino-Bold: Standard "(001.005S)" Standard ROM
*Font Palatino-BoldItalic: Standard "(001.005S)" Standard ROM
*Font Palatino-Italic: Standard "(001.005S)" Standard ROM
*Font Palatino-Roman: Standard "(001.005S)" Standard ROM
*Font Symbol: Special "(001.007S)" Special ROM
*Font Times-Bold: Standard "(001.007S)" Standard ROM
*Font Times-BoldItalic: Standard "(001.009S)" Standard ROM
*Font Times-Italic: Standard "(001.007S)" Standard ROM
*Font Times-Roman: Standard "(001.007S)" Standard ROM
*Font ZapfChancery-MediumItalic: Standard "(001.007S)" Standard ROM
*Font ZapfDingbats: Special "(001.004S)" Standard ROM
该脚本可执行,用于接收转换后的postscript流,然后将该流使用gs(GhostScript)转换为tiff
#!/bin/bash
<strong># argv[1] The job ID
# argv[2] The user printing the job
# argv[3] The job name/title
# argv[4] The number of copies to print
# argv[5] The options that were provided when the job was submitted
# argv[6] The file to print (first program only)</strong>
DIRPATH=/usr/local/faxserver
FILESPATH=/tmp
TARGETFILE=$FILESPATH/$3
LOGFILE=/tmp/faxserver_print.log
exec 1>>$LOGFILE
exec 2>>$LOGFILE
echo "======================================================================"
echo "$1"
echo "$2"
echo "$3"
echo "$4"
echo "$5"
cat $6 > $TARGETFILE.ps
RET_STATUS=$?
if [ $RET_STATUS -ne 0 ]
then
echo "[ERR] cat ps file get error!"
exit 5 #Job failed, cancel job
fi
<strong>gs -q -r204x196 -g1728x2156 -dNOPAUSE -dBATCH -dSAFER -sDEVICE=tiffg3 -sOutputFile=$TARGETFILE.tiff $TARGETFILE.ps</strong>
RET_STATUS=$?
if [ $RET_STATUS -ne 0 ]
then
echo "[ERR] trans ps file to tiff get error!"
exit 5 #Job failed, cancel job
fi
chmod a+r $TARGETFILE.tiff
RET_STATUS=$?
if [ $RET_STATUS -ne 0 ]
then
echo "[ERR] change tiff file to all read get error!"
exit 5 #Job failed, cancel job
fi
#OPTS="$5"
#PUSH_CMD=${OPTS##*PUSH_CMD=}
#PUSH_CMD=${PUSH_CMD%% *}
#PUSH_CMD=${PUSH_CMD//#/ }
#`$PUSH_CMD`
#RET_STATUS=$?
#if [ $RET_STATUS -ne 0 ]
#then
# echo "[ERR] push tiff file get error!"
# exit 5 #Job failed, cancel job
#fi
exit 0
/etc/cups:cups服务printer配置目录
/etc/cups/ppd:相关pinter ppd文件目录
/etc/foomatic/foomatic.conf:过滤器foomatic的配置文件
/usr/share/cups/model:cups过滤器模板,在创建printer的时候可以用这下面的ppd,然后系统会帮你在/etc/cups/ppd下生成一个ppd
/usr/lib/cups/backend:打印进进一步处理的可执行文件目录
一些重要资料:
中文cups打印原理:http://www.linuxidc.com/Linux/2010-12/30698.htm
cups原理过滤器权威网站:https://wiki.archlinux.org/index.php/Cups
书籍:Unix linux system administrator hand book 第四版