fix(printer): 打印参数调整
支持封面彩色打印,内容灰色打印
This commit is contained in:
parent
0724f442e6
commit
5978de5534
@ -50,6 +50,7 @@ public class PrintServerConfig {
|
|||||||
// 确定配置文件路径
|
// 确定配置文件路径
|
||||||
String userHome = System.getProperty("user.home");
|
String userHome = System.getProperty("user.home");
|
||||||
configFile = new File(userHome + File.separator + ".goeing" + File.separator + CONFIG_FILE);
|
configFile = new File(userHome + File.separator + ".goeing" + File.separator + CONFIG_FILE);
|
||||||
|
// System.out.println(configFile.getAbsolutePath());
|
||||||
|
|
||||||
// 确保目录存在
|
// 确保目录存在
|
||||||
if (!configFile.getParentFile().exists()) {
|
if (!configFile.getParentFile().exists()) {
|
||||||
|
|||||||
@ -13,12 +13,12 @@ public class PrintOption {
|
|||||||
private String size;
|
private String size;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 打印颜色模式,如"Full Color", "Monochrome"等
|
* 打印颜色模式,如"Color", "Monochrome"等
|
||||||
*/
|
*/
|
||||||
private String color;
|
private String color;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 打印面设置,如"One-Sided", "Double-Sided"等
|
* 打印面设置,如"Single-Sided", "Double-Sided"等
|
||||||
*/
|
*/
|
||||||
private String sides;
|
private String sides;
|
||||||
|
|
||||||
|
|||||||
@ -3,6 +3,7 @@ package com.goeing.printserver.main.utils;// src/main/java/com/example/printer/P
|
|||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
import com.goeing.printserver.main.domain.bo.PrintOption;
|
import com.goeing.printserver.main.domain.bo.PrintOption;
|
||||||
import org.apache.pdfbox.pdmodel.PDDocument;
|
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||||
|
import org.apache.pdfbox.pdmodel.PDPage;
|
||||||
import org.apache.pdfbox.printing.PDFPageable;
|
import org.apache.pdfbox.printing.PDFPageable;
|
||||||
import org.apache.pdfbox.printing.Orientation;
|
import org.apache.pdfbox.printing.Orientation;
|
||||||
import org.apache.pdfbox.printing.PDFPrintable;
|
import org.apache.pdfbox.printing.PDFPrintable;
|
||||||
@ -68,13 +69,43 @@ public class PdfPrinter {
|
|||||||
public static void print(String pdfPath, String printerName, PrintOption option) throws Exception {
|
public static void print(String pdfPath, String printerName, PrintOption option) throws Exception {
|
||||||
// 参数验证
|
// 参数验证
|
||||||
validatePrintParameters(pdfPath, printerName, option);
|
validatePrintParameters(pdfPath, printerName, option);
|
||||||
|
PrinterJob job = getPrintServiceByName(printerName);
|
||||||
// 加载PDF文档并执行打印
|
// 加载PDF文档并执行打印
|
||||||
PDDocument document = null;
|
PDDocument document = null;
|
||||||
try {
|
try {
|
||||||
document = PDDocument.load(new File(pdfPath));
|
document = PDDocument.load(new File(pdfPath));
|
||||||
PrinterJob job = getPrintServiceByName(printerName);
|
|
||||||
setPageStyle(document, job, option);
|
String color = option.getColor();
|
||||||
|
//如果是封面彩打 那么要把封面和 内容分开打印 就做两个打印任务 先打封面 再打内容
|
||||||
|
if (color.equalsIgnoreCase("Cover Letter Color Only")){
|
||||||
|
|
||||||
|
|
||||||
|
PDDocument cover = new PDDocument();
|
||||||
|
cover.addPage(document.getPage(0));
|
||||||
|
cover.close();
|
||||||
|
|
||||||
|
PDDocument content = new PDDocument();
|
||||||
|
for (int i = 0; i < document.getNumberOfPages(); i++) {
|
||||||
|
//第一页是封面
|
||||||
|
if (i==0){
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
content.addPage(document.getPage(i));
|
||||||
|
}
|
||||||
|
content.close();
|
||||||
|
//打印 封面和内容
|
||||||
|
option.setColor("color");
|
||||||
|
setPageStyle(cover, job, option);
|
||||||
|
option.setColor("black & white");
|
||||||
|
setPageStyle(content, job, option);
|
||||||
|
|
||||||
|
}else {
|
||||||
|
//全部打印
|
||||||
|
setPageStyle(document, job, option);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
} finally {
|
} finally {
|
||||||
// 确保文档被关闭
|
// 确保文档被关闭
|
||||||
if (document != null) {
|
if (document != null) {
|
||||||
@ -182,12 +213,8 @@ public class PdfPrinter {
|
|||||||
// 设置页面方向
|
// 设置页面方向
|
||||||
Orientation pdfOrientation = getPdfOrientation(option.getOrientation());
|
Orientation pdfOrientation = getPdfOrientation(option.getOrientation());
|
||||||
|
|
||||||
// 获取纸张尺寸
|
// 设置纸张 A4 Letter等
|
||||||
String size = option.getSize() != null ? option.getSize() : "Letter";
|
Paper paper = createPaperFromOption(option);
|
||||||
double[] dimensions = getPaperDimensions(size);
|
|
||||||
|
|
||||||
// 创建自定义Paper对象
|
|
||||||
Paper paper = createPaper(dimensions[0], dimensions[1], option.getMargin());
|
|
||||||
|
|
||||||
// 创建PageFormat对象
|
// 创建PageFormat对象
|
||||||
PageFormat pageFormat = new PageFormat();
|
PageFormat pageFormat = new PageFormat();
|
||||||
@ -204,7 +231,6 @@ public class PdfPrinter {
|
|||||||
Book book = new Book();
|
Book book = new Book();
|
||||||
|
|
||||||
// 将所有页面添加到Book中,使用相同的PageFormat
|
// 将所有页面添加到Book中,使用相同的PageFormat
|
||||||
// 根据页面方向选择适当的缩放模式
|
|
||||||
Scaling scaling;
|
Scaling scaling;
|
||||||
if (option.getSize() != null) {
|
if (option.getSize() != null) {
|
||||||
// 如果用户指定了纸张大小,使用适应页面的缩放模式
|
// 如果用户指定了纸张大小,使用适应页面的缩放模式
|
||||||
@ -215,7 +241,7 @@ public class PdfPrinter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 创建PDFPrintable对象,设置居中和显示页面边框
|
// 创建PDFPrintable对象,设置居中和显示页面边框
|
||||||
PDFPrintable printable = new PDFPrintable(document, scaling, false, 0, true);
|
PDFPrintable printable = new PDFPrintable(document, scaling, false, 300, true);
|
||||||
book.append(printable, pageFormat, document.getNumberOfPages());
|
book.append(printable, pageFormat, document.getNumberOfPages());
|
||||||
|
|
||||||
// 应用自定义页面设置到打印作业
|
// 应用自定义页面设置到打印作业
|
||||||
@ -234,8 +260,7 @@ public class PdfPrinter {
|
|||||||
*/
|
*/
|
||||||
public static Paper createPaperFromOption(PrintOption option) {
|
public static Paper createPaperFromOption(PrintOption option) {
|
||||||
// 获取纸张尺寸
|
// 获取纸张尺寸
|
||||||
String size = option.getSize() != null ? option.getSize() : "Letter";
|
double[] dimensions = getPaperDimensions(option.getSize());
|
||||||
double[] dimensions = getPaperDimensions(size);
|
|
||||||
|
|
||||||
// 获取边距(默认为0.5英寸)
|
// 获取边距(默认为0.5英寸)
|
||||||
double marginInches = option.getMargin();
|
double marginInches = option.getMargin();
|
||||||
@ -252,11 +277,7 @@ public class PdfPrinter {
|
|||||||
*/
|
*/
|
||||||
private static PrintRequestAttributeSet createPrintRequestAttributeSet(PrintOption option) {
|
private static PrintRequestAttributeSet createPrintRequestAttributeSet(PrintOption option) {
|
||||||
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
|
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
|
||||||
|
|
||||||
// 设置纸张大小
|
|
||||||
// String size = option.getSize() != null ? option.getSize() : "Letter";
|
|
||||||
// aset.add(PAPER_SIZES.getOrDefault(size, MediaSizeName.NA_LETTER));
|
|
||||||
|
|
||||||
// 设置颜色模式
|
// 设置颜色模式
|
||||||
setColorMode(aset, option.getColor());
|
setColorMode(aset, option.getColor());
|
||||||
|
|
||||||
@ -300,7 +321,7 @@ public class PdfPrinter {
|
|||||||
|
|
||||||
String c = color.trim().toLowerCase();
|
String c = color.trim().toLowerCase();
|
||||||
|
|
||||||
if (c.equals("color") || c.equals("full color") || c.equals("cover letter color only")) {
|
if (c.equals("color")) {
|
||||||
aset.add(Chromaticity.COLOR);
|
aset.add(Chromaticity.COLOR);
|
||||||
} else {
|
} else {
|
||||||
aset.add(Chromaticity.MONOCHROME);
|
aset.add(Chromaticity.MONOCHROME);
|
||||||
@ -314,7 +335,7 @@ public class PdfPrinter {
|
|||||||
* @param sides 打印面字符串
|
* @param sides 打印面字符串
|
||||||
*/
|
*/
|
||||||
private static void setPrintSides(PrintRequestAttributeSet aset, String sides) {
|
private static void setPrintSides(PrintRequestAttributeSet aset, String sides) {
|
||||||
if (sides != null && ("Double-Sided".equalsIgnoreCase(sides) || "Two-Sided".equalsIgnoreCase(sides))) {
|
if (("Double-Sided".equalsIgnoreCase(sides))) {
|
||||||
aset.add(Sides.TWO_SIDED_LONG_EDGE);
|
aset.add(Sides.TWO_SIDED_LONG_EDGE);
|
||||||
} else {
|
} else {
|
||||||
aset.add(Sides.ONE_SIDED);
|
aset.add(Sides.ONE_SIDED);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user