Solved

Print PDF via java

Posted on 2004-09-14
18
1,662 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
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
 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

PaperPort is a popular document imaging/management product from Nuance Communications (http://www.nuance.com/). It is in widespread use by both individuals (http://www.nuance.com/for-individuals/by-product/paperport/index.htm) and businesses (http:/…
In a previous article here at Experts Exchange (http://www.experts-exchange.com/articles/18414/Create-a-PDF-file-with-Contact-Sheets-montage-of-thumbnails-for-all-JPG-files-in-a-folder-and-each-of-its-subfolders-using-an-automated-batch-method.html)…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
Suggested Courses

710 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