Google Cloud Print (GCP) API for Java
Document : https://developers.google.com/cloud-print/
There are two ways to do that:
- Load printer capabilities and create an XML(XPS format) file using Windows API. Ex.: Using "PrintQueue" class --> GetPrintCapabilitiesAsXml.WriteTo(file)
- Obtaining the PPD file of the PostScript printer. Uploading one of these files, GCP was able to understand the printer capabilities.
Recommended from JoĂŁo Vianey (Google Cloud Print developer) Thank you very much.
String email = "YOUR_GOOGLE_EMAIL";
String password = "YOUR_GOOGLE_PASSWORD";
GoogleCloudPrint cloudPrint = new GoogleCloudPrint();
//connect to google cloud print
cloudPrint.connect(email, password, "geniustree-cloudprint-1.0");
cloudPrint.disconnect();
// create and setup printer
RegisterPrinterResponse response = cloudPrint.registerPrinter(printer);
and
InputStream inputStream = null;
try {
//URL ppdURL = Example.class.getResource("/ppd/ADIST5CS.PPD");
URL xpsURL = Example.class.getResource("/xml/XPSCapabilities.xml");
File capabilitiesFile = new File(xpsURL.getPath());
inputStream = new FileInputStream(capabilitiesFile);
/*
* require
* - printer name
* - display name
* - proxy
* - capabilities
* - defaults
* - tag
* - status
* - description
* - capsHash
*/
Printer printer = new Printer();
printer.setName("Test Printer");
printer.setDisplayName("Test Printer");
printer.setProxy("pamarin");
Set<String> tags = new HashSet<String>();
tags.add("test");
tags.add("register");
printer.setTags(tags);
String capsHash = DigestUtils.sha512Hex(inputStream);
printer.setCapsHash(capsHash);
printer.setStatus("REGISTER");
printer.setDescription("test register printer");
printer.setCapabilities(capabilitiesFile);
printer.setDefaults(capabilitiesFile);
RegisterPrinterResponse response = cloudPrint.registerPrinter(printer);
if (!response.isSuccess()) {
LOG.debug("message = > {}", response.getMessage());
return;
}
for (Printer print : response.getPrinters()) {
LOG.debug("printer response => {}", print);
}
} catch (IOException ex) {
LOG.warn(null, ex);
} catch (CloudPrintException ex) {
LOG.warn(null, ex);
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException ex) {
LOG.warn(null, ex);
}
}
}
listener job from google talk notification
cloudPrint.addJobListener(new JobListener() {
//
@Override
public void onJobArrive(Job job, boolean success, String message) {
//do something ...
}
});
and
cloudPrint.addJobListener(new JobListener() {
//
@Override
public void onJobArrive(Job job, boolean success, String message) {
if (success) {
try {
LOG.debug("job arrive => {}", job);
File directory = new File("C:\\temp\\cloudPrint");
if (!directory.exists()) {
directory.mkdirs();
}
//download print file from google cloud print
String fileName = FilenameUtils.removeExtension(job.getTitle()) + ".pdf";
File outputFile = new File(directory, fileName);
cloudPrint.downloadFile(job.getFileUrl(), outputFile);
//do something...
//get job information or printing option such as
// - document collate
// - page output color
// - page orientation
// - page media size
// - duplex page
// - copies document
// ...
// response is XML format
String ticketResponse = cloudPrint.getJobTicket(job.getTicketUrl());
LOG.debug("ticketResponse => {}", ticketResponse);
//update job status
ControlJobResponse response = cloudPrint.controlJob(job.getId(), JobStatus.DONE, 200, "success.");
LOG.debug("control job response=> {}", response.isSuccess() + ", " + response.getMessage());
} catch (CloudPrintException ex) {
LOG.warn(null, ex);
}
} else {
LOG.info("job arrive error message => {}", message);
}
}
});
send job to google cloud print
//create and setup job
SubmitJobResponse response = cloudPrint.submitJob(submitJob);
and
//send job to target printer
//printer id = "810c1d39-981f-cd36-5fdc-951ea5e62613"
InputStream jsonInputStream = null;
InputStream imageInputStream = null;
try {
jsonInputStream = Example.class.getResourceAsStream("/json/submitJobTicketRequest.json");
imageInputStream = Example.class.getResourceAsStream("/testImage.png");
byte[] content = IOUtils.toByteArray(imageInputStream);
//get job option from json file
//such as
// - document collate
// - page output color
// - page orientation
// - page media size
// - duplex page
// - copies document
// ...
String jsonTicket = IOUtils.toString(jsonInputStream);
Ticket ticket = gson.fromJson(jsonTicket, Ticket.class);
String json = gson.toJson(ticket);
LOG.debug("json => {}", json);
//create job
SubmitJob submitJob = new SubmitJob();
submitJob.setContent(content);
submitJob.setContentType("image/png");
submitJob.setPrinterId("810c1d39-981f-cd36-5fdc-951ea5e62613"); //*****
submitJob.setTag(Arrays.asList("koalar", "hippo", "cloud"));
submitJob.setTicket(ticket);
submitJob.setTitle("testImage.png");
//send job
SubmitJobResponse response = cloudPrint.submitJob(submitJob);
LOG.debug("submit job response => {}", response.isSuccess() + "," + response.getMessage());
LOG.debug("submit job id => {}", response.getJob().getId());
//control job (update job)
//...
//...
} catch (Exception ex) {
LOG.warn(null, ex);
} finally {
if (jsonInputStream != null) {
try {
jsonInputStream.close();
} catch (IOException ex) {
LOG.warn(null, ex);
}
}
if (imageInputStream != null) {
try {
imageInputStream.close();
} catch (IOException ex) {
LOG.warn(null, ex);
}
}
}
//jobId generate by google cloud print
DeleteJobResponse response = cloudPrint.deleteJob(jobId);
LOG.debug("delete job response => {}", response.isSuccess() + ", " + response.getMessage());
//printer id = "a1dbe503-eb96-6d26-dc7b-a290a1cfaf3b"
Printer printer = new Printer();
printer.setId("a1dbe503-eb96-6d26-dc7b-a290a1cfaf3b");
printer.setName("Adobe PDF2"); //set new name
printer.setDisplayName("Adobe PDF2");
UpdatePrinterResponse response = cloudPrint.updatePrinter(printer);
LOG.debug("update printer response => {}", response.isSuccess() + ", " + response.getMessage());
//search all printers which name is "fax"
//PrinterStatus.ALL is all printer status
// - ONLINE
// - UNKNOWN
// - OFFLINE
// - DORMANT
SearchPrinterResponse response = cloudPrint.searchPrinter("fax", PrinterStatus.ALL);
if (!response.isSuccess()) {
LOG.debug("message = > {}", response.getMessage());
return;
}
for (Printer printer : response.getPrinters()) {
LOG.debug("printer => {}", printer);
}
//printer id = "dc6929f5-8fdc-5228-1e73-c9dee3298445"
PrinterInformationResponse response = cloudPrint.getPrinterInformation("dc6929f5-8fdc-5228-1e73-c9dee3298445");
if (!response.isSuccess()) {
LOG.debug("message = > {}", response.getMessage());
return;
}
for (Printer printer : response.getPrinters()) {
LOG.debug("printer information response => {}", printer);
}
//parameter
//job id : get from subscribe job (generate by google cloud print)
//job status :
// - QUEUED
// - IN_PROGRESS
// - DONE
// - ERROR
//job code : get from printer
//job message : get from printer
ControlJobResponse response = cloudPrint.controlJob(job.getId(), JobStatus.IN_PROGRESS, 100, "PROGRESSING.");
LOG.debug("control job response=> {}", response.isSuccess() + ", " + response.getMessage());
Get all jobs
JobResponse response = cloudPrint.getJobs();
if (!response.isSuccess()) {
LOG.debug("message = > {}", response.getMessage());
return;
}
for (Job job : response.getJobs()) {
LOG.debug("job response => {}", job);
}
Get job of printer
//printer id = "dc6929f5-8fdc-5228-1e73-c9dee3298445"
JobResponse response = cloudPrint.getJobOfPrinter("dc6929f5-8fdc-5228-1e73-c9dee3298445");
if (!response.isSuccess()) {
LOG.debug("message = > {}", response.getMessage());
return;
}
for (Job job : response.getJobs()) {
LOG.debug("job response => {}", job);
}
//printer id = "12280cbe-6486-2c98-c65a-22083bd18b5b"
DeletePrinterResponse response = cloudPrint.deletePrinter("12280cbe-6486-2c98-c65a-22083bd18b5b");
LOG.debug("delete printer response => {}", response.isSuccess() + ", " + response.getMessage());
//printer id = "dc6929f5-8fdc-5228-1e73-c9dee3298445"
//target email = "[email protected]"
SharePrinterResponse response = cloudPrint.sharePrinter("dc6929f5-8fdc-5228-1e73-c9dee3298445", "[email protected]");
LOG.debug("share printer message => {}", response.isSuccess() + ", " + response.getMessage());