-
×InformationNeed Windows 11 help?Check documents on compatibility, FAQs, upgrade information and available fixes.
Windows 11 Support Center. -
-
×InformationNeed Windows 11 help?Check documents on compatibility, FAQs, upgrade information and available fixes.
Windows 11 Support Center. -
- HP Community
- Printers
- Printing Errors or Lights & Stuck Print Jobs
- Cannot print using my deskjet 5810 printer using javax.print...

Create an account on the HP Community to personalize your profile and ask a question
11-23-2016 03:34 AM
I cannot print anything using my hp deskjet 5810 printer using the javax.print library. Printing works for other programs. I am not sure why its not working for my program.
this is the code i am using
DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PAGEABLE; PrintRequestAttributeSet patts = new HashPrintRequestAttributeSet(); patts.add(Sides.DUPLEX); PrintService[] ps = PrintServiceLookup.lookupPrintServices(flavor, patts); if (ps.length == 0) { throw new IllegalStateException("No Printer found"); } System.out.println("Available printers: " + Arrays.asList(ps)); PrintService myService = null; for (PrintService printService : ps) { if (printService.getName().equals("HP Deskjet 5810 series")) { myService = printService; break; } } if (myService == null) { throw new IllegalStateException("Printer not found"); } FileInputStream fis = new FileInputStream("C:/example.pdf"); Doc pdfDoc = new SimpleDoc(fis, DocFlavor.INPUT_STREAM.AUTOSENSE, null); DocPrintJob printJob = myService.createPrintJob(); printJob.print(pdfDoc, new HashPrintRequestAttributeSet()); fis.close();
I opened the printer software to see whats printing and this is what is seen while my code is running
Here the `Java Printing` print job stay there like for 2-3 seconds and vanishes without printing anything. Please shed some light on this. Any help would be much appreciated?
Solved! Go to Solution.
Accepted Solutions
12-05-2016 09:20 PM
Hi, i fixed the problem by changing from javax.print to java.awt printing and using PDFBox Now it works great. Here is the code
import java.io.File; import java.io.IOException; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.printing.PDFPageable; import java.awt.print.PrinterJob; import java.awt.print.PrinterException; public class App { public static void main( String[] args ) throws IOException, PrinterException { String filename = "./example.pdf"; PDDocument document = PDDocument.load(new File(filename)); PrinterJob job = PrinterJob.getPrinterJob(); job.setPageable(new PDFPageable(document)); if(job.printDialog()) { job.print(); } } }
and here is a link to a working example repository
11-24-2016 07:24 AM
Welcome to the HP Forums 🙂
I would like to take a moment and thank you for using this forum, it is a great place to find answers.
For you to have the best experience on the HP forum, you could also visit our HP Forums Guide for First Time Here? Learn How to Post and More.
As I understand the print job works well with other programs, however, it doesn't work whilst using the Javascript alone,
This would confirm that the issue isn't within the printer but the script used:
To know about the script, I would suggest you click here
(The above link is 3rd party and is not owned by HP, I've posted this in here only so that I may help you ward off your worries, please ignore the ads and take only the free/available suggestions)
Click here for Fixing Poor Print Quality (if any).
Let me know if those steps worked for you.
If they did, that’s awesome!
Give us some reinforcement by clicking the solution and kudos buttons,
that’ll help us and others see that we’ve got the answers!
Good Luck.
Riddle_Decipher
I am an HP Employee
Learning is a journey, not a destination.
Let's keep asking questions and growing together.
11-29-2016 04:23 AM - edited 11-29-2016 04:26 AM
Hi Riddle_Decipher,
Thanks for the support. I've tried the solution you gave me.
the solution instructed me to change the doc flavor to
application/octet-stream
unfortunately its not working
these are the supported doc flavors for my printer:
image/gif; class="[B" image/gif; class="java.io.InputStream" image/gif; class="java.net.URL" image/jpeg; class="[B" image/jpeg; class="java.io.InputStream" image/jpeg; class="java.net.URL" image/png; class="[B" image/png; class="java.io.InputStream" image/png; class="java.net.URL" application/x-java-jvm-local-objectref; class="java.awt.print.Pageable" application/x-java-jvm-local-objectref; class="java.awt.print.Printable" application/octet-stream; class="[B" application/octet-stream; class="java.net.URL" application/octet-stream; class="java.io.InputStream"
11-29-2016 07:54 AM
Thank you for the update,
Although, at this point, I'm obligated to let you know that the issue pertains the Java script alone and as you mentioned earlier the printer works fine with other applications,
I would recommend you contact a Java expert or a local tech who may assist you with the same
As I've provided all the support options i saw fit and have been aware off as this issue is beyond my boundaries of support.
Please let me know if there's anything else, apart from this that needs my attention or assistance 🙂
Have a good day, ahead
Regards,
Riddle_Decipher
I am an HP Employee
Learning is a journey, not a destination.
Let's keep asking questions and growing together.
12-05-2016 09:20 PM
Hi, i fixed the problem by changing from javax.print to java.awt printing and using PDFBox Now it works great. Here is the code
import java.io.File; import java.io.IOException; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.printing.PDFPageable; import java.awt.print.PrinterJob; import java.awt.print.PrinterException; public class App { public static void main( String[] args ) throws IOException, PrinterException { String filename = "./example.pdf"; PDDocument document = PDDocument.load(new File(filename)); PrinterJob job = PrinterJob.getPrinterJob(); job.setPageable(new PDFPageable(document)); if(job.printDialog()) { job.print(); } } }
and here is a link to a working example repository
12-06-2016 06:26 AM
Thank you for the update,
I appreciate your time and efforts,
I'm glad you found the solution you were looking for.
Thanks for taking the time to let the community know about the solution
You've been great to work with and it has been a genuine pleasure.
I hope both you and your product works great and remain healthy for a long time 😉
To simply say thanks for my efforts to assist you, please click the "Thumbs Up" button to give me a Kudos.
And accept this as the solution to help other find your post helpful.
Take care now and do have a fabulous week ahead. 🙂
Riddle_Decipher
I am an HP Employee
Learning is a journey, not a destination.
Let's keep asking questions and growing together.