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

7

6

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?

Pinebrook answered 28/8, 2011 at 17:34 Comment(0)
D
3

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.

Donoghue answered 28/8, 2011 at 22:10 Comment(0)
D
2

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.

Dauphin answered 28/8, 2011 at 17:38 Comment(4)
What's a pseudo printer? Possibly a dumb question but I'm not familiar with the term.Pinebrook
@Pinebrook A pseudo printer installs as if it is a printer, and appears to Eclipse as if it is a printer, but actually outputs to a PDF file. It is a slightly more elaborate version of "print to file".Auberon
Nice. Any recommendations for ones that you use?Pinebrook
This needs still some program to actually print the Java source code. (I suppose you don't want to append screenshots from your IDE to the PDF.)Axilla
H
2

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...

Hollis answered 28/8, 2011 at 17:50 Comment(0)
B
2

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.

Barrus answered 28/8, 2011 at 20:39 Comment(0)
P
2

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.

Proulx answered 27/2, 2015 at 18:28 Comment(0)
F
1

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.

Frank answered 28/8, 2011 at 17:54 Comment(0)
A
0

You could just copy & paste into OpenOffice/LibreOffice and export to PDF.

Amphibolite answered 28/8, 2011 at 18:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.