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
Marshal
Joined: Mar 22, 2005
Posts: 35254
7
posted
0
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).
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 ]
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
Joined: May 11, 2007
Posts: 13
posted
0
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
Joined: May 11, 2007
Posts: 13
posted
0
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 ?
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
Joined: Jun 05, 2008
Posts: 1
posted
0
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
Joined: Oct 30, 2001
Posts: 1970
posted
0
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 ]
Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.<br /> <br />#:^P
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32712
4
posted
0
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
Joined: Aug 16, 2007
Posts: 1374
posted
0
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
Joined: Nov 13, 2005
Posts: 108
posted
0
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.
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
Joined: Sep 11, 2008
Posts: 2
posted
0
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
Joined: Feb 03, 2011
Posts: 2
posted
0
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?
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/
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 Gurram
Greenhorn
Joined: May 04, 2012
Posts: 2
posted
0
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
Joined: Oct 13, 2005
Posts: 32712
4
posted
0
Welcome to the Ranch
You may have a long wait for a reply; John Smith hasn’t posted for four years.
Abhyuday Reddy Gurram
Greenhorn
Joined: May 04, 2012
Posts: 2
posted
0
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.