Printing report programmatically

Stimulsoft Reports.JAVA discussion
Post Reply
paroxit
Posts: 11
Joined: Thu Apr 12, 2018 10:53 am

Printing report programmatically

Post by paroxit »

We developing a web app with Java + Spring. For the design phase, we use JS report designer, everything works perfectly.

But for some business logic, we need to print a report design from Java code programmatically.

Is it possible to print a report from Java on a web application ?

I've tried below code but it didn't work.

Code: Select all

        StiReport stiReport = StiReport.newInstance();
        stiReport.loadFromJson(modelJsonStr);

        StiJsonDatabase jsonDatabase = new StiJsonDatabase("offer");
        jsonDatabase.LoadFromJsonObject(new JSONObject(dataJsonString));

        stiReport.getDictionary().getDatabases().add(jsonDatabase);
        stiReport.render();

        PrinterJob printJob1 = StiPrintHelper.preparePrinterJob(stiReport.pages);

        StiPrintHelper.printJob(printJob1,stiReport);
This code thrown below exception;

java.lang.IllegalArgumentException: Numbers of source Raster bands and source color space components do not match

Maybe I need to try another way ?
paroxit
Posts: 11
Joined: Thu Apr 12, 2018 10:53 am

Re: Printing report programmatically

Post by paroxit »

Using a temporary file and Apache PDFBox, I was able to do that.

Code: Select all

        
        
        String jsonPath = request.getSession().getServletContext().getRealPath("/resources/temp/"+DateTime.now().getMillis()+".json");

        Files.write(Paths.get(jsonPath),jsonData.getBytes());

        StiJsonDatabase jsonDatabase = new StiJsonDatabase("offer",jsonPath);

        StiDatabaseCollection coll = new StiDatabaseCollection();
        coll.add(jsonDatabase);
        stiReport.dictionary.setDatabases(coll);
        stiReport.setJsonReport(true);

        stiReport.render();
        
        PrintServiceAttributeSet printServiceAttributeSet = new HashPrintServiceAttributeSet();
        printServiceAttributeSet.add(new PrinterName(printerName, null));
        
        javax.print.PrintService[] printServices = (javax.print.PrintService[]) PrintServiceLookup.lookupPrintServices(null, printServiceAttributeSet);
        javax.print.PrintService printService = printServices[0];

        PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
        Copies copies = new Copies(1);
        pras.add(copies);
        pras.add(OrientationRequested.PORTRAIT);
        pras.add(MediaSizeName.ISO_A4);

	// Write pdf to temporary file
        String realFilePath = request.getSession().getServletContext().getRealPath("/resources/temp/"+DateTime.now().getMillis()+".pdf");
        OutputStream outputStream = new FileOutputStream(realFilePath);
        StiExportManager.exportPdf(stiReport, outputStream);

        outputStream.flush();
        outputStream.close();

	// Get temporary pdf and print it using Apache PDFBox
        PDDocument document = PDDocument.load(new File(realFilePath));

        PrinterJob job = PrinterJob.getPrinterJob();
        job.setPageable(new PDFPageable(document));
        job.setPrintService(printService);
        job.print();

        document.close();
        
        
Lech Kulikowski
Posts: 6197
Joined: Tue Mar 20, 2018 5:34 am

Re: Printing report programmatically

Post by Lech Kulikowski »

Hello,

Thank you for the provided information.
Post Reply