Date post: | 21-Oct-2014 |
Category: |
Technology |
View: | 113 times |
Download: | 4 times |
Copyright © 2013 Roots nv, Belgium - All rights reserved
iText @ Car-PassHow to use PDF Templates with iText.
Copyright © 2011 Roots nv, Belgium - All rights reserved
Koen Dehaen
Managing Partner
@ Roots Software since 1997
Car-Pass and Johnson & Johnson
Enterprise Services Architect
Copyright © 2011 Roots nv, Belgium - All rights reserved
Roots Software
Deliver business driven, sustainable and customised solutions
Based on Service Oriented Architecture
Projects managed with an Agile approach
!Founded in 1994
Antwerp, Belgium
35 associates
Works for Car-Pass, Janssen Pharmaceutica, Johnson & Johnson, DHL, Achmea, Sanoma Media Belgium, Sapa, Syngenia (GDF Suez), …
!www.roots.be
Copyright © 2011 Roots nv, Belgium - All rights reserved
What brings us here?
Our experience with iText (since version 2), iText used to create simple & complex PDF’s from scratch re-organise & watermark existing PDF’s embed QR codes, images, … complete PDF Templates
directly from the java code, no need for extra tooling
The Car-Pass IS v2 project
Copyright © 2013 Roots nv, Belgium - All rights reserved
how it all startedcreate a PDF from code
Copyright © 2011 Roots nv, Belgium - All rights reserved
Code - Hello World!
!Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(“/tmp/hello-world.pdf”));
document.open();
document.add(new Paragraph("Hello World!"));
...
document.close();
!
http://itextpdf.com/book/examples.php
Copyright © 2011 Roots nv, Belgium - All rights reserved
Some real-life examples
Copyright © 2011 Roots nv, Belgium - All rights reserved
Our findings
Advantages 100% java, 100% cross-platform No external tools/plug-ins required Full control on the PDF structure in code
Disadvantages Document layout managed by Developer
Use iText when your document flow is straight forward your document structure/layout is stable a PDF Template is hard to build
Copyright © 2013 Roots nv, Belgium - All rights reserved
topic of todayPDF templates & QR codes
Copyright © 2013 Roots nv, Belgium - All rights reserved
before we startintroduce Car-Pass
Copyright © 2011 Roots nv, Belgium - All rights reserved
The starting point
Average annual registrations of second hand vehicles in Belgium: 750.000 cars
Odometer Reading fraud was a serious problem in Belgium > 60.000 new cases a year (estimates based on data Technical Inspection) Estimated value of the fraud: € 150 – 200 million per year
Consequences Poses financial risk and subsequent losses, for consumers Bad image for second hand car salesmen Distorted competition in the market & unfair practices
Authorities and automotive professional associations joined efforts to create a legal framework
Copyright © 2011 Roots nv, Belgium - All rights reserved
Car-Pass
A Car-Pass is a legally required document that a seller of a used car has to handover to the buyer.
It shows the history of odometer readings since its first registration.
A Car-Pass is delivered to the seller by the technical car inspection; who requests it to Car-Pass vzw
Copyright © 2011 Roots nv, Belgium - All rights reserved
Car-Pass vzw
A non-profit organization authorized by Royal Decree
Founded in 2006
To manage the central database with the odometer readings
To deal effectively with odometer fraud
Copyright © 2011 Roots nv, Belgium - All rights reserved
Statistics
Since 2006 140.226.468 Odometers readings 19.483.281 cars
In 2013 777.790 Car-Pass certificates created (+- 500 / hour) 14.676.983 Odometer readings 6.143.554 vehicles are active.
source: http://www.car-pass.be/nl/docs/2013_Jaarverslag_NL_final.pdf
Copyright © 2011 Roots nv, Belgium - All rights reserved
Car-Pass IS v2 project
Complete rebuild of the Car-Pass IS v1 platform
Keeping into account performance maintainability scalability traceability security
Everything in 1 year
Copyright © 2011 Roots nv, Belgium - All rights reserved
Document (PDF tool) requirements
Fast & reliable tool to generate Car-Pass certificates & other PDF documents
Based on Templates adjustable by the End-User
Ensure a small footprint
Improve customers experience
Transparant & affordable license
Copyright © 2011 Roots nv, Belgium - All rights reserved
iText: a perfect fit
Java has no support for PDF’s
Compared tools/suites/platforms vs API’s
iText, due to Lightweight Full control on PDF structure (helps to manage footprint) Ability to manage PDF Templates with Acrobat Pro Scalable, as it easily integrates in a SOA (even OSGI friendly) Support for QR codes Speed (under 90 ms / Car-Pass Certificate instead of 900 ms) Our track record
Copyright © 2011 Roots nv, Belgium - All rights reserved
time for action
Copyright © 2011 Roots nv, Belgium - All rights reserved
Car-Pass Template
Copyright © 2011 Roots nv, Belgium - All rights reserved
Code - Complete Fields with data
try (
FileInputStream is = new FileInputStream(pdfTemplate);
FileOutputStream os = new FileOutputStream(completedCarPassPDF)
) {
PdfReader pdfReader = new PdfReader(is);
PdfStamper stamper = new PdfStamper(pdfReader, os);
AcroFields acroFields = stamper.getAcroFields();
for ( Map.Entry<String, String> entry : model.entrySet() ) {
acroFields.setField ( entry.getKey(), entry.getValue() );
}
stamper.setFormFlattening(true);
stamper.close();
pdfReader.close();
}
Copyright © 2011 Roots nv, Belgium - All rights reserved
Code - From Object to Key-Value pairs
Map<String, String> model = BeanUtils.recursiveDescribe ( carPass, "car-pass" );
http://stackoverflow.com/questions/6133660/recursive-beanutils-describe
model = car-pass.certificateNumber = 0782-7851-9342
car-pass.vin = VF154PB5VXA1138BB
car-pass.make = Renault Megane
car-pass.price = 7,00 EUR
car-pass.firstUse = 22/12/1981
car-pass.firstRegistrationBe = 22/12/1981
car-pass.validUntil = 01/02/2007
car-pass.odometerReadings[0].date = 01/12/2006
car-pass.odometerReadings[0].value = 185643
car-pass.odometerReadings[1].date = 16/04/2006
car-pass.odometerReadings[1].value = 173459
…
Copyright © 2011 Roots nv, Belgium - All rights reserved
Code - QR Code
BarcodeQRCode qrcode = new BarcodeQRCode("https://ws.car-pass.be/verify/" +
carPass.getCertificateNumber(), 160, 160, null); !Image image = qrcode.getImage();
image.setAbsolutePosition(430f, 650f);
PdfContentByte content = stamper.getOverContent(pdfReader.getNumberOfPages());
content.addImage(image); !!
Copyright © 2011 Roots nv, Belgium - All rights reserved
Code - Keeping it small
PdfDictionary drdict = reader.getCatalog().getAsDict(PdfName.ACROFORM).getAsDict(PdfName.DR);
PdfDictionary drfontdict = drdict.getAsDict(PdfName.FONT);
for (PdfName key : new HashSet<PdfName>(drfontdict.getKeys())) {
drfontdict.remove(key);
}
PdfArray pages = reader.getCatalog().getAsDict(PdfName.PAGES).getAsArray(PdfName.KIDS);
for (int i = 0; i < pages.size(); i++) {
PdfDictionary page = pages.getAsDict(i);
PdfArray annots = page.getAsArray(PdfName.ANNOTS);
for (int j = 0; j < annots.size(); j++) {
PdfDictionary annot = annots.getAsDict(j);
PdfName itvalue = annot.getAsName(new PdfName("IT"));
if (new PdfName("FreeTextTypewriter").equals(itvalue)) {
annot.put(new PdfName("IT"), new PdfName("FreeTextTypeWriter"));
}
}
}
Copyright © 2011 Roots nv, Belgium - All rights reserved
iText RUPS
http://itextpdf.com/product/itext_rups
Copyright © 2013 Roots nv, Belgium - All rights reserved
seeing, is believing
Copyright © 2011 Roots nv, Belgium - All rights reserved
www.roots.be
!github.com/koendehaen/itext_summit_pdf_templates
Q&A