• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java printing - printing a pdf

 
John Smith
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've been googling information on how to print a pdf in java, and it seems that the java print api is used more for printing from a swing app. What I'm needing to do is send a pdf document to the printer upon user request.

I would like my app to work as follows

- User selects pdf to be printed
- Program determines pages to be printed and deducts amount from user account
- If sufficient money, program sends job to printer automatically without popping up the print screen.
- If printed successfully, then program tells user it was printed.

I'm able to figure out the rest of the pieces, in regards to user input and determining pages of the pdf, except I don't see how I will be able to print the pdf.

If someone could point me to some resources on java printing that i might not be finding, or help me with some information on this, I would truly appreciate it.

I've tried to use this code




but all that I get is junk characters spread across 15 or so pages, for a pdf file that simply says "Hello World". I can open the pdf file on my computer, and print it ok, but when using the above code, it just prints out junk. I'm wondering if anyone can point me somewhere that can help me understand how I might need to format or setup the pdf so that I can print it correctly. My program could be run on any number of Linux systems, with a multitude of printer types.
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think there are printers or printer drivers that understand PDF files (at least not in the standard JRE), so this approach likely won't work.

This library can render PDFs; it should be possible to adapt it to print them as well (if it can't do that out of the box).
 
Joe Ess
Bartender
Posts: 9313
10
Linux Mac OS X Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This code works for me (adapted from the example in the java api)



The only real difference I see is that the code above passes a PrintRequestAttributeSet instance to the job.print() call.
As Ulf mentions, this code is dependent on your printer being able to direct-print a PDF. My LaserJet 4250 has this capability.
[ April 24, 2008: Message edited by: Joe Ess ]
 
John Smith
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you say this code works for me, does that mean it works for you to be able to print pdfs? Or just to be able to print?
 
Joe Ess
Bartender
Posts: 9313
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Print PDFs (you can see the input file is "some.pdf").
By the way, could you take a moment and change your displayed name to your real (or at least real-sounding) first and last name? Our naming policy is one of our two rules (the other is "Be Nice"). You can change it here
 
John Smith
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your help. I've not got the opportunity to test it as I'm at work, but will let you know once I can.

I've also changed my username over as well. Thanks for the heads up.
 
John Smith
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I was able to test that code, and I'm getting the same as I got before. It prints fine, but it just prints out tons of characters, not the actual document I'm sending to the printer.

Any ideas? Is there a library that could be added into my code that would allow me to print a pdf, w/out having a special printer that can handle postscript pdf ?
 
Joe Ess
Bartender
Posts: 9313
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use Acrobat to print from the command line as documented here.
If that doesn't work, you could try the component Ulf pointed out and Java 2D printing.
 
Val Perumal
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Printing a PDF file from Java can be achieved using JPedal (a GPL version of JPedal is available), JPS and java.awt.print package.

The code (pure Java solution) is given below:

 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ulf Dittmer:
I don't think there are printers or printer drivers that understand PDF files


Sure there are - or the company I work for wouldn't have a business! Low-end consumer printers typically won't do PDF directly; larger office and production printers mostly will do PDF (and lots of other formats) directly.

However, if you want a solution that works for all printers, you can't rely on the printer knowing about PDF.
[ June 05, 2008: Message edited by: Peter Chase ]
 
Campbell Ritchie
Sheriff
Pie
Posts: 49841
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Val Perumal:
Printing a PDF file from Java can be achieved using JPedal . . .
Thank you very much, and welcome to JavaRanch
 
Vikas Kapoor
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Val,

Thanks for sharing this code.

Do they provide any kind of documentation(PDF/online material) with example or explanation for JPedal API so that we can explore other utilities also. I ain't asking for API doc. Please share the link if you have.
Thanks.
 
Nikhil Sun
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Joe...were you finally able to print out the pdf document as it is?Because I face the same problem as you...I try to print a text/gif/pdf file and all I get is junk characters as output instead of the proper content.Kindly help.
 
Joe Ess
Bartender
Posts: 9313
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nikhil, Please Use One Thread Per Question.. You have two other posts on this same topic here and here. This duplicates the effort of the volunteers at the Ranch and wastes people's valuable time.
 
Pierre Antoine
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are a few libraries specialized in printing PDF documents with Java.

For instance jPDFPrint allows you to print PDF documents with 2 lines of code.

PDFPrint pdfPrint = new PDFPrint (fileName, null);
pdfPrint.print (new PrintSettings

Even though some libraries pretend to be free, most require a license (including jPedal).

[ September 11, 2008: Message edited by: Pierre Antoine ]
[ September 11, 2008: Message edited by: Pierre Antoine ]
 
Joshim Ahmed
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a further question on this:

I have a program for printing a pdf file that was generated using the iText 2 package.
The file is created as an A4 document.
The program is able to load the file and send the print job - it reaches the printer, but on the printer the control panel prompts the user to load A4, Plain Paper. The print job will come out if the user presses "OK" on the printer to override and print to whatever paper is loaded.
I need it to print directly without user intervention, as the same file prints directly from Adobe Reader without this problem.
I also need it to print 2 copies and it only prints one.

Here is the code for creating the print job:



I am running this on a Windows 7 32-bit machine.
Printer model is HP LJ P3015.
The printer is hosted on a Windows Server 2003 machine.
I have set the printer defaults to:
Paper size = A4
Paper source = Auto
Paper Type = Unspecified

In the printer setup I have selected the paper size and types to Auto and unspecified.

So in PrintRequestAttributes or DocPrintJobAttributes how do I make sure
my print job is not trying to override the default paper type?
 
Joshim Ahmed
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Somebody suggested using a command line utility:
http://www.coderanch.com/t/520635/java/java/Java-Silent-Print-PDF-file#2358247
Is this the best solution?
 
Mick Branks
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adobe reeader not designed for automated printing and has no extra features such as setting up a printer propertys, scalling page, page count, and other print options. I would recommend command line tool CLPRINT who has a lot of options for PDF printing. you can visit http://www.commandlinepdf.com/
 
Jay Orsaw
Ranch Hand
Posts: 356
Android Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didn't read anything posted, but I have a program I downloaded called "Cute PDF." It basically created a printer to select when printing, and makes a PDF out of it. If you cannot do it via Java, I suggest downloading it, because it works and unless you need something for Java only to do... Goodluck!
 
Abhyuday Reddy
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello John,
Were you able to print the pdf file?
Even I face the same problem. The same tonns of junk characters.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49841
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

You may have a long wait for a reply; John Smith hasn’t posted for four years.
 
Abhyuday Reddy
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the welcome Ritchie!

I could get the issue fixed with PDFRenderer.jar.
This provides PDFFile, PDFPrintPage to load the pdf file and read it with ByteBuffer and print the DocFlavor.INPUT_STREAM.AUTOSENSE.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49841
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done sorting it out
 
Marianandan Arockiasamy
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joe Ess wrote:This code works for me (adapted from the example in the java api)



The only real difference I see is that the code above passes a PrintRequestAttributeSet instance to the job.print() call.
As Ulf mentions, this code is dependent on your printer being able to direct-print a PDF. My LaserJet 4250 has this capability.
[ April 24, 2008: Message edited by: Joe Ess ]




Hi Joe Ess,

I tried your code and it works fine.
Thanks a lot!

Below is my version of code:

package com.avery.asb.util;
import java.io.*;
import javax.print.*;
import javax.print.attribute.*;

public class PrintTest{
public static void main(String args[]){
FileInputStream psStream = null;
try {
psStream = new FileInputStream("mypdffile");
} catch (FileNotFoundException ffne) {
ffne.printStackTrace();
}
if (psStream == null) {
return;
}
DocFlavor psInFormat = DocFlavor.INPUT_STREAM.AUTOSENSE;
Doc myDoc = new SimpleDoc(psStream, psInFormat, null);
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
PrintService[] services = PrintServiceLookup.lookupPrintServices(psInFormat, aset);
// this step is necessary because I have several printers configured
PrintService myPrinter = null;
for (int i = 0; i < services.length; i++){
String svcName = services[i].toString();
System.out.println("service found: "+svcName);
if (svcName.contains("my local printer name")){
myPrinter = services[i];
System.out.println("my printer found: "+svcName);
break;
}
}
if (myPrinter != null) {
DocPrintJob job = myPrinter.createPrintJob();
try {
job.print(myDoc, aset);

} catch (Exception pe) {pe.printStackTrace();}
} else {
System.out.println("no printer services found" );
}
}
}
 
Abhilash Jaiswal
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to print .afp file ?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic