Import
Replace iText or PDFBox
See in your own Java lines how to replace iText or PDFBox with Brevier, for a PDF form that you fill and for code that draws a PDF, before you install anything.
On this page
A Java service makes a PDF with iText or PDFBox in one of two ways. It fills the fields of a PDF form, or it draws each text at an x and y position. This page shows the change for both. The code is from two compiled examples with made-up data: an insurance policy (the German Police) and a delivery note (the German Lieferschein). Each block names its source file in this repository.
| Your code | Section |
|---|---|
| Opens a PDF form and sets the value of each field | A filled form |
| Draws texts at positions and starts new pages by hand | Drawing code |
A filled form
The Police form is an AcroForm: a PDF with named form fields. The service sets a value for each field and flattens the form, so the PDF has no fields left.
Import the form
brevier import police.pdf -o templates/policeThe import reads the form and prints detected: AcroForm, 23 fields, 2 pages.
It writes police.html, police.css, police.schema.json and sample.json.
Each field keeps its name, so vn.name in the form is {{[vn.name]}} in the
template (the brackets keep the dot in the name). See the import overview.
Step 1: one .with(name, value) for each setValue
The code that loads the policy stays. Only the lines that call the PDF library change.
try (PdfDocument pdf = new PdfDocument(
new PdfReader(Paths.get("police", "police.pdf").toFile()), new PdfWriter(out))) {
PdfAcroForm form = PdfFormCreator.getAcroForm(pdf, true);
form.getField("policennummer").setValue(police.getPolicennummer());
form.getField("vn.anrede").setValue(vn.getAnrede());
form.getField("vn.name").setValue(vn.getName());
form.getField("deckung.glas").setValue(police.isDeckungGlas() ? "Yes" : "Off");
form.flattenFields();
}PDFBox has the same 23 setValue lines. Only the lines that open and close the
form differ:
try (PDDocument pdf = Loader.loadPDF(Paths.get("police", "police.pdf").toFile())) {
PDAcroForm form = pdf.getDocumentCatalog().getAcroForm();
form.getField("policennummer").setValue(police.getPolicennummer());
form.getField("vn.name").setValue(vn.getName());
form.flatten();
pdf.save(out);
}Replace the whole block with one chain. A check box takes the boolean itself,
so ? "Yes" : "Off" is not needed:
return Brevier.template("police")
.with("policennummer", police.getPolicennummer())
.with("vn.anrede", vn.getAnrede())
.with("vn.name", vn.getName())
.with("deckung.glas", police.isDeckungGlas())
.with("ort_datum", police.getOrtDatum())
.render();(The blocks above show some of the 23 fields. The source files hold all of them.)
Step 2: pass the objects
When step 1 runs, drop the .with lines. The client binds each object to the
template by its field names:
return Brevier.template("police").render(police, vn);The engine checks the data against police.schema.json when it renders.
OpenPDF
OpenPDF keeps the old iText API. This example has no compiled version of it, so the code below is not compiled:
PdfReader reader = new PdfReader(Paths.get("police", "police.pdf").toString());
PdfStamper stamper = new PdfStamper(reader, out);
AcroFields form = stamper.getAcroFields();
form.setField("policennummer", police.getPolicennummer());
form.setField("vn.name", vn.getName());
form.setField("deckung.glas", police.isDeckungGlas() ? "Yes" : "Off");
stamper.setFormFlattening(true);
stamper.close();
reader.close();setField(name, value) takes the name of the form field, as getField(name).setValue(value)
does in iText 8. Each setField becomes a .with with the same name.
What the PDF looks like
The check boxes and radio buttons of this form have empty appearance streams.
PDFBox sets their values and draws no mark. iText draws the mark as a line.
Brevier prints an X.
Drawing code
The Lieferschein service draws each text with showText at an x and y
position. Each table row is 4 calls. When y is below 80, the code adds a page
and draws the column titles again. There is no step 1, because drawing code has
no template to keep.
The code that draws
for (Position position : lieferschein.positions()) {
if (y < BOTTOM) {
PdfPage page = pdf.addNewPage(PageSize.A4);
out = new PdfCanvas(page);
y = header(out, bold, TOP);
}
text(out, plain, 10, LEFT, y, position.articleNumber());
text(out, plain, 10, 150, y, position.description());
text(out, plain, 10, 400, y, String.valueOf(position.quantity()));
y = text(out, plain, 10, 450, y, position.unit());
}for (Position position : lieferschein.positions()) {
if (y < BOTTOM) {
out.close();
page = new PDPage(PDRectangle.A4);
pdf.addPage(page);
out = new PDPageContentStream(pdf, page);
y = header(out, TOP);
}
text(out, PLAIN, 10, LEFT, y, position.articleNumber());
text(out, PLAIN, 10, 150, y, position.description());
text(out, PLAIN, 10, 400, y, String.valueOf(position.quantity()));
y = text(out, PLAIN, 10, 450, y, position.unit());
}The template from your classes
The codegen tool reads your classes and writes their JSON Schema. Then
brevier template --schema writes a starting template from the schema:
java -cp build/classes/java/main:brevier-codegen.jar dev.brevier.codegen.SchemaGeneratorCommand \
--root lieferschein=lieferschein.model.Lieferschein > lieferschein.schema.json
brevier template --schema lieferschein.schema.jsonThe second command writes lieferschein.html, lieferschein.css and a
sample.json. A List<Position> becomes a table with one column for each
property of Position. You edit the layout and the column titles.
The engine does the work of the y < 80 test. It draws the header row at the
top of every page. The CSS rule tr { break-inside: avoid; } keeps a row on one
page.
After: one call
Delete the drawing code. The service keeps only the Lieferschein object:
return Brevier.template("lieferschein").render(lieferschein);Next
- Replace the call: the same two steps for JasperReports and Word, in four languages.
- Java client: how to install the client and start the engine.