I am preparing an acceptance testing tool for java web start application. That app autoprints pdfs to system-installed printers and I want to verify if produced pdfs are correct. To do this I am looking for a way to mock printing.
I found a way to register a custom printing service before the test:
PrintServiceLookup.registerServiceProvider(fakePrinterService);
And I know how to select that fake printer service in my tested application.
Unfortuately I found no implementations of javax.print.StreamPrintService that I can use to print to a file except of sun.print.PSStreamPrintService, which prints to postscript. It also seems to be a part of internals of the JDK, not a public API. Works but leaves some doubts.
Are there any other java printers implementations? It would be best if it could print a pdf.
If not I guess I will go with PSStreamPrintService and ps2pdf conversion.
Related
I'm working on an existing Java web application (HTML/CSS/JS/JSP/Servlets and Java classes in this particular app) that currently uses an applet to print checks.
My boss recently came to me and informed me that there are errors coming back on user's machines when testing the check printing against the latest versions of Java.
He is wondering how we could set up the application to print checks off without using an applet.
In the past, I've used Crystal Reports to lay out forms and print them but that was in asp.net.
I know there are Java PDF libraries available but I'm not at all familiar with any of them and not sure that they could be used to format and print checks in a Java web application.
So, I'm ultimately wanting to know about what has worked for those who have implemented check or form printing using Java/JSP/Servlets.
2012-02-24 # 13:15EST edit
I mentioned "Java PDF libraries" above but have since found out that PDF cannot be used as end-users should not be able to save the check documents (unless PDF's can be made to not be saveable and just printable). All of the data is managed right on the database (Oracle in our case).
I've used iText to create PDF files before for things like this. PDF is your answer, since the whole point of the format is that it never really changes. Much better than an Applet.
http://itextpdf.com/
I ended up digging deeper into using iText and came across flying-saucer which makes it super-easy to render a PDF from XML or XHTML.
Check it out at http://code.google.com/p/flying-saucer/
I also found out how to partially hide the save functionality by rendering the PDF inside a hidden iframe: Create a "print-only" PDF with itext
I need to implement a custom printer driver which does the following in the backgroud:
Create a PDF document of the document to print
Send the created PDF document to a predefined email address OR
call a web service and transmit the document there
The pinterdriver should be available for windows and mac osx. My prefered implementation language is Java.
Is this possible with Java?
Are there frameworks available which reduce the coding effort to a minimum?
Are you sure "driver" is really the word you are looking for here? Usually when one says a print driver they actually mean something that translates document data into commands for a printer. I think you are really looking for something of a pre-processor.
In any case, if you want it to look like a printer to the OS, it will be almost impossible to do purely in Java. Your best bet would be to create drivers for each of the platforms that use JNI(or just invoke a JVM), do your processing, then forward it on to the printer.
Although I do not know if a similar approach will work on Windows, what you want to do is almost trivial to do on OS X. Apple already provides a cups-pdf service that converts any printable document into a pdf, all you have to do is take that output, forward it to where it needs to go, then forward it to a printer, no need to do anything in Java.
For an example of how to do this, check out the following project:
https://bitbucket.org/codepoet/cups-pdf-for-mac-os-x/downloads
Windows can be configured to send printer output to a file. You can create a printer in Windows that uses a PostScript driver, and writes it to c:\myfile.txt
In Windows: Add a Printer, Select Local printer, Select Create a new port, and type the file name (Full path) you want to use. Then pick the driver you want, which your Java program will have to parse. Generic text could be useful in some cases, or Postscript if you need all that formatting, and can handle parsing it.
Unlike the "File:" option under existing ports, it will not ask the user for a filename. It will just automatically save to the specified file every time.
Your Java program can monitor this file for changes, and then process the data it receives.
I have a generated post script file and want to print using it. How can it be achieved in java either using javax print API or AWT. Is it possible?
Complicated. Does your printer(s) support PostScript? Is it networked? If so, most networked printers can talk LPR and you can shove the file over as-is. On Windows, you could also stream the file as-is to the lpt1: mapped port via something like NET USE LPT1: \\[Computer Name]\Printer /PERSISTENT:YES.
If you're on a server and you do lots of PostScript handling and your printer infrastructure supports it, I would very much look into the LPR protocol. I've written several LPR/LPD management functions in Java to handle printer jobs, so definetely know it can be done with some relative ease.
http://tools.ietf.org/pdf/rfc1179.pdf
Have done some research into this topic, but found no relevant answers. What I need is to print a number of PDF files on one of three forms, which are loaded into different trays of a particular printer. I need to specify which printer to use and it's not the default printer. Additionally, I need to specify which tray to use based upon an attribute of each PDF file and be able to switch between them at run time. Java PrintService seems to only be interested in the local default printer. I'd appreciate any suggestions on how to accomplish this task. Thanks.
The Printer API does allow for talking to different printers other than the default. That being said it is limited. What we did in our shop is to write a JNI layer that talks directly to the Print Queue of Windows and we use that. If you want finer control than the PrintService API provides you will need to write a JNI layer and access that from Java.
You could configure a different printer for each tray in your OS, Then print to that printer depending on the properties of your file.
Thanks for all the suggestions, but I think I have worked this out now. I'm using LPR for the printing and org.apache.commons.net.ftp.FTPClient for the tray switching commands. The actual tray commands are in text files. My tests (so far) have been successful and I did not have to install the printer on my workstation.
I have used perf4j to calculate the performance of the process. Now I want to make the graph for that file. Is it possible to make it without using Google API or any other method that can be used?
You have a couple of options:
When you use the org.perf4j.LogParser to parse your log, you can use the "--format csv" option to generate CSV output, open this up in Excel, then create a graph in Excel.
As the other poster said, I'm not sure why you're against using the Google Chart API, but if you are dead set against using it, note that the JFreeChart guys provide an implementation of the Google Chart APIs called Eastwood Charts: http://www.jfree.org/eastwood/. You could just set up an eastwood server and then proxy (or just set up in a hosts file) chart.apis.google . com to your eastwood server. Alternatively, if you look at the org.perf4j.LogParser class, you can override the newMeanTimeChartGenerator and newTpsChartGenerator methods to call the GoogleChartGenerator constructor that takes the base URL.
You can always create another implementation of the StatisticsChartGenerator interface and use this instead. Obviously this is the most involved option.
Can you explain a bit more as to why you want to avoid Google Charting APIs (I ask since I am intrigued as to why someone would not want to do something that is very simple, elegant and FREE)?
Also there is a GraphingServlet section "Exposing Performance Graphs in a Web Application" which I think internally does the same thing.
If you still want you can take the graph data (in raw csv format) and write some helper classes to create charts using libraries like jfreechart (rich client)
follow the following steps;
Go to the location where your perf4j log file exits and open command prompt there.
Hit the following command in cmd, make sure you have java(jdk) path set as environment variable and perf4j jar on this location:
java -jar perf4j-0.9.16.jar --graph perfGraphs.html perfLogFile.log
this will generate perfGraphs.html file in this location, which consists of graphs.