Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Selecting Printer tray based on Doc type #24

Closed
kislaylal opened this issue Sep 23, 2019 · 3 comments
Closed

Selecting Printer tray based on Doc type #24

kislaylal opened this issue Sep 23, 2019 · 3 comments

Comments

@kislaylal
Copy link

kislaylal commented Sep 23, 2019

I am trying to use CUPS4J to help in printing the docs with a specified Tray and media type but somehow what ever I do/give params, it prints from the default Tray of the printer.
Below is the code I am using to test out the functionality, can you please help me pointing out what am I missing here.
Rest of the params are working fine and I get the desired result except the Tray.
Commented code are the different options I tried but none worked. Please help in resolving.
-----code ------

package com.test.printer;

import java.io.File;
import java.net.URL;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.cups4j.CupsClient;
import org.cups4j.CupsPrinter;
import org.cups4j.PrintJob;
import org.cups4j.PrintRequestResult;

public class CupsPrintTest {

	static Logger LOG = LogManager.getLogger(App.class);
	public enum MimeTypes {
		TEXT, PDF;
	}
	private MimeTypes mimeType;		
	public static void main(String[] args) {					
		CupsPrintTest app = new CupsPrintTest();			
		app.printThruPenndotCups("PR3311");
	}
	@SuppressWarnings("unused")
	private boolean printThruPenndotCups(String printerName) {
		try {				
			String strCupsURI = "http://localhost:631/printers/"; /*Local CUPS Service running on Docker*/
			URL cupsURI = new URL(strCupsURI);				

// File file = new File("FILE-PATH\ABCD.pdf"); // PDF file path
byte[] byteArray = Files.readAllBytes(file.toPath());
int copies = 1;
CupsClient cupsClient;
cupsClient = new CupsClient(cupsURI.getHost(), cupsURI.getPort(), "user");
printerName = "PR3311";//@network printer
String strPrinterURL = strCupsURI + printerName;
URL printerURL = new URL(strPrinterURL);
CupsPrinter cupsPrinter = cupsClient.getPrinter(printerURL);
boolean isDuplex = true;
// String tray = "upper";
// String tray = "middle";
// String tray = "lower";
// String tray = "Tray 1";
// String tray = "Tray 2";
// String tray = "Tray2";
String tray = "na_legal_8.5x14in";
// String tray = "legal";
mimeType = MimeTypes.PDF;
if (mimeType == MimeTypes.TEXT) {
cupsPrinter.setMimeTypesSupported(Arrays.asList("text/plain"));
isDuplex = false;
cupsPrinter.setMediaDefault(tray);
} else {
cupsPrinter.setMimeTypesSupported(Arrays.asList("application/vnd.cups-ppd"));
cupsPrinter.setMediaDefault(tray);
cupsPrinter.setMediaSupported(Arrays.asList(tray));
isDuplex = true;
}
Map<String, String> attributes = new HashMap<>();
// attributes.put("compression", "none");
// attributes.put("job-attributes", "print-quality:enum:9#fit-to-page:boolean:false#sheet-collate:keyword:collated");
// attributes.put("job-attributes", "media:keyword:bottom#media-default:keyword:bottom#media-supported:keyword:bottom#media-ready:keyword:bottom");
// attributes.put("job-attributes", tray);
// attributes.put("job-attributes", "media:bottom");
attributes.put("job-attributes", "media-source:keyword:Tray-2");
PrintJob printJob = new PrintJob.Builder(byteArray)
.userName("loggedInUser")
.jobName("JobName")
.duplex(isDuplex)
.copies(copies)
// .pageFormat(tray)
.attributes(attributes)
.build();
PrintRequestResult printRequestResult = null;
printRequestResult = cupsPrinter.print(printJob);
if (printRequestResult != null)
System.out.println(" Done with printing with ID = "+ printRequestResult.getJobId());
else
System.out.println("Did NOT print");
return true;

		} catch (Exception e) {
			System.out.println("In exception block" + e);
		}
		return false;
	}	

}
-------code ends ----------

@kislaylal
Copy link
Author

kislaylal commented Sep 25, 2019

I found a workaroud ... the printer that we are using is Lexmark brand and using the Lexmark's driver I am able to create 2 printer configuration in the CUPS server, 1 each for individual tray as default Tray (like PrinterX-T1 & PrinterX-T2) and then sending the printing command to each depending upon the need. This is clumsy I agree, but it works :)
My quest is to make it work cleaner with less management, please help!

@in-my-heart
Copy link

Can you print docs files normally, why can I only print pdf, img

@harwey
Copy link
Owner

harwey commented Dec 14, 2019

CUPS is the linux printing system that delivers many drivers to print different file types. I never heard of a print driver for CUPS that is able to print microsoft documents.
Cups4J just helps sending print jobs to CUPS from java. So if you can find a solution for your problem, please come back and report it here.

@harwey harwey closed this as completed Oct 16, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants