linux - 命令 a2ps 和 Cups - 汉字

标签 linux cups

美好的一天。

我在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/

相关文章:

linux - 由于 “error: cannot compute suffix of object files”,从bash脚本运行./configure失败

printing - CUPS:如何修复点阵打印机的右边距

linux - 如何在 Lua 和 cups 中使用打印机

regex - grep 命令模式中插入符号的不同位置?

linux - 从字符串中提取文件路径

linux - 如何从管道结果中提取文件名?

linux - systemd - 如何从系统服务访问当前用户名?

java - 使用JAVA打印 "Bixolon Thermal Printer", "No pages found!"错误

linux - CUPS Linux : Help printing these media types: MS Excel, MS Word 和 HTML

Python 数组按单词过滤