1

I am using jwt awt to print pdf file in java. But the print keeps printing in unknown format and forever. Please find the below image, enter image description here

I have used java code,

    PrintService service = PrintUtility.findPrintService("Champ RP Series");
        FileInputStream inputStream = new FileInputStream("D:/Rose/Work/17.pdf");
        PrintRequestAttributeSet  pras = new HashPrintRequestAttributeSet();
        pras.add(new Copies(1));

        DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
        Doc doc = new SimpleDoc(inputStream, flavor, null);
        DocPrintJob job = service.createPrintJob();

        job.print(doc, pras);
        inputStream.close();

Is there any way to preview this before printing? How can I solve this issue? Thank you

Rose
  • 666
  • 6
  • 14
  • 1
    Seems like this is printing the text representation of the binary data. Are you sure AWT can even understand the PDF format? Have a look here, this might help: https://stackoverflow.com/questions/16293859/print-a-pdf-file-using-printerjob-in-java – Thomas Oct 04 '22 at 07:19
  • 2
    java is not per se pdf aware. You could use https://pdfbox.apache.org/ to open and print pdfs – Lutz Oct 04 '22 at 07:22
  • 1
    I do wonder if using [DocFlavor.INPUT_STREAM.PDF](https://docs.oracle.com/en/java/javase/19/docs/api/java.desktop/javax/print/DocFlavor.INPUT_STREAM.html#PDF) would have had better results than using AUTOSENSE. – VGR Oct 04 '22 at 12:24
  • I used the code " PDDocument document = PDDocument.load(new File("D:\\Rose\\Work\\17.pdf")); PrintService myPrintService = findPrintService("Champ RP Series"); PrinterJob job = PrinterJob.getPrinterJob(); job.setPageable(new PDFPageable(document)); job.setPrintService(myPrintService); job.print();" , now there is no issue in printing but I am unable to capture the status of the print. How can I capture the status automatically? Thanks –  Oct 06 '22 at 04:14
  • @VGR if I use PDF, getting error as "sun.print.PrintJobFlavorException: invalid flavor" –  Oct 06 '22 at 06:28

1 Answers1

1

If your printer is a Champ it needs the pdf converted into Esc/POS Printer Language

Here we have a web page or PDF prior to print on the left and the Esc/POS codes the printer needs on the right. There is NO way you can directly send PDF to POS thermal unless it has an onboard PDF reading computer.

Generally "Thermal Printers" require either an encoded binary 7/8bit stream that includes "Escape Characters" sometimes seen as left arrow or box, that will not be used in a PDF, or 6/7bit text stream such as Zebra Print Language (ZPL) as used mainly by dispatch label devices.

enter image description here

Simulated Output enter image description here

The System Printer Application converts lines of input text (billing items) into strings of plain text with instructions as to which font to use from the printers onboard fonts (ANK Font Font A: 12x24dots, Font B: 9x17dots). The technology is similar to traditional monochrome dot matrix printers such as Epson 80 series (simulated above) but updated to cater for high speed serial (USB BT WiFi) rather than "fast" clunky Centronics LPT1 parallel cables.

You asked if you can preview before print and the best I can find are
php java tools https://github.com/dacduong/escpos-printer-simulator
or commercial apps (as above https://www.pclviewer.com/escdownload.htm)
or this one looks promising as lightweight android based mobile? https://github.com/402d/Virtual_POS_printer

K J
  • 8,045
  • 3
  • 14
  • 36
  • I am able to print after converting from PDF to Esc/POS Printer Language, but the printer output image is very small, How do I get the actual size? I have asked a question, https://stackoverflow.com/questions/74011940/printing-pdf-with-escpos-is-very-small-with-java –  Oct 10 '22 at 08:22