打印文件时获取原始文件流(pdf)

时间:2014-03-26 14:39:02

标签: java jasper-reports

我正在尝试打印JasperExportManager.exportReportToPdfStream(jasperPrint,outputStream)的结果并在纸上获取原始流(打印结果)。我不知道我的java源代码是否有问题。如何打印pdf文件(创建打印作业)?

package testprint;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintException;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.SimpleDoc;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.Copies;
import javax.print.attribute.standard.JobName;
import javax.print.attribute.standard.MediaPrintableArea;
import javax.print.attribute.standard.MediaSize;
import javax.print.attribute.standard.MediaSizeName;
import net.sf.jasperreports.engine.JREmptyDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;

public class Printing {

    public static void main(String[] args) {
        Printing printing = new Printing();
        Map<String, Object> params = new HashMap();
        params.put("TEST_PARAM", "This is a test string");

        printing.print(params, 1);
    }

    public void print(Map<String, Object> params, int copies) {
        try {
            InputStream jasper = getClass().getResourceAsStream("/jasper/TestReport.jasper");
            JasperPrint jasperPrint = JasperFillManager.fillReport(jasper, params, new JREmptyDataSource());

            File tempFile = File.createTempFile("TestReport", ".tmp");
            OutputStream out = new FileOutputStream(tempFile);
            JasperExportManager.exportReportToPdfStream(jasperPrint, out); 

            PrintService printService = PrintServiceLookup.lookupDefaultPrintService();
            DocFlavor docflavor = DocFlavor.INPUT_STREAM.AUTOSENSE;

            PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
            aset.add(new Copies(copies));
            MediaSizeName mediaSizeName = MediaSize.findMedia((float) 8.5, (float) 2.75, MediaPrintableArea.INCH);
            aset.add(mediaSizeName);
            String jobName = "Test Report";
            aset.add(new JobName(jobName, Locale.US));

            Doc doc = new SimpleDoc(new FileInputStream(tempFile), docflavor, null);

            DocPrintJob printJob = printService.createPrintJob();
            printJob.print(doc, aset);

        } catch (FileNotFoundException ex) {
            System.out.println(ex.getMessage());
        } catch (JRException | IOException | PrintException ex) {
            System.out.println(ex.getMessage());
        }
    }
}

在纸上得到以下结果(打印结果)

%PDF-1.4
%âãÏÓ
3 0 obj
<</Length 138/Filter/FlateDecode>>stream
xœ+är
á26S043RIár
á
ä2Rð‰* !ˆ4´´PÉåÒw3T04PIãÒÐÉ©E(1PHÎEÖdhj¢`hd¡2Y§X¾(KÃ#5''dŽB:³¢ct
Ø-¸m12´$`KHFf±%*”¤—(—eæ¥e+ —;z
endstream
endobj
1 0 obj
<</Group<</Type/Group/CS/DeviceRGB/S/Transparency>>/Parent 4 0 R/Contents 3 0 R/Type/Page/Resources<</ProcSet [/PDF /Text /ImageB /ImageC /ImageI]/ColorSpace<</CS/DeviceRGB>>/Font<</F1 2 0 R>>>>/MediaBox[0 0 612 198]>>
endobj
5 0 obj
[1 0 R/XYZ 0 208 0]
endobj
2 0 obj
<</BaseFont/Helvetica/Type/Font/Encoding/WinAnsiEncoding/Subtype/Type1>>
endobj
4 0 obj
<</ITXT(2.1.7)/Type/Pages/Count 1/Kids[1 0 R]>>
endobj
6 0 obj
<</Names[(JR_PAGE_ANCHOR_0_1) 5 0 R]>>
endobj
7 0 obj
<</Dests 6 0 R>>
endobj
8 0 obj
<</Names 7 0 R/Type/Catalog/ViewerPreferences<</PrintScaling/AppDefault>>/Pages 4 0 R>>
endobj
9 0 obj
<</Creator(JasperReports \(TestReport\))/Producer(iText 2.1.7 by 1T3XT)/ModDate(D:20140326210633+07'00')/CreationDate(D:20140326210633+07'00')>>
endobj
xref
0 10
0000000000 65535 f 
0000000220 00000 n 
0000000489 00000 n 
0000000015 00000 n 
0000000577 00000 n 
0000000454 00000 n 
0000000640 00000 n 
0000000694 00000 n 
0000000726 00000 n 
0000000829 00000 n 
trailer
<</Root 8 0 R/ID [<4fbdbc1295f7e9f178f2c58efb84a5f8><e0046353cbc6a6edb104ec2aee0a5fb1>]/Info 9 0 R/Size 10>>
startxref
989
%%EOF

0 个答案:

没有答案