Outpuf PDF as a stream for calling process - java

I'm using jasperstarter (a java program, open source) to generate reports that I define in iReports. This runs in windows and I need the output PDF generated to be directly read by the calling process which is a C# program.
Jasperstarter has lots of options like viewing the PDF generated directly, or generating the PDF as a file, sending it to printer, but no option to stream the result PDF to the calling process.
Is this possible at all, even if I have to change jasperstarter's open source code? How?
Is this possible in some other way that doesn't imply changing the jasperstarter's code? How?

Actually the answer was quite easy. I've downloaded jasperstarter and was able to use JasperExportManager.exportReportToPdfStream method which already existed on jasper's library to export the pdf to "System.out" stream. Together with adding a new option to run this process, this makes jasperstarter to be able to allow for redirection of the PDF's result (pipe '|' and redirect can be used directly on the program).
I tried contact with jasperstarter's project owner to see if I can get this change commited on the master branch.

Related

A library I am using is opening an input stream and not closing it. How can I close it?

Every few days, I get a SocketException, too many files open. I tracked the issue to a temporary pdf file that is being used during a certain process. The process passes a name of a temporary file that the library creates. At some point, the library opens an input stream but doesn't close it. Given that my code only has the name of the file, is there any way for me to close the stream?
Details:
Java Web App running in Tomcat6
The best approach is to request a version of this library with this bug fixed.
If this is not possible, get the sources, fix the bug yourself.
If you can't (only a binary jar file), try a tool like jd-gui, decompile the faulty class, fix, recompile that class and replace the .class in the jar.
If it still does not work use ASM and add a close statement at the right place. THIS SOLUTION SHOULD BE AVOIDED. It's complex if you do not master this technology.

Open PDF to a specific page

Is there a way to go to open a specific PDF-page with Java?
It can either open the page in Adobe Reader or a built in reader in Java.
I have the file path and the file saved locally.
It looks like you have a couple of options here.
The easiest way to do this on Windows would be using the command line. Both Acrobat and Reader take command line arguments such as zoom level and starting page. The command to open a document on a specific page on Windows looks like this:
Acrobat.exe /A "page=1000" "C:\example.pdf"
And here is a reference of all the parameters Acrobat and Reader take on Windows - Parameters for Opening PDF Files.
On Mac, things get a bit tricky. Neither Acrobat nor Reader take command line parameters there. However, you still have some options on the Mac. Your best option would be to use AppleScript. Acrobat has extensive support for it, it's fairly easy to get started, and you can even import the AcrobatLibrary and see a list of available API. Here is a good article to get you started on this. As with Windows, it looks like this is both available for Acrobat and Reader.
I've put together a small script that should open a document and then go to a specific page.
tell application "Adobe Acrobat Pro"
tell PDF Window 1
goto page 3
end tell
end tell
Here is a good way to open the newly created AppleScript from Java.
You can add a "go-to" PDF action to the document's "open" viewer application event. Here is how you do it with our company product PDFOne (for Java). You might be able to do the same with other PDF libraries.
http://www.gnostice.com/nl_article.asp?id=217&t=Trouble_free_Linking_To_PDF_Pages_Online
Sample PDF: http://www.gnostice.com/newsletters/downloads/2011_03/PDF_that_skips_to_page_3_by_default.pdf
I think this is what you are asking for...
Say you have a document that has several pages of minutia at the beginning (perhaps a bunch of legalese that you’re not going to read the first time, much less every time). Rather than scrolling past all that minutia every time, you want it to go straight to the table of contents so you can find what you’re looking for more quickly.
If that sounds similar to what you are wanting, then here you go…
(By the way, I’m using Adobe Acrobat Pro. I don’t know if this will work for Reader.)
Open the document in Adobe.
Click Enable All Features.
Click File.
Click Properties.
Click on the tab called Initial View.
Find where it says “Open to page” and enter the page number you want it to open to.
Click OK.
Save the file.
Close the file.
Reopen the file. (It should open to the page you set.)

How to store a file in Java, which is generated on execution of a .exe file?

I have a .exe file, which produces certain files when made to run :
The files produced are WatchDataTest, ngrtgs.test, shubhangi, slatey (as they appear in the image)
I want to run the .exe file through a separate Java Program and obtain reference to the above files. How can this be done?
My point of view: I think, obtaining an OutputStream(wrapped by ObjectOutputStream) on the Process object of this executable can be used to read the objects (files, in this case). However, I am not sure in what way does this executable provides reference to the files produced. Other than that, I have a confusion whether the GUI display is part of the output. I mean does the OutputStream of this executable include the GUI object, which displays on the screen? If not, what all is the output of this .exe?(Pretty confusion here)
The .exe file calls your OS native functions to create those files. You cannot catch that from Java.
If you want to read the content of those files from Java, find them in the directory structure, and open them for reading with the normal Java File I/O API.
I think you want to access those certificates, right? Most likely they're not stored in separate files, but in one file called keystore. In this case I recommend to use Java PKI API or tools to manage your keystore.

Convert DGN to PDF

How can I convert MicroStation (DGN) files to PDF via command line?
The OpenDesign Alliance has libraries for dgn and dwg with pdf capabilities.
You could could use them to create the command line utility.
Decision Graphics http://www.dgnlink.com/ has a number of products that convert DGN to DWG, all of which can be run from a command line.
Once you have the DWG files, you can use one of the command line DWG to PDF converters (I can't recommend a specific one but a quick Google will find loads), or if you have a copy of AutoCAD or AutoCAD LT, you could write a script for that to convert the DWGs by using the DWG To PDF plotter.
From command line parameters, you can not do it. But you can create a Visual Basic or VB.NET application that will lunch microstation to do it for you.
I have been struggling with this one for a long time. All of the existing tools out there require expensive per-user licenses, which was a no-go for my implementation.
I solved the issue with a combination of three things:
Downloaded Bentley Viewer. This is their 100% free viewing/printing application.
Set up a virtual Windows postscript printer using GhostScript and RedMon. There are various guides online for this, and it can be a bit tricky to track down all of the prerequisites, but it is quite stable once set up correctly. Here is one guide: GhostScript/RedMon Guide
Use a "Key-In" script to pass print commands to Bentley View. This involves simply creating a text file with the required commands (List of Commands) and passing it AND the file you want to convert on the command line as such:
BentleyView.exe -M [Filename.dgn] -S[KeyIn FileName]
Please note that you need a space between the -M and the DGN filename but NOT between the -S and the Key-In script filename.
The script I used to simply print the document and then quit the GUI was:
PRINT PRINTERNAME PDFWriter
PRINT EXECUTE
QUIT
This is not a perfect solution at all, especially since it requires the installation of Bentley View and it also opens a UI when called (although it does close it immediately after printing).

PDF to PCL conversion

In my Java application, I have PDF files that I eventually need to convert to PCL and send to a RightFax server. I'll also need to embed codes in the PCL files that RightFax will read to know where to send the fax.
What's the best approach towards doing this?
Searching online, it seems like I could use Java's StreamPrintService to print the PDF files to PCL. Is this correct? Does this also mean that I must have installed on my OS a printer that can interpret PCL?
Once the PCL file is generated, I need to add the embedded codes in the file. Do I add the codes to the end of the file (by opening it in Java and writing out to it)?
The simplies solution, IMHO, is to drop the PDF into a folder on the RightFax server. Then create a small text file with all the instructions for who to send the document to etc. using Embedded Codes or FCL (Fax Command Language). We do this all of the time and it works great. Note: Fax Command Languate is only available if you have the Integration Module. Both Embedded Codes and FCL each have a command to attach a file(s). Once RightFax receives this text file it will process the commands and attach the PDF and fax and/or email the document. Here are two examples (one Embedded and one FCL).
Embedded Code File:
<TOFAXNUM:999999999>
<TONAME:Douglas Anderson>
<BILLINFO1:12345>
<NOCOVER>
<WHO:DOUG>
<ADDDOC2: C:\pdfFiles\12345.pcl>
FCL Code File:
{{begin}}
{{fax 999999999}}
{{contact Douglas Anderson}}
{{billinfo1 12346}}
{{nocover}}
{{attach C:\pdfFiles\12345.pcl delete}}
{{imagetype pdf}}
{{end}}
Sending this simple text file to RightFax will prompt it to process and insert the document you specify. There are vaious ADDDOC commands and switchinges for ATTACH that tell RightFax to delete the file once it has been sent etc.
The Embedded Code File can be sent in via the HPFAX queue and the FCL can be sent in via the Production Inbox (c:\program files\rightfax\production\inbox).
This gives a lot of control and allows for easier troubleshooting as you still have a PDF that is viewable (due to the fact that you didn't stick text at the start of it), and you can easily output the Embedded Code or FCL files to an alternate folder for viewing and even modifying with simple tools like Notepad.
Edit: OpenSource is correct that you can concatenate files together, I haven't done this with Embedded Codes for a long time (see example at end) but have done something similar with FCL (if you have the Ingetration Module you can do this).
FCL with PDF or Postscript embedded in data (RightFax treats PS and PDF):
{{begin}}
{{fax 999999999}}
{{contact Douglas Anderson}}
{{billinfo1 12346}}
{{nocover}}
{{beginpostscript}}
%PDF-1.3...
...your pdf...
{{endpostscript}}
{{end}}
The PCL variant looks like this:
{{begin}}
{{fax 999999999}}
{{contact Douglas Anderson}}
{{billinfo1 12346}}
{{nocover}}
{{beginpcl}}
...your pcl data...
{{endpcl}}
{{end}}
False first page with Embedded Codes (as per my notes from something we did a long time ago):
<TOFAXNUM:999999999>
<TONAME:Douglas Anderson>
<BILLINFO1:12345>
<NOCOVER>
<WHO:DOUG>
<DELETEFIRSTPAGE>
*PCL formfeed character*
...your pcl data...
Whatever you send will appear on the 'first page' but this will be deleted. The other option is to send this data after the Formfeed a the end of the document and use the <DELETELASTPAGE> option. This data can also appear inline with the PCL file itself and as such you may be able to send it at the start of the job without the <DELETEFIRSTPAGE> command and the formfeed splitter.
We have a very similar process. What we do is we have a pcl file and a control file (a text file with the rightfax instructions in it). We concatenate these two files using java NIO and send it across to the rightfax print queue. We basically create a new file and write the above two into the new file using the transferFrom() method in the channel which is got by stream.getChannel(). We put control instructions at the top not at the bottom as you have mentioned? may be you misstated it - I think Rightfax needs it at the top. I have to admit I have not tried to send it at the bottom. May be it will work just dont know.
JPS lets you print to file so you just need to print to file on a PCL printer.
print-to-file seems to be the right approach here.

Categories

Resources