Is there a satisfying way of printing complex PDF-Files in Java - java

I have a grayscale pdf with a few images and Text to print inside of Java. The usual approach via rendering to a graphics object and sending to a printer per java.awt.print or org.eclipse.swt.print results either in a blurred output or takes several minutes to print (The rendering process is fast though).
I have already tried many opensource renderer (PDFBox, jpod, PDF Renderer) or commercial products (crionics JPDF, Gnostice PDFOne,...).
It seems all these libraries are using in some way the java.awt.print API and render the pdf File to the internal graphics.
I'm looking for a more direct approach, like converting the pdf to postscript (or another, for the printer more readable format) and then printing it directly, without the need of rendering it first).
It should work for most printers and on Win/MacOsX/Linux likewise :)

The trick I am using in one of my applications, is to execute an OS command with Runtime.exec(). It works fine on Mac OS X and Linux with the command lp . On Windows the command would be AcroRd32.exe /t , but the acrobat reader must be installed and the .exe file must be located somehow (by putting the directory in the PATH for instance).

Related

Chromium headless PDF generation (in Java) using String instead of temp file/page

We currently use the PhantomJS executable for two things in our Java project:
Create a PDF file from a given String html we get from our database (for which we write the String to a temp file first)
Create a screenshot of a given Widget-Object (for which we have an open HTML page on the front-end)
Since PhantomJS hasn't been updated for a few years, I'm about to change it to a headless Chromium method instead, which has the options --print-to-pdf and --screenshot for options 1 and 2.
Option 2 isn't really relevant since we have a page, but for option 1 it would be nice if we could directly use the chromium command-line with the given String. Right now with PhantomJS, we convert the String to a temp file, and then use the executable to generate the actual PDF output file. I can of course do the same with the headless Chromium executable, but since I'm changing it right now anyway, it would be nice if the 'String to temp HTML file' step wouldn't be necessary for creating the output PDF file, since we already have the page in memory anyway after retrieving it from the database.
From what I've seen, the Chromium executable is usually run for either a HTML file to PDF file:
chromium --headless -disable-gpu --print-to-pdf="C:/path/to/output-file.pdf" C:/path/to/input-file.html
Or for a HTML page to PDF file:
chromium --headless -disable-gpu --print-to-pdf="C:/path/to/output-file.pdf" https://www.google.com/
I couldn't really find the docs for the chrome/chromium executable (although I have been able to find the list of command options in the source code), so maybe there are more options besides these two above? (If anyone has a link to the docs, that would be great as well.)
If not, I guess I'll just use a temp file as we did before with PhantomJS.
The terms 'chrome read stdin' would probably have brought you to this question explaining how to read from a data url:
chrome.exe "data:text/html;base64,PCFET0NUWVBFIGh0bWw+PGh0bWw+PGhlYWQ+PHRpdGxlPlRlc3Q8L3RpdGxlPjwvaGVhZD48Ym9keT5ZbzwvYm9keT48L2h0bWw+"
Reading input from stdin sounds like you would also want to write the output to stdout: 'chrome pdf to stdout'. Which links to someone trying the same thing and running into issues about not being able to combine --stdout with screenshot or pdf output from 2018.
And (depending on the usecase) even worse, a limitation of the data url's of 2MB.
So if you can't guarantee the input to be less than 2MB you might be better off using files anyway, or check if the limitation has been removed.
Also, given that you specify that option 2 has a solution in serving the page directly, would that not also open up the option to do the same for option 1?
You should not need redundant -disable-gpu usure which version it was not needed for Windows, but redundant in https://chromium-review.googlesource.com/c/chromium/src/+/1161172 (2018), however, you may want to replace with --print-to-pdf-no-header to avoid those.
Your using windows as the shell to run Chrome/MSEdge.exe so for that reason there will be a significantly smaller command line ability to CMD pass a variable string.
To pass a base64 for html string as stdin will often be limited by content for similar string length reasons to say 1.5MB (75% of 2MB). Thus in special exceptional cases that may be 4096 pages see https://github.com/GitHubRulesOK/MyNotes/raw/master/Hanoi.htm however the norm is usually only a few standard html pages.
PDF file handling requires a file system to generate the pages, thus a file centred approach to store the decimal based file index. So the memory work around is to use a RamDrive/Disk or its Bytes IO equivalent as named FileStream object.
Using PDF data in memory is usually highly disk intensive as the limited resources after contents program processing need to draw on the bus disk cache to augment virtual ram. As a result working in memory can be just as slow if not slower than using cached disk file data.
%Tmp% / %temp% files can usually respond quicker and be very easily overwritten.
There are many other working and non-working switches bandied about the web, but the semi-official list is https://peter.sh/experiments/chromium-command-line-switches/

Writing icons to PE File Formats in Java

I'm in the process of writing a read/write library for Windows PE files using Java (and eventually hope to release it as OpenSource). For now though this is for cross platform compatibility and will allow me to modify strings/version info/icons/resources etc from an Ant build system or command line/terminal etc.
The problem I have reached is that I can now embed icons in the .rsrc section from a source BMP file and have it display them but unfortunately (Even though the data is correct) the icon comes out within Windows Explorer (and CFF Explorer/ Visual Studio etc) as incorrect. It has a sort of pattern across the raster data as if the image something has be Xor'd or digitally signed.
Any ideas what i'm doing wrong when adding this data?
On a side note I'm also a bit confused as to why I need to modify the height part of the BMP header so that its marked as 2x its real size.
Thanks In Advance.
- Tim.

How can I pretty-print Java source code as a PDF?

I'm planning to put some Java code in an appendix to my report. The report is a PDF document, and I use Eclipse for Java.
How can I present it best and do this easily? Any recommendations?
For this purpose, I created a LaTeX doclet. This is a Javadoc doclet, which converts the javadoc comments to LaTeX code, and (if wanted) also includes a pretty-printed version of the source code of the documented methods.
You can then convert the generated LaTeX document to PDF, and append it to your report.
If you use Windows, install CutePDF. This adds a "Printer" that when you print to it it asks you a file name and then prints the output to a .pdf document on your hard drive - hence it is a psuedo printer - it acts like a printer, but is really a pdf file writer.
Don't know solutions for other o/s...
I usually prefer to install a PDF "psuedo" printer in whatever OS I am using. That way I can use the print facilities of whatever app I am using (like Eclipse for example) and get the result in PDF file.
EDIT:
Here is one example of a pseudo printer, this for the Windows platform. Mac OS X has a built in "print to PDF file" capability.
You can use doxygen to generate documentation for your project which can include a formatted source file listing in addition to Javadoc. doxygen can generate both HTML and PDF output. You'll need latex to generate the PDF output.
Another way to pretty print is with IntelliJIDEA. It works also with the community edition.
It's advisable to install a PDF printer, in order to try printouts without wasting a lot of paper. Once you're satisfied with the result, you can print on the real printer. On Windows you can use CutePDF, on Linux Ubuntu install the package cups-pdf with sudo apt-get install cups-pdf.
Note that IntelliJ prints the theme's background, so it's advisable to be on a white background to avoid wasting ink.
To print click on menu File -> Print. The printer selection is in the next menu, after you press on the Print button.
Interestingly you can also print only the selected text, which is useful if you don't want to print import statements.
Other options include the possibility to add line numbers, syntax highlighting and colour printing. On Linux IntelliJ 14.0.3, the default font was a huge size 14, so you might want to change that too.
You could just copy & paste into Word (2007+) and save as PDF. It's a little more straightforward than the file printer, and you can format your code for best results in Word.
You could just copy & paste into OpenOffice/LibreOffice and export to PDF.

PDF generated with jasperreport not showing well on Linux but yes on Mac, could the os be related?

A PDF I generate with jasper reports renders Ok in my MAC but some labels show wrong on Linux. For example, I have a static label that doesn't show completely on linux (only a part of the whole word) but yes on Mac. Can the OS be somehow related? What is the usual source of this kind of problems?
Missing fonts on your linux machine may cause such problems. I had the same when creating pdfs with iText.
Always embed fonts in generated PDFs! It saves you alot of hassles...
And notify the following slight difference: If you create a PDF with Arial as font it shows good in Windows, and will very likely use Helvetica on Linux (which is nearly the same font), but since it has some other metric properties your bounding boxes will not fit.
Again: Always embed fonts in generated PDFs!
If you have time you can look at Docmosis, it generates PDFs with the fonts embedded, so cross-platform rendering isn't an issue. Beware though if you cross-platform generate doc or odf files, then the fonts will be potentially different showing up in pagination or layout changes. Like has been said above, you need to take care that the destination will have the fonts to display those that were used to generate the document (or embed the fonts if possible).
The closes fotn to the PDF Helvetica (SansSerif) is MS Arial.ttf. The problem is that it is not available on the Linux machine by default. Copy it to the /usr/share/fonts (and update fonts.dir) or put it to your Linux JRE installation to fonts/ directory (and update fonts.dir). If you do not want to use MS Arial, try GNU FreeFont http://ftp.gnu.org/gnu/freefont/
You can embed the fonts in the PDF as Daniel has adviced, but it makes PDF larger.

Generate PCL from PDF in Java

What is the best way to generate a PCL output file from an existing PDF file in java?
It depends on how much you want to invest, and how robust the solution needs to be. For quick and dirty, you can print from Adobe Acrobat to a file, using a PCL driver (look, mom, no Java ...).
The Java Print Service API can process PDF. Use StreamPrintService and write the stream to a file, using PCL for the output format.
If you need to have more control over the content, maybe modify it or add to it, you can use a PDF parser (this one, for instance) and print the resulting HTML from a browser that your application starts, by adding some Javascript, for example.
The StreamPrintService from JDK 6 does only support PS. I am still searching for a StreamPrintService which supports PCL.
We capture PCL generated from Acrobat printing a PDF to a PCL driver and redirect as input to our Windows console PCLXForm program. With a custom script, we can "stream edit" the PCL. We can extract the address block text for address correction, insert the corrected text, add the Intelligent Mail Barcode, 2-D barcodes, sort the documents, batch them by page count, change tray assignments, merge with other documents, etc. The product required is PCLTool SDK - Option V at www.pagetech.com

Categories