Jump to content
We've recently updated our Privacy Statement, available here ×

Printing pdf


dencel

Recommended Posts

Just posting my findings with printing pdf files silent from a java application.

 

Beside some commercial products there are no good solutions to do this, at least I didn't find them.

 

The solution is to use a external program and call it with Runtime.getRuntime().exec().

 

There are two tools I've tested:

1. Acrobat reader

Command: acrord32 /t "<docname>" "<printername>"

For more information look at the developers FAQ doc of acrobat reader for all commandline parameters. The parameters are not supported!

I had problems printing using acrobat reader because it messed up all kind of characters when printing it silent, really weird. I decided to use program 2.

2. Ghostscript + gsprint

Command: gsprint -printer "<printer>" "<docname>"

These tools are free. GSprint is delivered with gsview. They are not very fast but work really good.

 

Hint: from windows, don't use spaces in the command and start a new command process for each process:

cmd.exe /C start C:Progra~1Ghostgumgsviewgsprint.exe -printer "<printer>" "<docname>"

Link to comment
Share on other sites

  • Replies 5
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

Why you just don't export your report directly to printer? Here's a snippet:

 

Code:
JasperPrint print = (JasperPrint) JRLoader.loadObject(new File("reports/fill/example1.jrprint"«»));
PrinterJob job = PrinterJob.getPrinterJob();
PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null);
int selectedService = 0;
for(int i = 0; i <services.length;i++){
if(services.getName().contains("HP"«»)){
selectedService = i;
}
}

System.out.println("Selected " + services[selectedService].getName());


job.setPrintService(services[selectedService]);
PrintRequestAttributeSet printRequestAttributeSet = new HashPrintRequestAttributeSet();
MediaSizeName mediaSizeName = MediaSize.findMedia(4, 4, MediaPrintableArea.INCH);
printRequestAttributeSet.add(mediaSizeName);
printRequestAttributeSet.add(new Copies(1));

JRPrintServiceExporter exporter;
exporter = new JRPrintServiceExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, print);
exporter.setParameter(JRPrintServiceExporterParameter.PRINT_SERVICE, services[selectedService]);
exporter.setParameter(JRPrintServiceExporterParameter.PRINT_SERVICE_ATTRIBUTE_SET, services[selectedService].getAttributes());
exporter.setParameter(JRPrintServiceExporterParameter.PRINT_REQUEST_ATTRIBUTE_SET, printRequestAttributeSet);
exporter.setParameter(JRPrintServiceExporterParameter.DISPLAY_PAGE_DIALOG, Boolean.FALSE);
exporter.setParameter(JRPrintServiceExporterParameter.DISPLAY_PRINT_DIALOG, Boolean.FALSE);
exporter.exportReport();
System.out.println("Done"«»);
Link to comment
Share on other sites

Thanks, that is good tip. But I am doing some editing on the pdf doc using iText.

 

I am creating A5 size prints using Jasper and then reformat them on a A4 thus saving the company some printingcosts ;). As far as I know this is not possible using Jasper (?).

Link to comment
Share on other sites

You can use this code if your using java 6 otherwise you have to download the right library :

 

Code:
Desktop.getDesktop().print(new File("your path"«»));

 

The only probleme is that it's a direct print. You can only print one file at a time.

 

Otherwise you can use that code if your compiling a jrxml :

 

Code:
[code]JasperPrintManager.printReport(jasperPrint, true);

 

The boolean value allow you to choose if you want to open a print panel or not.

Post edited by: bouliton, at: 2007/07/19 09:15

Link to comment
Share on other sites

dencel wrote:

Thanks, that is good tip. But I am doing some editing on the pdf doc using iText.

I am creating A5 size prints using Jasper and then reformat them on a A4 thus saving the company some printingcosts ;). As far as I know this is not possible using Jasper (?).

 

Ehhh? You arrange two A5 reports on a single page, am I right? If so - nice trick! I'm not quite sure, but I guess you can achieve this with subreports.

Link to comment
Share on other sites

The problem I was having with subreports is the overflow when the datasource has more detailrecords then the 1st report can display. Jasper doesn't like it and crashed sometimes.

The iText example page came to the rescue with examples like concat and twoOnOne. Now I create the pdf with jasper, concat some reports and then place two on one. I am working at a transport company where we use about 1000 A4 pages each day thus saving 500 pages each day (Take that Al Gore!;)).

 

I made a simple tool for it which takes a xml file and is configured like:

Code:

<operation action="concat">
<operation action="toA4">
<report name="MYREPORTNAME">
<param name="IMAGE_DIR">c:mypath</param>
</report>
</operation>
</operation>

If anybody likes to have it, I will post it online (if you don't mind my bash coding style).

 

 

Anyway just wanted to share the knowledge. Thanks for the replies, they are usefull!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...