Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Print PDF via java

Posted on 2004-09-14
18
Medium Priority
?
1,751 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
15 Comments
 
LVL 86

Accepted Solution

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

Assisted Solution

by:sudhakar_koundinya
sudhakar_koundinya earned 248 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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 248 total points
ID: 12060378
0
 
LVL 3

Assisted Solution

by:sbockelman
sbockelman earned 248 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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:/…
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…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Suggested Courses

876 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