Automatic printing does not work in Java
I have asked to print PDF files in two different ways - one through the web page, the user will see the print preview, select the printer and print The second method is automatic printing. PDF can be sent to the printer by clicking a button
The first printing method works through the web page, but not the second Successfully retrieved the default printer for automatic printing, but it won't print, and I didn't get any errors Here is my analysis:
>At first, I thought docflavor was not supported Then I list the docflavor supported by downloading the printer, one of which is the application / octet stream, that is, docflavor INPUT_ STREAM. AUTOSENSE. So the printer supports flavor. > Then, I added printjoblistener to check whether the print job failed When I add this listener to printjob, it prints No_ More_ Events and data_ TRANSFER_ Complete, the job should be printed_ Complete, if successful. > The final step is to debug the Java code When I execute a line job Print(), which enters win32printjob Print() method I did F6 to execute each line to see what it was doing I compared it with the code in grepcode because the source code is not loaded in eclipse It's all right. I can't see any mistakes The only place it didn't enter this block, it checked mdestination value, because I didn't provide it, it didn't pass
See the following code:
if (mDestination != null) { // if destination attribute is set try { FileOutputStream fos = new FileOutputStream(mDestination); byte [] buffer = new byte[1024]; int cread; while ((cread = instream.read(buffer,buffer.length)) >= 0) { fos.write(buffer,cread); } fos.flush(); fos.close(); } catch (FileNotFoundException fnfe) { notifyEvent(PrintJobEvent.JOB_Failed); throw new PrintException(fnfe.toString()); } catch (IOException ioe) { notifyEvent(PrintJobEvent.JOB_Failed); throw new PrintException(ioe.toString()); } notifyEvent(PrintJobEvent.DATA_TRANSFER_COMPLETE); notifyEvent(PrintJobEvent.JOB_COMPLETE); service.wakeNotifier(); return; }
This is job_ Complete is the only place I think this block is written to a file, which is not necessary for me
I think the actual print is in the same method win32printjob Print () occurs on the following line
private native boolean More ...printRawData(byte[] data,int count);
But this is a local method, so I don't know what's going on here
Please let me know why I can't print PDF
Edit:
Attach the code of the printed document:
PrintService pss = PrintServiceLookup.lookupDefaultPrintService(); DocPrintJob job = pss.createPrintJob(); DocAttributeSet das = new HashDocAttributeSet(); Doc document; try { document = new SimpleDoc(new FileInputStream(new File(fileName)),DocFlavor.INPUT_STREAM.AUTOSENSE,das); PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet(); job.addPrintJobListener(new PrintJobWatcher()); job.print(document,pras); }
Note: I have tried different flavors, such as PDF, PCL Without any work, I get unsupported runtime errors
Solution
You tried jpadel to print PDF files:
From sample codes
final PdfBook pdfBook = new PdfBook(pdfDecoder,printJob.getPrintService(),attributes); pdfBook.setChooseSourceByPdfPageSize(false); final SimpleDoc doc = new SimpleDoc(pdfBook,DocFlavor.SERVICE_FORMATTED.PAGEABLE,null); // used to track print activity printJob.addPrintJobListener(new PDFPrintJobListener()); try { printJob.print(doc,attributes); } catch (final Exception e) { LogWriter.writeLog("Exception " + e + " printing"); // <end-demo> }
In addition, you can provide the printer name and add the list pdfprintjoblistener