• ×
    Information
    Windows update impacting certain printer icons and names. Microsoft is working on a solution.
    Click here to learn more
    Information
    Need Windows 11 help?
    Check documents on compatibility, FAQs, upgrade information and available fixes.
    Windows 11 Support Center.
  • post a message
  • ×
    Information
    Windows update impacting certain printer icons and names. Microsoft is working on a solution.
    Click here to learn more
    Information
    Need Windows 11 help?
    Check documents on compatibility, FAQs, upgrade information and available fixes.
    Windows 11 Support Center.
  • post a message
Guidelines
Experiencing 'Printer Blocked' or 'Printer Error' message? Click here for more information.
Check some of the most frequent questions about Instant Ink: HP INSTANT INK, HP+ PLANS: INK AND TONER.


Check out our Black or Color Ink Not Printing, Other Print Quality Issues info about: Print quality and Cartridge Issues.
HP Recommended
HP Deskjet 5810 Printer
Microsoft Windows 7 (32-bit)

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

 

print_problem.png

 

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?

1 ACCEPTED SOLUTION

Accepted Solutions
HP Recommended

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

View solution in original post

5 REPLIES 5
HP Recommended

 

@basithkunimal

 

 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

HP Recommended

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"
HP Recommended

@basithkunimal

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

HP Recommended

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

HP Recommended

@basithkunimal

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

† The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the <a href="https://www8.hp.com/us/en/terms-of-use.html" class="udrlinesmall">Terms of Use</a> and <a href="/t5/custom/page/page-id/hp.rulespage" class="udrlinesmall"> Rules of Participation</a>.