0

Good day.

I have a problem to print files with Chinese characters directly of linux.

We use CUPS to manage your printers on linux and send the print command by a2ps.

Our files are in the encode/unicode (UTF-8 and ISO-8859), but the physical printing is not seeing the Chinese characters

example:

¸£ÌØÆû³µ½ðÈÚ£¨Öйú£©ÓÐÏÞ¹«Ë¾/

Has anyone been through this and know how I can change the unicode of the a2ps command or cups to be able to convert the files?

Estevão Jordão
  • 189
  • 1
  • 5
  • 20

1 Answers1

0

As a solution, I adopted perform a conversion to pdf with the correct encode and send the converted PDF to CUPS via PrintJob.java file.

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;
}

}

Estevão Jordão
  • 189
  • 1
  • 5
  • 20