Comments (6)
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.
+1
from cups4j.
I just released this version to maven central.
<dependency> <groupId>org.cups4j</groupId> <artifactId>cups4j</artifactId> <version>0.7.0</version> </dependency>
from cups4j.
congratulation!
from cups4j.
any example how to geeting started with this lib ?
cc @harwey
from cups4j.
I just uploaded a demo server implementation using Cups4J here:
https://github.com/harwey/cups4jserver/blob/master/README.md
from cups4j.
Related Issues (20)
- Print Word (.docx) files using cups4J HOT 1
- Add a new printer to cups HOT 1
- ipps protocol support in ipp operation HOT 6
- Runtime dependencies 0.7.6 updating to 0.7.8 HOT 10
- cannot set resolution & metadata (user & jobname) not set HOT 5
- Publish new version with latest in master HOT 2
- CupsGetPrintersOperation make-and-model requested but not captured
- cups printer getPrinterState() returns 'null' HOT 4
- Inability to use secure print feature after setting job-password attribute
- Make getPrinters return CupsPrinter objects with a correct isDefault() return value
- May I ask a question about number-up-layout?
- Override duplex settings HOT 4
- who published 0.7.9? HOT 6
- getState() returning null
- getPrinters() returns no printers when I use 192.168.0.213 instead of 127.0.0.1 HOT 4
- In java cups library want to fetch all the jobs attributes of given print job with the completion time? HOT 1
- Unable to add the ipp network printer to cup server HOT 1
- how can i get media source info【tray】 HOT 2
- Cups4j sometimes does not connect correctly
- Compatibility with cups 1.6.3 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cups4j.