美好的一天。
我在直接用linux打印带有中文字符的文件时遇到问题。
我们使用CUPS在Linux上管理您的打印机,并通过a2ps发送打印命令。
我们的文件位于编码/ 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 - Command a2ps和Cups-汉字,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24555715/