JasperReports, export to PDF, blank new lines - java

JasperReports library seems to be miscalculating how many new lines the text field need to fit all its contents. In JasperReports Studio (in preview) everything looks fine, but not when exporting to PDF from my application.
I've read PDF FAQ from JasperReports and I tried setting PROPERTY_FORCE_LINEBREAK_POLICY in the report to true, but this didn't make any difference. I also experimented with various fonts, but then in runtime Java complained about missing font in AWT, so I guess my default font (SansSerif) is just fine.
When I print "english only" version of the report, then everything looks okay, because all texts are about a half in length, but when using longer texts (like in example below) then this line miscalculation happens.
Here is how it looks right now and I marked unwanted extra lines with red:
Since I've tried with both hints from JasperReport's PDF FAQ, what else can I do?

I found the solution:
Switch all fonts to Dejavu (I used to use default fonts)
Have a jasperreports-fonts extension included in the project.
The new font is a tiny bit wider, but that's okay. Other than that it looks very alike.

Related

Java: How to pretty print .java file

I have a JTextArea, and I have this being populated with a chosen .java file, the problem is that the file is appearing as just normal text. I would like to add some pretty print so that the user will be able to see the file as if the file was open in eclipse (keywords different color, comments will be in grey... etc)
I have not been able to find any good examples online for how to do this. what would be the best way to do this?
EDIT:
I got this to finally work, i added the JSyntaxPane jar file to my classpath (jsyntaxpane-0.9.5-b29.jar to be exact) then i added the following two lines of code to get it working for my JEditorPane.
jsyntaxpane.DefaultSyntaxKit.initKit();
JEditorPane.setContentType("text/java");
This can also be used on the following languages: JavaScript, Properties, Groovy, C, C++, XML, SQL, Ruby and Python... just by simply changing text/java to text/LANGUAGE_YOU_WANT in the above code
JTextArea can only display plain text (like notepad in Windows). If you want to show pretty printed source code with colors, you need JEditorPane, which allows you to display HTML.
Now you can either generate HTML manually with syntax-highlighted Java source code or use some library.
See also
Where can I find a syntax highlighting library for Java?
JEdit-Syntax has a JEditTextArea class that will do syntax highlighting. This project was spun off to package some subcomponents of the JEdit project.

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.

iReport generates different PDFs than Web App

I have some troubles with JasperReports. I generated a formular with iReport including two subreports which generates a grid of values (1 or 2 Characters long).
The compiled PDF from iReport it works fine and looks good, but if i use the same *.jrxml and *.jasper files for my web app the generated PDF has some minor differences. One big problem is, that some cells of the grid now are 2 lines high. Values like "NB" only use one line but "GS" for example uses 2 lines.
For me it is not possible to find the error. Workarounds with smaller font size or wider cells didn't help.
Make sure the font you are using in the template is available on the JVM generating the report. If the font doesn't exist then a different font will be used. If changing the font isn't an option then you can create a font extension package. Creating a font extension is documented here: JasperReports Font Reference
Sound like you could have a different version of iReports in your web application. Making the cells sufficiently wide enough should at least allow the text to span just one line.
Create a Java Desktop test that generates a PDF based on the .jrxml and make sure it has the same results. If it does then there is something with the way iReports is working, if it doesn't then you know it is something with how you are viewing or creating the PDF in the web app.

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.

Using new fonts with Apache FOP in Java

I have to internationalize an application, and therefore have to let a user chose a font for a PDF that I will create at runtime. I want to be able to display a list of font choices from the user's system using
GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();
This works fine for getting fonts, but then it seems that I have to jump through some hoops to get that font to play nice with FOP, as outlined here. I can follow all the instructions that they've outlined, except I'm not sure how to get the location of fonts in order to run TTFReader.
Does anyone know how to list the path of each font that java gathers with the getAllFonts() command?
Is it a good idea to embed fonts in PDFs? For instance the Arial Unicode MS TFF file is ~ 30 MB.
Is this the only way to create internationalized pdfs with FOP and XSL?
There have been major improvements in font configuration with the last release (FOP 0.95), so please just consult the document there for a much easier approach:
http://xmlgraphics.apache.org/fop/0.95/fonts.html#basics
The XML font metrics files are no longer necessary.
To your questions:
That's impossible. Java won't give you that information. That's partly why FOP has its own font subsystem.
Yes, it's actually recommended. Please note that FOP supports font subsetting so only the glyphs needed are embedded in the PDF.
Not sure what you mean. With the above information and an upgrade to the latest release should make this simple enough.

Categories