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

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.

<aside><strong>What is Codename One?</strong> Codename One is an open-source framework for building native iOS, Android, desktop, and web apps from a single Java or Kotlin codebase. Learn more at <a href="https://www.codenameone.com/">codenameone.com</a>.</aside>

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:

```java
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 things that flow, like an invoice with sixty lines. You add paragraphs and tables, and it breaks pages, repeats table headers and runs a decorator on each page for the footer:

```java
PdfLayout layout = new PdfLayout(doc, PdfPageSize.A4, 48);
layout.add(new PdfParagraph("Invoice 1042", PdfFont.HELVETICA_BOLD, 22).setSpaceAfter(12));
layout.add(new PdfParagraph("Payment is due within ", body, 11)
        .append("30 days", PdfFont.HELVETICA_BOLD, 11)
        .appendLink(". Contact us", "mailto:billing@example.com", body, 11, 0x1565c0));
PdfTable items = new PdfTable(4, 1, 1.5f).setHeaderRows(1).setFont(body, 10);
items.addCell("Item").addCell("Qty").addCell("Price");
layout.add(items);
layout.finish();
```

Hebrew and Arabic work too, with right-to-left layout and Arabic letter shaping. Plenty of PDF libraries still print Hebrew backwards.

## The same code on the server

Nothing in the package touches the UI, so your [Codename One backend](https://www.codenameone.com/blog/multi-tenant-saas-backend-in-java/) generates documents with the same classes. Return `doc.toByteArray()` as `application/pdf` from an endpoint, or mail statements from a scheduled job.

![Diagram](https://mermaid.ink/img/Zmxvd2NoYXJ0IExSCiAgICBMW0ludm9pY2VMYXlvdXQuamF2YTxici8-c2hhcmVkIG1vZHVsZV0gLS0-IEFbQXBwPGJyLz5vZmZsaW5lIHJlY2VpcHQgb24gdGhlIHBob25lXQogICAgTCAtLT4gQltCYWNrZW5kPGJyLz5tb250aGx5IHN0YXRlbWVudHMgYnkgbWFpbF0KICAgIEEgLS0-IFZbUGRmVmlld2VyXQogICAgQiAtLT4gVgogICAgViAtLT4gUFtQcmludGVyLnByaW50UERGXQ==?type=png&bgColor=ffffff align="center")

That's where the money is. The class that lays out an invoice is written once and shared. The technician's tablet makes the PDF on site with no signal, and the server makes the identical PDF for the accounting archive. Nobody keeps two templates in sync.

## Viewing a PDF, the same way everywhere

`PdfViewer` renders PDFs in pure Java:

```java
PdfViewer viewer = new PdfViewer(pdf);
Form form = new Form("Invoice", new BorderLayout());
form.add(BorderLayout.CENTER, viewer);
form.show();
viewer.setPage(2);
```

![The PDF viewer showing a generated invoice](https://www.codenameone.com/blog/release-2026-11-20/pdf-viewer-invoice.png align="center")

It doesn't hand the file to the platform, so it doesn't inherit the platform's quirks. The same file draws the same pixels on iOS, Android, the desktop and the web, and our CI holds it to a screenshot on every one of them. It handles what real-world files throw at you: JPEG 2000, JBIG2 and CCITT scans, shadings, soft masks, TrueType, CFF, Type 1 and Type 3 fonts, form fields and annotations. `renderPage(index, scale)` gives you any page as an `Image` for thumbnails.

`PdfReader` extracts text and metadata, rebuilds files with broken cross-reference tables, and opens encrypted documents:

```java
PdfReader reader = PdfReader.open(data, password);   // RC4, AES-128 and AES-256
Log.p(reader.getTitle() + ": " + reader.getPageCount() + " pages");
String firstPage = reader.extractText(0);
```

Either the user or the owner password opens it. That matters for the bank statements, payslips and insurance documents your users actually receive, which are almost always password protected.

## And printing

```java
if (Printer.isPrintingSupported()) {
    Printer.printPDF(path, null);
}
```

That's it. Generate, show, print, with no per-platform branch.

## Everything connects

PDF on its own is a feature. PDF connected to everything else is what an enterprise buys.

A dispatcher plans the week on a [Gantt chart](https://www.codenameone.com/blog/field-service-scheduling-template/), and `SchedulePrinter.writePdf` turns the whole project into a paginated PDF, with the time scale and the task table repeated on every page, ready to mail to the client. A finance team filters a [DataGrid](https://www.codenameone.com/blog/a-million-rows-and-live-excel-formulas/) of a million rows down to one region and sends the result as a report. A manager signs in through the company's Okta with [SAML](https://www.codenameone.com/blog/multi-tenant-saas-backend-in-java/), sees only their own tenant's invoices, and downloads the month as one statement generated on the server. A [Kanban](https://www.codenameone.com/blog/diagrams-flowcharts-and-kanban-boards/) card for a repair carries the signed work order as an attachment.

That's the list every enterprise app needs on day one: single sign-on, tenant isolation, grids, schedules, boards, documents, printing. Normally it's six vendors, six licenses and six security reviews. Here it's one framework, one language and one test suite.

Codename One is great for an indie developer shipping their first app. For an enterprise developer it's fantastic, because the parts the enterprise demands are already there, and they already talk to each other.

Tomorrow: [Fieldline, a complete field service app](https://www.codenameone.com/blog/field-service-scheduling-template/): scheduling, dispatch, maps and an optimizer, ready to ship.
