打印ByteArrayOutputStream对象

时间:2018-05-09 08:55:32

标签: java printing itext java-print

我有一个使用iText库生成的PDF文档,然后我使用ByteArrayOutputStream将文档保存在内存中进行打印,但它没有打印任何内容。任何关于为什么不打印的想法?您可以在下面找到代码并提前感谢。

    ByteArrayOutputStream byteArr = new ByteArrayOutputStream();
    Document document = new Document();
    PdfWriter.getInstance(document, byteArr);
    document.open();

    /*
    * Adding data and tables to the document
    */

    document.close();

    DocFlavor docType = DocFlavor.BYTE_ARRAY.AUTOSENSE;    
    byte[] byteStream = byteArr.toByteArray();// fetch content in byte array;
    // byteArr is the ByteArrayOutputStream object
    // Tried using InputStream but did not work as well.

    Doc documentToBePrinted = new SimpleDoc(byteStream, docType, null);
    PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();  
    PrintService services = PrintServiceLookup.lookupDefaultPrintService();               
    DocPrintJob job = services.createPrintJob();  
    try {  
    job.print(documentToBePrinted, aset);
    System.out.println("Donee");

    } 
    catch (Exception pe)
    {
     pe.printStackTrace();

    } 

    byteArr = null;


}

2 个答案:

答案 0 :(得分:1)

无法运行代码,这不是一个容易回答的问题。

可能的问题是您没有关闭1) sessionToken=$(curl -s -X POST -d '{"userName":"NAME", "password": "xxxxx"}' https://host:18641/security/public-rest/v1/authentication/login -H "Content-Type:application/json" -H "X-Requested-By:SDC" -c - | grep SSO | grep -o '\S*$') 2) curl -X POST https://host:18641/jobrunner/rest/v1/job/681c449d-7c22-48d6-9532-2e6ef74971bc/start --header "Content-Type:application/json" --header "X-Requested-By:SDC" --header "X-SS-REST-CALL:true" --header "X-SS-User-Auth-Token:$sessionToken" -i 课程。 因此,不会释放底层资源,并且不会刷新字节。

因此,生成了无效(有时甚至是空的)PDF文档,当然打印机不会(或不能)打印。

如果您可以存储PDF,并且它在查看器中正确显示,那么您很可能在应用程序的打印部分出错了。

答案 1 :(得分:0)

您使用的打印机可能不直接支持PDF。尝试使用PDFBox打印文档。我做了一个小的maven项目来测试它,它对我有用:

Main.java:

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.printing.PDFPageable;

import javax.print.*;
import java.awt.print.PrinterJob;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

class Main {
    public static void main(String[] args) throws DocumentException, IOException {
        ByteArrayOutputStream byteArr = new ByteArrayOutputStream();
        Document pdfDocument = new Document();
        PdfWriter.getInstance(pdfDocument, byteArr);
        pdfDocument.open();
        pdfDocument.add(new Paragraph("Hello World!"));
        pdfDocument.close();

        byte[] byteStream = byteArr.toByteArray();// fetch content in byte array;
        PrintService services = PrintServiceLookup.lookupDefaultPrintService();
        PrinterJob job = PrinterJob.getPrinterJob();
        PDDocument pdDocument = null;
        try {
            pdDocument = PDDocument.load(byteStream);
            job.setPageable(new PDFPageable(pdDocument));
            job.setPrintService(services);
            job.print();
            System.out.println("Done");

        } catch (Exception pe) {
            pe.printStackTrace();

        } finally {
            if (pdDocument != null) {
                pdDocument.close();
            }
        }


    }
}

的pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>it.fortytwoapps</groupId>
    <artifactId>so-50249273</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itextpdf</artifactId>
            <version>5.5.13</version>
        </dependency>

        <dependency>
            <groupId>org.apache.pdfbox</groupId>
            <artifactId>pdfbox</artifactId>
            <version>2.0.9</version>
        </dependency>
    </dependencies>
</project>