Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
557 views
in Technique[技术] by (71.8m points)

spring - Java send pdf to Printer by camel-print LPR

I'm trying to send a PDF to a printer since my microservice-spring5 on Docker.

I found Apache Camel Printer to do this. if you have any others suggestions please comment.

https://camel.apache.org/components/latest/lpr-component.html

So.. I do a telnet to validate that the port 515 is OK for lpd/lpr

I try to send a PDF(InputStream) directly to the printer by lpr

public void sendToPrinter(InputStream is) {
  String sendTo = "lpr://myIpAddress/myPrinterName?flavor=DocFlavor.INPUT_STREAM&mimeType=PDF";
  try {
    ModelCamelContext context = new DefaultCamelContext();
    context.addRoutes(new RouteBuilder() {
      public void configure() {
        from("direct:start").to(sendTo);
      }
    });
    context.start(); // **exception throw here** 
    
    ProducerTemplate template = context.createProducerTemplate();
    template.start();
    template.send("direct:start", new Processor() {
      @Override
      public void process(Exchange exchange) throws Exception {
        byte[] buffer = new byte[is.available()];
        int n = is.available();
        for (int i = 0; i < n; i++) {
          buffer[i] = (byte) is.read();
        }
        is.close();
        exchange.setProperty(Exchange.CHARSET_NAME, "UTF-8");
        Message in = exchange.getIn();
        in.setBody(buffer);
      }
    }
}

Exception: javax.print.PrintException: No printer found with name: myIpAddresse/myPrinterName. Please verify that the host and printer are registered and reachable from this machine.

I can't setup a server CUPS in my docker image linux I have to address directly the printer server by code. Any idea ?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...