使用TSC TTP-342E Pro热敏打印机Java打印

时间:2018-09-20 11:37:16

标签: java printing thermal-printer

我正在尝试使用打印机 TSC TTP-342E Pro 工业热敏打印机打印标签。但无法打印。我使用相同的程序在激光打印机 HP LaserJet M1530 中进行打印,并且工作正常。在这两种情况下,打印都是从pdf开始的。该程序正在与打印机通信,但未打印任何内容。 我已经使用了pdfbox,是否应该使用bartenter之类的东西?

在下面的程序中,该程序将从某个位置读取pdf,并与打印机进行通信以生成打印输出。

代码

public static PrintService choosePrinter(String printerName) {
        PrintService[] services = PrinterJob.lookupPrintServices(); // list of printers
        PrintService printService = null;
        for(PrintService service : services) {
            String availablePrinterName = service.getName();
            System.out.println(availablePrinterName);
            if(service.getName().contains(printerName)) {
                printService = service;
                break;
            }
        }
        return printService;
    }

    public static void main(String[] args) {
        try {

            LOGGER.info("===== PRINT JOB 2 STARTED =======");
            Properties prop = getProperty();
            String printerName = prop.getProperty("printername");
            //String fileLocation = prop.getProperty("filelocation");
            String printStatus = prop.getProperty("enableprint");
            Double height = Double.valueOf(prop.getProperty("height"));
            Double width = Double.valueOf(prop.getProperty("width"));
            Boolean enablePrintPageSetup = Boolean.valueOf(prop.getProperty("enablePrintPageSetup"));
            PDDocument pdf = PDDocument.load(new File(location));
            PrinterJob job = PrinterJob.getPrinterJob();
            Paper paper = new Paper();
            paper.setSize((width*72), (height*72)); // 1/72 inch
            paper.setImageableArea(0, 0, paper.getWidth(), paper.getHeight());
            PageFormat pageFormat = new PageFormat();
                if(enablePrintPageSetup) {
                    pageFormat.setPaper(paper);
                }
            pageFormat.setOrientation(PageFormat.PORTRAIT);
            Book book = new Book();
            PDFPrintable pdfPrintable = new PDFPrintable(pdf, Scaling.SHRINK_TO_FIT);
            book.append(pdfPrintable, pageFormat, pdf.getNumberOfPages());
            job.setPageable(book);
            PrintService printService = choosePrinter(printerName);
            if(printService != null) {
                if(Boolean.parseBoolean(printStatus)) {
                    LOGGER.info("Print enabled  ");
                     job.setPrintService(printService);
                     job.print();
                } else {
                    LOGGER.info("Print disbaled  ");
                }
            } else {
                System.out.println("No PDF printer available.");
                LOGGER.info("===== No PDF printer available. =======");
            }
            System.out.println("===== PRINT JOB 2 STOPPED =======");
            LOGGER.info("===== PRINT JOB 2 STOPPED =======");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            LOGGER.error("Error : ", e);
            e.printStackTrace();
        }
    }
    private static Properties getProperty() {
        Properties prop = new Properties();
        try {

            File jarPath=new File(App_1.class.getProtectionDomain().getCodeSource().getLocation().getPath());
            String propertiesPath=jarPath.getParentFile().getAbsolutePath();
            System.out.println(" propertiesPath-"+propertiesPath);
            prop.load(new FileInputStream(propertiesPath+"/config.properties"));
            location = propertiesPath + "/printpdf.pdf";
        } catch (Exception e) {
           e.printStackTrace(); 
           LOGGER.error("Error : ", e);
        }
        return prop;
    }

0 个答案:

没有答案