gpt4 book ai didi

linux - 命令 a2ps 和 Cups - 汉字

转载 作者:太空宇宙 更新时间:2023-11-04 03:56:23 27 4
gpt4 key购买 nike

美好的一天。

我在linux下直接打印带有汉字的文件时遇到问题。

我们使用CUPS来管理Linux上的打印机并通过a2ps发送打印命令。

我们的文件采用encode/unicode(UTF-8和ISO-8859),但物理打印看不到汉字

示例:

有人经历过这个并知道如何更改 a2ps 命令或 cups 的 unicode 以便能够转换文件吗?

最佳答案

作为解决方案,我采用了使用正确的编码执行 PDF 转换,并通过 PrintJob.java 文件将转换后的 PDF 发送到 CUPS。

public class PDFPrintService {

/**
* Printer Job.
*/
private PrinterJob printerJob = null;

/**
* File to printer.
*/
private InputStream file;

/**
* Class that represents the file to be printed.
*/
private PDFFilePrint pdfFilePrint;

/**
* File converted to PDF for printed.
*/
private PDFFile pdfFile;

/**
* Temporary directory used in the conversion to postscript used in prints.
*/
private String temporaryDirectoryPostscriptFiles;

/**
* Default Temporary Directory of Files.
*/
private String defaultTemporaryDirectoryFiles;

/**
* java.io.tmpdir
*/
private static final String JAVA_IO_TEMPDIR = "java.io.tmpdir";

/**
* Constructs the print job based on the PDFFilePrint class.
*
* @param inputStream
* @param jobName
* @throws IOException
* @throws PrinterException
*/
public PDFPrintService(PDFFilePrint pdfFilePrint) throws IOException, PrinterException {
this.pdfFilePrint = pdfFilePrint;
loadFile(pdfFilePrint.getFileName());
byte[] pdfContent = new byte[this.file.available()];
this.file.read(pdfContent, 0, this.file.available());
initialize(pdfContent, this.pdfFilePrint.getJobName());
}

/**
* Method responsible to load the file for print.
*
* @param fileName
* @throws FileNotFoundException
*/
private void loadFile(final String fileName) throws FileNotFoundException{
try {
this.file = new FileInputStream(fileName);
} catch (FileNotFoundException e) {
e.printStackTrace();
throw new FileNotFoundException("The File : " + fileName);
}
}

/**
* Initializes the job
*
* @param pdfContent
* @param jobName
* @throws IOException
* @throws PrinterException
*/
private void initialize(byte[] pdfContent, String jobName)
throws IOException, PrinterException {
ByteBuffer bb = ByteBuffer.wrap(pdfContent);

this.pdfFile = new PDFFile(bb);
PDFPrintPage pages = new PDFPrintPage(pdfFile);

this.printerJob = PrinterJob.getPrinterJob();

loadPrinterDestination();
PageFormat pageFormat = PrinterJob.getPrinterJob().defaultPage();

Book book = new Book();
book.append(pages, pageFormat, pdfFile.getNumPages());

printerJob.setPageable(book);
printerJob.setJobName(jobName);

Paper paper = new Paper();
paper.setImageableArea(0, 0, paper.getWidth(), paper.getHeight());
pageFormat.setPaper(paper);
}

/**
* Method responsible to get the printer.
*
* @throws PrinterException
*/
private void loadPrinterDestination() throws PrinterException {
String printerName = new String();
try {
PrintService[] services = PrinterJob.lookupPrintServices();
for (PrintService printService : services) {
printerName = printService.getName();
if (printerName.equalsIgnoreCase(this.pdfFilePrint.getPrinterName())) {
printerJob.setPrintService(printService);
break;
}
}
} catch (PrinterException e) {
e.printStackTrace();
throw new PrinterException("Printer not found : printerName " + printerName);
}
}

/**
* Method responsible to printer.
*
* @throws PrinterException
*/
public void print() throws PrinterException {
try {
loadNewTemporaryDirectoryPostscriptFiles();
this.printerJob.print(getPrinterPageSettings());
} finally {
loadDefaultTemporaryDirectoryPostscriptFiles();
closeFile();
}
}

/**
* Method responsible to load a new area for a temporary converted files in
* server.
*/
private void loadNewTemporaryDirectoryPostscriptFiles() {
this.defaultTemporaryDirectoryFiles = System.getProperty(JAVA_IO_TEMPDIR, null);
if(!temporaryDirectoryPostscriptFiles.trim().isEmpty()){
System.setProperty(JAVA_IO_TEMPDIR, temporaryDirectoryPostscriptFiles);
}
}

/**
* /**
* Method responsible to load a default temporary area of files.
*/
private void loadDefaultTemporaryDirectoryPostscriptFiles() {
String currentDirectoryUsed = System.getProperty(JAVA_IO_TEMPDIR, null);
if(!currentDirectoryUsed.equalsIgnoreCase(defaultTemporaryDirectoryFiles)){
System.setProperty(JAVA_IO_TEMPDIR, defaultTemporaryDirectoryFiles);
}
}

/**
* Method responsible to load settings of printer.
*
* @return PrintRequestAttributeSet
*/
private PrintRequestAttributeSet getPrinterPageSettings() {
PrintRequestAttributeSet printRequestAttribute = new HashPrintRequestAttributeSet();
loadPageRange(printRequestAttribute);
loadSide(printRequestAttribute);
loadOrientationPortrait(printRequestAttribute);
printRequestAttribute.add(NORMAL);
return printRequestAttribute;
}

/**
* Method responsible to close the file after the printer.
*/
private void closeFile() {
try {
this.file.close();
} catch (IOException e) {
e.printStackTrace();
}
}

/**
* Method responsible to load the orientation Portrait.
*
* @param printRequestAttribute
*/
private void loadOrientationPortrait(PrintRequestAttributeSet printRequestAttribute) {
printRequestAttribute.add(PORTRAIT);
}

/**
* Method responsible to load the Side of printer(ONE_SIDED or DUPLEX).
*
* @param printRequestAttribute
*/
private void loadSide(PrintRequestAttributeSet printRequestAttribute) {
if (!this.pdfFilePrint.isDuplexSidet()) {
printRequestAttribute.add(ONE_SIDED);
} else {
printRequestAttribute.add(DUPLEX);
}
}

/**
* Method responsible to load the page range of print.
*
* @param printRequestAttribute
*/
private void loadPageRange(PrintRequestAttributeSet printRequestAttribute) {
int lowerBound = pdfFilePrint.getLowerBound();
int upperBound = pdfFilePrint.getUpperBound();

if ((lowerBound < 1) && (upperBound < 1)) {
lowerBound = 1;
upperBound = pdfFile.getNumPages();
} else {
if ((lowerBound < 1) && (upperBound > 0)) {
lowerBound = 1;
} else {
if ((lowerBound > 0) && (upperBound < 1)) {
upperBound = pdfFile.getNumPages();
}
}
}

if (upperBound < lowerBound) {
upperBound = lowerBound;
}

if (lowerBound > pdfFile.getNumPages()) {
lowerBound = pdfFile.getNumPages();
}

if (upperBound > pdfFile.getNumPages()) {
upperBound = pdfFile.getNumPages();
}

PageRanges pageRanges = new PageRanges(lowerBound, upperBound);
printRequestAttribute.add(pageRanges);
}

/**
* Set temporaryDirectoryPostscriptFiles.
*
* @param temporaryDirectoryPostscriptFiles
*/
public void setTemporaryDirectoryPostscriptFiles(
String temporaryDirectoryPostscriptFiles) {
this.temporaryDirectoryPostscriptFiles = temporaryDirectoryPostscriptFiles;
}

}

关于linux - 命令 a2ps 和 Cups - 汉字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24555715/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com