Solved

Print PDF via java

Posted on 2004-09-14
18
1,605 Views
Last Modified: 2013-12-02
Is it possible to print a pdf using java?

The awt cant print pdfs.
The sdk1.4 provides printing services but pdfs are unsupported.

So... what to do?
Thanks,
Dominik
0
Comment
Question by:Daij-Djan
  • 6
  • 4
  • 2
  • +2
18 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 64 total points
ID: 12055881
0
 
LVL 14

Assisted Solution

by:sudhakar_koundinya
sudhakar_koundinya earned 62 total points
ID: 12055995
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12056012
But Open Source iText library is efficient I think

http://www.lowagie.com/iText/faq.html
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12056020
from FAQ of http://www.lowagie.com/iText/faq.html

How can I send a PDF file to a printer directly?

Printing is a very platform dependent functionality. On Windows, you can print a PDF file by executing Acrobat Reader:
String osName = System.getProperty("os.name" );
//FOR WINDOWS 95 AND 98 USE COMMAND.COM
if( osName.equals( "Windows 95" ) || osName.equals( "Windows 98" )){
    Runtime.getRuntime().exec("command.com /C start acrord32 /p /h" + claim.pdf);
}
//FOR WINDOWS NT/XP/2000 USE CMD.EXE
else {
    Runtime.getRuntime().exec("cmd.exe /C acrord32 /p /h" + claim.pdf);
}
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12056331
If you're using a current version of GhostScript for your printing, it will accept PDF as input, and convert to your printer's language. Use POSTSCRIPT as the flavor (PDF is PostScript 3)

If not, here are some alternatives:

http://crowbar.dnsalias.com:443/crowbar/000233.html
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12056344
Here's the pertinent portion of the post:

Well, I've just tested another "crowbar" that works very nicely with Java/Windows, but is a bit complex to setup. Once set up, however, it's quite reliable, will print to nearly any printer, and doesn't need any Adobe products. It will also work nicely under Linux (if ghostscript is installed - Redmon isn't required for Linux).

We are basically fooling Java into thinking it's printing a PostScript document, so it simply streams the PDF file's contents to the printer. Redmon intercepts the stream, and sends it through GhostScript to be interpreted into whatever language we tell it to, then streams the resultant data to the printer.

1. Install GhostScript (the latest version, or one which supports direct PDF printing)

2. Install Redmon.

3. Follow the instructions to set up a local printer, using the Apple PS printer driver (or any other PS driver):

http://www.cs.wisc.edu/~ghost/redmon/en/redmon.htm#371

4. In your .rsp file, set the DEVICE to match your printer. For PostScript printers, use "pswrite". If you're in the US, you will probably want to use "letter" for PAPERSIZE. Make sure the paths to lib and fonts match the version of gs you installed.

5. On the "RPT1: Properties" dialog, make sure the paths to gswin32c.exe and your .rsp file are set correctly (c:\gs\gs8.11\gswin32c.exe, c:\gs\myrspfile.rsp for the latest version of gs)

6. Set "Run:" to "Hidden", so that you won't get a flashing DOS box with every printjob.

7. Name your new local printer something like "PrintPDF".

8. Compile the java test program below, and run it with the path to a PDF file as the parameter.

9. When the print dialog comes up, make sure to select the printer you just created (PrintPDF), or you will print pages and pages of garbage!!!

This is a very simplistic test app - but it uses the standard javax.print methods to print PDF.

[CODE]
// PrintPDF.java
import javax.print.*;
import javax.print.attribute.*;
import java.io.*;

public class PrintPDF {
public static void main(String args[]) throws Exception {
String filename = args[0];
PrintRequestAttributeSet pras =
new HashPrintRequestAttributeSet();
DocFlavor flavor = DocFlavor.INPUT_STREAM.POSTSCRIPT;
PrintService printService[] =
PrintServiceLookup.lookupPrintServices(flavor, pras);
PrintService defaultService =
PrintServiceLookup.lookupDefaultPrintService();
PrintService service = ServiceUI.printDialog(null, 200, 200,
printService, defaultService, flavor, pras);
if (service != null) {
DocPrintJob job = service.createPrintJob();
FileInputStream fis = new FileInputStream(filename);
DocAttributeSet das = new HashDocAttributeSet();
Doc doc = new SimpleDoc(fis, flavor, das);
job.print(doc, pras);
Thread.sleep(10000);
}
System.exit(0);
}
}

[/CODE]

Another option (Windows only) if Redmon/GhostScript install/config is too complex. Also handles viewing/printing Word, Excel, .txt, etc.

http://forum.planetpdf.com/webboard/wbpx.dll/read?98958,3
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12056991
Try


import java.io.*;
import java.awt.*;
import java.awt.print.*;
import javax.print.*;
import javax.print.attribute.*;
import javax.print.attribute.standard.*;

public class PrintPDF
{

   public void print(String filename) throws IOException, PrintException
   {
        // Use the pre-defined flavor for a PDF from an InputStream
        DocFlavor flavor = DocFlavor.INPUT_STREAM.PDF;
       
        // Create a set which specifies how the job is to be printed
        PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
        aset.add(MediaSizeName.ISO_A4);
        aset.add(new Copies(1));
       
        // Locate print services which can print a PDF file the manner specified
        PrintService[] pservices = PrintServiceLookup.lookupPrintServices(flavor, aset);
       
        if (pservices.length > 0)
        {
            // Create a Print Job
            DocPrintJob printJob = pservices[0].createPrintJob();
           
            // Create a Doc implementation to pass the print data
            // Doc doc = new InputStreamDoc(filename, flavor);
            Doc doc = new SimpleDoc(new FileInputStream(filename), flavor, null);
           
            // Print the doc as specified
            printJob.print(doc, aset);
        }
        else
        {
            System.err.println("No suitable printers");
        }
    }
       
    public static void main(String[] arg)
    {
        try
        {
            new PrintPDF().print("prova.pdf");
        }
        catch(Exception ex){ ex.printStackTrace();
        }
    }
}
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 5

Author Comment

by:Daij-Djan
ID: 12058114
Read the iText solution but it is windows-only.

@CEHJ: tried that - no printers found
@sudhakar_koundinya: will try that when I have more time. Thank you!
0
 
LVL 92

Assisted Solution

by:objects
objects earned 62 total points
ID: 12060378
0
 
LVL 3

Assisted Solution

by:sbockelman
sbockelman earned 62 total points
ID: 12061154
what about FOP?  

http://xml.apache.org/fop/
0
 
LVL 5

Author Comment

by:Daij-Djan
ID: 12065186
@sudhakar_koundinya: redmon is for windows only, no?
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12071867
yes

i have already said that

>>Another option (Windows only) if Redmon/GhostScript install/config is too complex. Also handles viewing/printing Word,
>>Excel, .txt, etc.
0
 
LVL 5

Author Comment

by:Daij-Djan
ID: 12073846
I thought only th other answer was windows-only,
anyway, as there does not seem to be a FREE cross-platform solution or different ones even, ill probably just go with JPrint
0
 
LVL 92

Expert Comment

by:objects
ID: 12080172
did the two links i posted above not help?
0
 
LVL 5

Author Comment

by:Daij-Djan
ID: 12084400
the first is JPrint... Ill award points once integrated
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
In a previously published article (http://www.experts-exchange.com/articles/10331/Automatic-Duplex-Scanning-in-PaperPort-Versions-11-12-14.html) here at Experts Exchange, I explained how to achieve duplex (double-sided) scanning in Nuance's PaperPor…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now