【问题标题】:Print PDF as byte array in JAVA在 JAVA 中将 PDF 打印为字节数组
【发布时间】:2014-03-24 11:09:28
【问题描述】:

我需要为我的打印机打印一个 pdf 文件。使用此代码,我已将我的 pdf 转换为 bytearray,但我被卡住了,不知道如何将其发送到打印机。有人可以帮助我吗?

    File file = new File("java.pdf");

    FileInputStream fis = new FileInputStream(file);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    byte[] buf = new byte[1024];

    try {
        for (int readNum; (readNum = fis.read(buf)) != -1;) {
            bos.write(buf, 0, readNum); //no doubt here is 0
            //Writes len bytes from the specified byte array starting at offset off to this byte array output stream.
            System.out.println("read " + readNum + " bytes,");
        }
    } catch (IOException ex) {
        System.out.println("ERROR!");
    }
    byte[] bytes = bos.toByteArray();

提前谢谢你。

【问题讨论】:

  • 查看我的回答here。可以使用套接字来完成这项工作
  • 感谢您的回答,但还有其他可能,比如不是套接字?

标签: java android pdf zebra-printers


【解决方案1】:

要遵循的步骤

  1. 在您的环境中查找默认打印机服务。
  2. 为 PDF 定义用于打印的文档风格。
  3. 准备要从字节数组打印的文档。
  4. 执行打印作业。

示例代码sn-p:

byte[] bytes = bos.toByteArray();
ByteArrayInputStream bais = new ByteArrayInputStream( bytes );

// First identify the default print service on the system  
PrintService printService = PrintServiceLookup.lookupDefaultPrintService();  

// prepare the flvaor you are intended to print  
DocFlavor docFlavor = DocFlavor.BYTE_ARRAY.PDF;  

// prepare the print job
DocPrintJob printJob = printService.createPrintJob();  

// prepare the document, with default attributes, ready to print  
Doc docToPrint = new SimpleDoc( bais, docFlavor, null );  

// now send the doc to print job, with no attributes to print
printJob.print( docToPrint, null );

【讨论】:

  • 它抛出“数据不是声明类型”异常
  • @whizzzkey:???哪个数据???它在哪里????发布您的查询,并提供有关您被打动的地方的适当详细信息...
  • "哪些数据???" - 只是简单的 pdf 文档,“它在哪里????” - 在线 Doc docToPrint = new SimpleDoc( bais, docFlavor, null );。我按照您发布的所有内容进行操作,并且我有一个类似 TS 的代码,用于从我的 pdf 文件中获取字节数组。
  • 正如我所建议的,您最好将您的代码、异常堆栈跟踪与您的查询一起作为一个新问题发布。仅通过 cmets 很难确定问题并给出解决方案。
  • 这段代码总是会抛出异常。您应该将字节数组而不是 IS 传递给 SimpleDoc obj(不需要 IS 更清楚)。否则,您应该将文档风格更改为 DocFlavor.INPUT_STREAM 才能正常工作。
【解决方案2】:

使用ByteArrayInputStream 会抛出java.lang.IllegalArgumentException,但常规字节数组对我有用。修改Ravinder Reddy的例子:

    // Get default print service
    PrintService printService = PrintServiceLookup.lookupDefaultPrintService();

    // Create a print job
    DocPrintJob printJob = printService.createPrintJob();

    // Optional fancy listener
    printJob.addPrintJobListener(new PrintJobAdapter() {
        @Override
        public void printJobCompleted(PrintJobEvent pje) {
            System.out.println("PDF printing completed");
            super.printJobCompleted(pje);
        }

        @Override
        public void printJobFailed(PrintJobEvent pje) {
            System.out.println("PDF printing failed");
            super.printJobFailed(pje);
        }
    });

    // Check the PDF file and get a byte array
    File pdfFile = new File("path/to/pdf");
    if (pdfFile.exists() && pdfFile.isFile()) {
        byte[] PDFByteArray = Files.readAllBytes(FileSystems.getDefault().getPath(pdfFile.getAbsolutePath()));

        // Create a doc and print it
        Doc doc = new SimpleDoc(PDFByteArray, DocFlavor.BYTE_ARRAY.AUTOSENSE, null);
        printJob.print(doc, null);
    }

【讨论】:

    【解决方案3】:

    另一种方法是使用intent 发送pdf 文件,这里是example

    示例代码:

    Intent prnIntent = new Intent(Intent.ACTION_SEND);
    prnIntent.putExtra(Intent.EXTRA_STREAM, uri);
    
    prnIntent.setType("application/pdf");
    
    
    startActivity(Intent.createChooser(prnIntent , "Send pdf using:"));
    

    使用这种方法无需使用缓冲区,而是将 pdf 文件直接发送到打印机!

    【讨论】:

    • 是的,但我已将我的 pdf 转换为 bytearray,在您的示例中,我将 bytearray 发送到打印机?
    • 在这个例子中你发送一个 pdf 文件。因此,您必须先将工作作为 bytearray,然后使用 convert to pdf 文件,然后使用此解决方案将其发送到打印机。
    • Android 方法将如何帮助标准 Java 应用程序? Intents 在这里如何适应?
    • 你是对的Christian 这只是一个安卓解决方案,但我认为这个问题是针对安卓的。所以,我认为问题中缺少标签 android。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-24
    • 2016-06-15
    • 2011-09-21
    • 2021-07-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多