Technology1 min read

Invoices, Tickets and Reports: Generate, View and Print PDFs From One Java Codebase

By · Published by Everything Blog

In short

The library introduces dual generation approaches, offering a canvas interface for fixed designs alongside an automated layout engine that handles page breaks and recurring headers. Because the document creation logic operates independently of any user interface, developers can run identical generation routines offline on mobile devices or within backend server pipelines. Furthermore, the included pure Java viewer consistently displays complex elements like annotations and form fields while shielding users from operating system rendering discrepancies. A built-in reader also equips applications to parse text, repair damaged cross-reference tables, and decrypt protected files.

Key points

  • The package unifies document creation, viewing, and printing into a single Java codebase…: The package unifies document creation, viewing, and printing into a single Java codebase spanning client and server environments.
  • Generation code functions completely detached from the user interface, enabling seamless…: Generation code functions completely detached from the user interface, enabling seamless operation offline on mobile devices or in backend workflows.
  • Printing is executed using a single command that eliminates the need for platform-specifi…: Printing is executed using a single command that eliminates the need for platform-specific conditional code branches.

Sources checked by our analysis pipeline: 1

Every business app ends at a PDF. The invoice, the boarding pass, the signed work order, the monthly statement, the report the regional manager prints and brings to a meeting. And every team I know solved it the same painful way: a server-side library for generating, a different viewer per platform, a web view on Android that renders differently from the one on iOS, and a print button that works on one of them.

Now one package does all of it, on the phone, the desktop, the web and the server.

Writing a PDF

com.codename1.pdf

has two ways in. The canvas is for things with a fixed design, like a ticket:

PdfDocument doc = new PdfDocument().setTitle("Ticket");

PdfPage page = doc.addPage(PdfPageSize.A5);

PdfCanvas c = page.getCanvas();

c.setFillColor(0x1565c0).fillRect(0, 0, page.getWidth(), 60);

c.setFillColor(0xffffff).setFont(PdfFont.HELVETICA_BOLD, 20).drawText("Boarding pass", 16, 38);

c.setFillColor(0x212121).setFont(PdfFont.HELVETICA, 12).drawText("Gate B12, seat 14C", 16, 150);

byte[] pdf = doc.toByteArray();

Paths, curves, clipping, transforms, alpha, embedded TrueType fonts, JPEG and PNG images, links and bookmarks are all there. Anything you draw on a mutable Image

can go into a document as pixels.

The layout is for…

Technology