Reputation: 63
public static void sendPdfToPrinter(String epsilon)
{
FileInputStream psStream = null;
try {
psStream = new FileInputStream(epsilon);
} catch (FileNotFoundException ffne) {
ffne.printStackTrace();
}
if (psStream == null) {
return;
}
DocFlavor psInFormat = DocFlavor.INPUT_STREAM.AUTOSENSE;
Doc myDoc = new SimpleDoc(psStream, psInFormat, null);
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
PrintService[] services = PrintServiceLookup.lookupPrintServices(psInFormat, aset);
// this step is necessary because I have several printers configured
PrintService myPrinter = null;
for (int i = 0; i < services.length; i++)
{
String svcName = services[i].toString();
System.out.println("service found: "+ svcName);
if (svcName.contains("series"))
{
myPrinter = services[i];
System.out.println("my printer found: "+svcName);
break;
}
}
if (myPrinter != null) {
DocPrintJob job = myPrinter.createPrintJob();
try {
job.print(myDoc, aset);
} catch (Exception pe) {pe.printStackTrace();}
} else {
System.out.println("no printer services found");
}
}
public static void main(String[] args) throws IOException
{
//@SuppressWarnings("unused")
//Testing t = new Testing();
String DEST = ("C:/Users/Brian/Desktop/SO046201R-17/TestingAlpha/6FS-2m.pdf");
sendPdfToPrinter(DEST);
}
I'm currently writing a program that'll write text to a series of PDF files and then send them to a printer. I've got the writing to the PDF part done, but whenever I try passing the file to the "sendPdfToPrinter" method I encounter problems. So far I've tested a HP Deskjet printer and a Canon Inkjet printer with no success (former gave a "Java Document error" message and latter wouldn't add file to the queue). I suppose my question boils down to this:
Is the problem with the code or with the printers I'm using? Is there a workaround?
Using Mark's edit:
public static void sendPdfToPrinter(String epsilon) throws
InvalidPasswordException, IOException
{
FileInputStream psStream = null;
try {
psStream = new FileInputStream(epsilon);
} catch (FileNotFoundException ffne) {
ffne.printStackTrace();
}
if (psStream == null) {
return;
}
DocFlavor psInFormat = DocFlavor.INPUT_STREAM.AUTOSENSE;
PDDocument myDoc = PDDocument.load(new File(epsilon));
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
PrintService[] services =
PrintServiceLookup.lookupPrintServices(psInFormat, aset);
// this step is necessary because I have several printers configured
PrintService myPrinter = null;
for (int i = 0; i < services.length; i++)
{
String svcName = services[i].toString();
System.out.println("service found: "+ svcName);
if (svcName.contains("series"))
{
myPrinter = services[i];
System.out.println("my printer found: "+svcName);
break;
}
}
if (myPrinter != null) {
PrinterJob job = PrinterJob.getPrinterJob();
try {
job.setPrintService(myPrinter);
job.setPageable(new PDFPageable(myDoc));
job.print();
} catch (PrinterException e) {
// Handle the exception.
}
} else {
System.out.println("no printer services found");
}
}
Upvotes: 2
Views: 1520
Reputation: 1765
This may be because your printers don't natively support PDFs. I actually managed to print a PDF with your code, but it printed it as it would look if a PDF file was opened as a text file.
One solution is to use Apache's PDF library for Java: PDFBox. The file can be loaded like so:
PDDocument myDoc = PDDocument.load(new File(epsilon));
To print the file, use PrinterJob
instead of DocPrintJob
:
PrinterJob job = PrinterJob.getPrinterJob();
try {
job.setPrintService(myPrinter);
job.setPageable(new PDFPageable(myDoc));
job.print();
} catch (PrinterException e) {
// Handle the exception.
}
Upvotes: 1