将Byte数组转换为pdf进行打印

时间:2013-10-29 05:02:07

标签: pdf bytearray

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

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

1 个答案:

答案 0 :(得分:0)

从我传递字节数组和

的方法调用它
DocFlavor flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE;

你必须通过传递flavor和byte数组来调用这个方法。比如这个

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");
          }
}