Direct PDF Printing on Java Printer

Asked

Viewed 2,407 times

4

So, I need to print a PDF directly to the printer, because I need to print Tax Notes.If I use this form Desktop d = Desktop.getDesktop().print("Caminho do arquivo") it prints normally the first time, but opens the adobe together and the adobe closes the document and then when I will print again, it "Buga" and prints everything that is in the extremely small PDF. And the idea is to print directly and in the correct size of the paper.

I own these 3 printers :Bematech MP 4200 TH, Bematech MP 4000 TH e MP2500TH. Below is how I learned to print directly from the printer.

PrintService iPadrao = PrintServiceLookup.lookupDefaultPrintService();
DocFlavor docFlavor = DocFlavor.INPUT_STREAM.AUTOSENSE;

try {
    FileInputStream stream = new FileInputStream("C:\\Users\\EX-PHP\\Documents\\TestePDF2\\vai.pdf");

    Doc doc = new SimpleDoc(stream, docFlavor, null);
    DocPrintJob p = iPadrao.createPrintJob();
    p.print(doc, null);
} catch (PrintException e) {
    e.printStackTrace();
}

The PDF in question contains a tax note containing a QR code. I also tried with Pdfrenderer, and with some codes I found on the internet but without success,.

  • If you use DocFlavor.INPUT_STREAM.PDF, what happens?

  • So there was just a mistake of my own, but putting everything right and with Flavor PDF it from invalid Browse. I need you to print it directly because it’s for printing a receipt.

  • Which model of your printer?

  • I have 2, Bematech MP 4200 TH, Bematech MP 4000 TH.

1 answer

2


An option to work with PDF is to use the library Apache PDFBox that will extract the contents of your file and allow it to be printed:

public static void main(String args[]) throws Exception {

  PDDocument documento = PDDocument.load(new File("C:/Users/EX-PHP/Documents/TestePDF2/vai.pdf"));
  PrintService servico = PrintServiceLookup.lookupDefaultPrintService();

  PrinterJob job = PrinterJob.getPrinterJob();
  job.setPageable(new PDFPageable(documento));
  job.setPrintService(servico);
  job.print();
  documento.close();
}

The dependencies for use of Apache PDFBox can be found in the session Dependencies site. For this example you will need Apache Commons Logging and the fontbox.

If you are using Maven use the following dependency import:

<dependency>
    <groupId>org.apache.pdfbox</groupId>
    <artifactId>pdfbox</artifactId>
    <version>2.0.7</version>
</dependency>

Answer to similar question on Stack Overflow: Print a PDF file using Printerjob in Java.

  • I downloaded the jar put it right there and used this same code, and returned me this error NoClassDefFoundError: org/apache/commons/logging/LogFactory

  • @Thej add the library Apache Commons Logging to your project also.

  • added and now returned this exception NoClassDefFoundError: org/apache/fontbox/type1/DamagedFontException

  • @Thej add the fontbox

  • It worked , thanks yesterday I was loco tired looking with a 20 tab open looking and re-looking Kd link and testing everything to see if funfava and could not and I didn’t even pass near this Pdfbox.As for the explanation of these 2 last jars that I added, i would need to create another post?

  • @Thej no, I’ll include in the answer

  • Quiet, thank you very much, you saved my livelihood.

  • My doubt now would be about the alignment of things to print. Because if I set everything to ALLIGN.CENTER or ALLIGN.MIDDLE the printer prints a small piece, and still blank. I open another post?

  • @Thej Yes, the doubt is now another

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.