-1

我有一个字节数组,我需要通过打印服务器套接字发送这个字节数组以打印为 pdf。如何将此字节数组转换为 pdf 字节?

基本上,我创建了一个 pdf 模板并使用 PdfStamper,我正在生成 pdf
PdfStamper stamper = new PdfStamper(pdfTemplate, out); 然后将 ByteArrayOutputStream 转换为字节数组 (out.toByteArray())。我正在将此字节数组发送到另一个服务,该服务只是选择字节数组并将其通过套接字发送以进行打印。我试图打印这个,但它只打印一个空白页。我猜,打印机没有将其识别为 pdf。如何告诉打印机我的字节数组是 pdf(如何将字节数组转换为 pdf 可打印格式)

4

1 回答 1

0

从我传递我的字节数组的方法中调用它

DocFlavor flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE;

您必须通过传递风味和字节数组来调用此方法 像这样

new PrintTest().print(byteArray, flavor);




import java.awt.Graphics;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.util.Set;

import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintException;
import javax.print.PrintService;
import javax.print.SimpleDoc;
import javax.print.attribute.DocAttributeSet;
import javax.print.attribute.standard.PrinterStateReason;
import javax.print.attribute.standard.PrinterStateReasons;
import javax.print.attribute.standard.Severity;
import javax.print.event.PrintJobAttributeEvent;
import javax.print.event.PrintJobAttributeListener;
import javax.print.event.PrintJobEvent;
import javax.print.event.PrintJobListener;
import javax.print.event.PrintServiceAttributeEvent;
import javax.print.event.PrintServiceAttributeListener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class PrintTest implements PrintServiceAttributeListener,PrintJobListener,Doc, Printable, PrintJobAttributeListener {

    protected Logger logger = LoggerFactory.getLogger(PrintTest.class);

    public void print(final byte[] byteArray, final DocFlavor flavor) {

        Thread newThread = new Thread(new Runnable() {

            public void run() {

                PrintService ps = PrinterJob.getPrinterJob().getPrintService();

                ps.addPrintServiceAttributeListener(PrintTest.this);
                DocPrintJob docJob = ps.createPrintJob();
                docJob.addPrintJobAttributeListener(PrintTest.this, null);
                docJob.addPrintJobListener(PrintTest.this);
                Doc document = new SimpleDoc(byteArray, flavor, null);

                try {
                  docJob.print(document,null);

                }
                catch (PrintException e) {
                  e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                }
            } 
         });

         newThread.start();

        /**
        PrintServiceAttributeSet attSet = ps.getAttributes();
        PrinterStateReasons psr = ps.getAttribute(PrinterStateReasons.class);

        if (psr != null) {
          Set<PrinterStateReason> errors = psr.printerStateReasonSet(Severity.REPORT);
          for (PrinterStateReason reason : errors)
            System.out.printf(" Reason : %s",reason.getName());
          logger.info();
        }          */
      }

    public void attributeUpdate(PrintServiceAttributeEvent psae) {
        logger.info("attributeUpdate: "+psae.getAttributes());
      }

      public void printDataTransferCompleted(PrintJobEvent pje) {
        logger.info("Transfer completed");
      }

      public void printJobCompleted(PrintJobEvent pje) {
        logger.info("Completed");
      }

      public void printJobFailed(PrintJobEvent pje) {
            logger.info("Failed");
            PrinterStateReasons psr = pje.getPrintJob().getPrintService().getAttribute(PrinterStateReasons.class);
            if (psr != null) {
              Set<PrinterStateReason> errors = psr.printerStateReasonSet(Severity.REPORT);
              for (PrinterStateReason reason : errors)
                  logger.info(" Reason : %s",reason.getName());
              logger.info("\n");
            }
      }

      public void printJobCanceled(PrintJobEvent pje) {
            logger.info("Canceled");
          }

          public void printJobNoMoreEvents(PrintJobEvent pje) {
            logger.info("No more events");
            logger.info("printJobNoMoreEvents: "+pje.getPrintEventType());
          }

          public void printJobRequiresAttention(PrintJobEvent pje) {
            logger.info("Job requires attention");
            PrinterStateReasons psr = pje.getPrintJob().getPrintService().getAttribute(PrinterStateReasons.class);
            if (psr != null) {
              Set<PrinterStateReason> errors = psr.printerStateReasonSet(Severity.REPORT);
              for (PrinterStateReason reason : errors)
                  logger.info(" Reason : %s",reason.getName());
              logger.info("\n");
            }
          }

          public DocFlavor getDocFlavor() {
            return DocFlavor.SERVICE_FORMATTED.PRINTABLE;  //To change body of implemented methods use File | Settings | File Templates.
          }

          public Object getPrintData() throws IOException {
            return this;
          }

          public DocAttributeSet getAttributes() {
            return null;  //To change body of implemented methods use File | Settings | File Templates.
          }

          public Reader getReaderForText() throws IOException {
            return null; //To change body of implemented methods use File | Settings | File Templates.
          }

          public InputStream getStreamForBytes() throws IOException {
            return null;  //To change body of implemented methods use File | Settings | File Templates.
          }

          public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
            return pageIndex == 0 ? PAGE_EXISTS : NO_SUCH_PAGE;  //To change body of implemented methods use File | Settings | File Templates.
          }

          public void attributeUpdate(PrintJobAttributeEvent pjae) {
            logger.info("Look out");
          }
}
于 2018-04-24T12:07:50.950 回答