Linux Centos 7 实现java控制打印机打印文件
设备:Hp LaserJet Pro MFP M126nw 打印机
操作系统: Centos7
yum源:163Centos7
cups版本: cups-1.6.3-51.el7.x86_64
hplip版本: 3.22.6
jdk:1.8
其余依赖: gcc、GUI库
操作步骤:
0.安装Centos的GUI
https://blog.csdn.net/babytiger/article/details/109563376 参考这篇文章
1.替换yum源并更新库,具体操作可以百度
2.yum install cups
3.systemctl start cups
前往
https://sourceforge.net/projects/hplip/files/hplip/3.22.6/
下载相应的tar.gz包,上传到服务器解压
tar -xjvf hplip-3.22.6.tar.gz
之后在解压后的路径/ppd/hpcpus 复制到 /usr/lib/cups/filter/下即可
也可用命令
find / -name hpcups*
查找
CUPS给Unix/Linux用户提供了一种可靠有效的方法来管理打印。它支持IPP,并提供了LPD,SMB(服务消息块,如配置为微软WINDOWS的打印机)、JetDirect等接口。CUPS还可以浏览网络打印机。
执行上述命令后,可以在浏览器打开 http://localhost:631用来配置网络打印机
4.安装 hplip依赖
yum install hplip-* -y
yum install gcc
yum install libusb yum install libusb-devel(尤其是提示缺少libusb库的时候)
5.安装hplip
从https://sourceforge.net/projects/hplip/files/hplip/ 下载相应版本的 hplip.run文件 ,,run文件是一种自动安装的文件,相较于编译安装更加便捷,推荐3.22.6版本,3.17.11之前使用的是plug插件来作为驱动,之后的使用了hpcpus最为驱动链接
执行
sh hplip-3.22.6.run
一路选择分别是
y y a y y y y u y d(由于网络原因大概率下载不到最新的驱动,等待一会即可) y i u
接下来是重点部分
第三个红框输入 惠普打印机的 ip
之后点击下一步
这里可以i输入打印机的名称以及相应的描述信息
之后选择 Add Printer 后,添加完成
前往http://localhost:631页面
我因为已经装过几次了,所以不需要再注册账户了,否则的话第一次安装cups需要 注册相应的账号
在printers一栏会显示刚刚安装上的打印机,如果status一栏显示 idle不显示任何驱动则表明目前为止安装成功
如果显示
/usr/lib/cups/filter/hpcpus 不存在的话则,前往
https://sourceforge.net/projects/hplip/files/hplip/3.22.6/
下载相应的tar.gz包,上传到服务器解压
tar -xjvf hplip-3.22.6.tar.gz
之后在解压后的路径/ppd/hpcpus 复制到 /usr/lib/cups/filter/下即可
也可用命令 查找
find / -name hpcups*
之后选择
双击
打印测试也,如无意外可正常打印,至此Centos7链接打印机成功
下方附带Java的测试代码
package com.datawells.utils;
import lombok.SneakyThrows;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.printing.PDFPrintable;
import org.apache.pdfbox.printing.Scaling;
import javax.print.*;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.Copies;
import javax.print.attribute.standard.Sides;
import java.awt.print.Book;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.PrinterJob;
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
public class PrintUtils {
public static void main(String[] args) throws Exception {
String pdfFile = "E:\\a.pdf";//文件路径
// File file = new File(pdfFile);
System.out.println(findPrints().toString());
URL url = new URL("url");
File file = urlToFile(url);
String printerName ="HP_LaserJet_Pro_MFP_M126nw";
// "HP LaserJet Pro MFP M126nw";//打印机名包含字串
//HP_LaserJet_Pro_MFP_M126nw
PDFprint(file,printerName);
}
@SneakyThrows
public static void test(){
String pdfFile = "E:\\a.pdf";//文件路径
// File file = new File(pdfFile);
System.out.println(findPrints().toString());
URL url = new URL("url");
File file = urlToFile(url);
String printerName = "1";//打印机名包含字串
System.out.println("进入打印方法");
PDFprint(file,printerName);
}
public static List<String> findPrints() {
List<String> list = new ArrayList<>();
HashPrintRequestAttributeSet requestAttributeSet = new HashPrintRequestAttributeSet();
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
//查找所有的可用的打印服务
System.out.println("查找所有的可用的打印服务");
PrintService[] printService = PrintServiceLookup.lookupPrintServices(flavor, requestAttributeSet);
if (printService == null || printService.length == 0) {
System.out.println("未找到可用的打印机列表");
}
if (printService != null) {
for (PrintService print : printService) {
list.add(print.getName());
}
}
return list;
}
public static File urlToFile(URL url) {
InputStream is = null;
File file = null;
FileOutputStream fos = null;
try {
file = File.createTempFile("tmp", null);
URLConnection urlConn = null;
urlConn = url.openConnection();
is = urlConn.getInputStream();
fos = new FileOutputStream(file);
byte[] buffer = new byte[4096];
int length;
while ((length = is.read(buffer)) > 0) {
fos.write(buffer, 0, length);
}
return file;
} catch (IOException e) {
return null;
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
}
}
}
}
public static void PDFprint(File file ,String printerName) throws Exception {
PDDocument document = null;
try {
document = PDDocument.load(file);
PrinterJob printJob = PrinterJob.getPrinterJob();
printJob.setJobName(file.getName());
if (printerName != null) {
// 查找并设置打印机
//获得本台电脑连接的所有打印机
System.out.println("获得本台电脑连接的所有打印机");
PrintService[] printServices = PrinterJob.lookupPrintServices();
if(printServices == null || printServices.length == 0) {
System.out.print("打印失败,未找到可用打印机,请检查。");
return ;
}
PrintService printService = null;
//匹配指定打印机
for (int i = 0;i < printServices.length; i++) {
System.out.println(printServices[i].getName());
if (printServices[i].getName().contains(printerName)) {
printService = printServices[i];
break;
}
}
System.out.println("匹配指定打印机");
if(printService!=null){
printJob.setPrintService(printService);
}else{
System.out.print("打印失败,未找到名称为" + printerName + "的打印机,请检查。");
return ;
}
}
//设置纸张及缩放
PDFPrintable pdfPrintable = new PDFPrintable(document, Scaling.ACTUAL_SIZE);
//设置多页打印
Book book = new Book();
PageFormat pageFormat = new PageFormat();
//设置打印方向
pageFormat.setOrientation(PageFormat.PORTRAIT);//纵向
pageFormat.setPaper(getPaper());//设置纸张
book.append(pdfPrintable, pageFormat, document.getNumberOfPages());
printJob.setPageable(book);
printJob.setCopies(1);//设置打印份数
//添加打印属性
HashPrintRequestAttributeSet pars = new HashPrintRequestAttributeSet();
pars.add(Sides.DUPLEX); //设置单双页
printJob.print(pars);
}finally {
if (document != null) {
try {
document.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static Paper getPaper() {
Paper paper = new Paper();
// 默认为A4纸张,对应像素宽和高分别为 595, 842
int width = 595;
int height = 842;
// 设置边距,单位是像素,10mm边距,对应 28px
int marginLeft = 10;
int marginRight = 0;
int marginTop = 10;
int marginBottom = 0;
paper.setSize(width, height);
// 下面一行代码,解决了打印内容为空的问题
paper.setImageableArea(marginLeft, marginRight, width - (marginLeft + marginRight), height - (marginTop + marginBottom));
return paper;
}
// 传入文件和打印机名称
public static void JPGPrint(File file, String printerName) throws PrintException {
if (file == null) {
System.err.println("缺少打印文件");
}
InputStream fis = null;
try {
// 设置打印格式,如果未确定类型,可选择autosense
DocFlavor flavor = DocFlavor.INPUT_STREAM.JPEG;
// 设置打印参数
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
aset.add(new Copies(1)); //份数
//aset.add(MediaSize.ISO.A4); //纸张
// aset.add(Finishings.STAPLE);//装订
aset.add(Sides.DUPLEX);//单双面
// 定位打印服务
PrintService printService = null;
if (printerName != null) {
//获得本台电脑连接的所有打印机
PrintService[] printServices = PrinterJob.lookupPrintServices();
if (printServices == null || printServices.length == 0) {
System.out.print("打印失败,未找到可用打印机,请检查。");
return;
}
//匹配指定打印机
for (int i = 0; i < printServices.length; i++) {
System.out.println(printServices[i].getName());
if (printServices[i].getName().contains(printerName)) {
printService = printServices[i];
break;
}
}
if (printService == null) {
System.out.print("打印失败,未找到名称为" + printerName + "的打印机,请检查。");
return;
}
}
fis = new FileInputStream(file); // 构造待打印的文件流
Doc doc = new SimpleDoc(fis, flavor, null);
DocPrintJob job = printService.createPrintJob(); // 创建打印作业
job.print(doc, aset);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} finally {
// 关闭打印的文件流
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}