• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1797
  • Last Modified:

Print PDF via java

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
Daij-Djan
Asked:
Daij-Djan
  • 6
  • 4
  • 2
  • +2
4 Solutions
 
sudhakar_koundinyaCommented:
0
 
sudhakar_koundinyaCommented:
But Open Source iText library is efficient I think

http://www.lowagie.com/iText/faq.html
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
sudhakar_koundinyaCommented:
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
 
sudhakar_koundinyaCommented:
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
 
sudhakar_koundinyaCommented:
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
 
CEHJCommented:
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
 
Daij-DjanAuthor Commented:
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
 
sbockelmanCommented:
what about FOP?  

http://xml.apache.org/fop/
0
 
Daij-DjanAuthor Commented:
@sudhakar_koundinya: redmon is for windows only, no?
0
 
sudhakar_koundinyaCommented:
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
 
Daij-DjanAuthor Commented:
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
 
objectsCommented:
did the two links i posted above not help?
0
 
Daij-DjanAuthor Commented:
the first is JPrint... Ill award points once integrated
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

  • 6
  • 4
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now