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.
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
Joined: May 11, 2007
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.
Joined: May 11, 2007
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 ?
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.
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.
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.
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!
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!