Giter VIP home page Giter VIP logo

Comments (6)

kentoj avatar kentoj commented on September 24, 2024 1

Here is a basic example:

First add this to your build file (gradle)

// https://mvnrepository.com/artifact/org.cups4j/cups4j
compile group: 'org.cups4j', name: 'cups4j', version: '0.7.1'

Then use the library in a class

package com.example.printing;

import com.example.printing.exception.PrinterNotFoundException;
import lombok.extern.slf4j.Slf4j;
import org.cups4j.CupsClient;
import org.cups4j.CupsPrinter;
import org.cups4j.PrintJob;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import javax.print.PrintException;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * This contains the logic for interacting with the CUPS Print Servers and their associated print queues.
 */
@Slf4j
@Component
public final class CupsCheckPrinter {

    public CupsCheckPrinter() {
    }

    @Override
    public String print(byte[] bytes, PrinterDTO printer) throws PrintException {
        log.info("Printing Document on host {} in queue {} ", printer.getCupsServerDnsName(), printer.getQueueName());

        InputStream myInputStream = new ByteArrayInputStream(bytes);
        URL printerUrl;
        String printerUrlString = "http://" + printer.getCupsServerDnsName() + ":" + printer.getCupsServerPort() + "/printers/" + printer.getQueueName();
        CupsPrinter cupsPrinter;

        try {
            log.info("PRINTER URL STRING: " + printerUrlString);
            printerUrl = new URL(printerUrlString);
            PrintJob.Builder pdfDocumentBuilder = new PrintJob.Builder(myInputStream);
            pdfDocumentBuilder.copies(1);
            PrintJob pdfPrintJob = pdfDocumentBuilder.build();
            log.info("Before cups Client");
            CupsClient cupsClient = new CupsClient(printer.getCupsServerDnsName(), printer.getCupsServerPort());
            log.info("After cups Client Url is :" + printerUrl);
            cupsPrinter = cupsClient.getPrinter(printerUrl);
            log.info("Cups Printer exists: " + cupsPrinter.getName());
            if (cupsPrinter == null)
                throw new PrinterNotFoundException(String.format("Could not find printer %s", printer.getQueueName()));
            cupsPrinter.print(pdfPrintJob);
        } catch (MalformedURLException ie) {
            throw new PrintException(String.format("Could not access the specified printer %s. Exception Message: %s.", printerUrlString, ie.getMessage()));
        } catch (Exception e) {
            throw new PrintException(String.format("CUPS Client had an error: %s", e.getMessage()));
        }
        log.info("printer name: " + cupsPrinter.getName() + "Location: " + cupsPrinter.getLocation() + ".");
        return cupsPrinter.getName();
    }
}


// Printer DTO
@Builder
@Data
public final class PrinterDTO {
    private final String cupsServerDnsName;
    private final Integer cupsServerPort;
    private final String queueName;
    private final String queueDescription;
}

// Exception
@ResponseStatus(HttpStatus.NOT_FOUND)
public final class PrinterNotFoundException extends RuntimeException {
    public PrinterNotFoundException(String s) {
        super(s);
    }
}

from cups4j.

oboehm avatar oboehm commented on September 24, 2024

+1

from cups4j.

harwey avatar harwey commented on September 24, 2024

I just released this version to maven central.

<dependency> <groupId>org.cups4j</groupId> <artifactId>cups4j</artifactId> <version>0.7.0</version> </dependency>

from cups4j.

oboehm avatar oboehm commented on September 24, 2024

congratulation!

from cups4j.

elvisbegovic avatar elvisbegovic commented on September 24, 2024

any example how to geeting started with this lib ?

cc @harwey

from cups4j.

harwey avatar harwey commented on September 24, 2024

I just uploaded a demo server implementation using Cups4J here:

https://github.com/harwey/cups4jserver/blob/master/README.md

from cups4j.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.