+ All Categories
Home > Technology > iText Summit 2014: Talk: How to use PDF Templates with iText

iText Summit 2014: Talk: How to use PDF Templates with iText

Date post: 21-Oct-2014
Category:
View: 113 times
Download: 4 times
Share this document with a friend
Description:
Templates for PDF iText strategies for the mass production of documents based on templates: two user stories presented by iText customers. There are different ways to create high volumes of PDF: - You can create the PDFs from scratch (the program is the template) - You can fill out PDF forms based on AcroForm technology (the template is static) - You can use an XML-based format as a template (XSL:FO, XHTML and CSS, custom XML) - You can use a dynamic XFA form (the XML Forms Architecture) In this session, we will give the floor to two of our customers, one that uses the core iText to create documents, one that uses XFA Worker, a tool built on top of iText. Project for Carpass: PDF templates from Data By Koen Dehaen, Roots Software Koen Dehaen will explain how his team of specialists realised the rendering of Car-Pass Certificates by using the core iText libraries. He will demonstrate how you can complete a standard PDF template with data; how you can flatten it; how you can save it with a minimal file size; and how to create a QR-code. Everything you should know to get started with iText in 20 minutes.
Popular Tags:
26
Copyright © 2013 Roots nv, Belgium - All rights reserved iText @ Car-Pass How to use PDF Templates with iText.
Transcript
Page 1: iText Summit 2014: Talk: How to use PDF Templates with iText

Copyright © 2013 Roots nv, Belgium - All rights reserved

iText @ Car-PassHow to use PDF Templates with iText.

Page 2: iText Summit 2014: Talk: How 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

[email protected]

Page 3: iText Summit 2014: Talk: How to use PDF Templates with iText

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

Page 4: iText Summit 2014: Talk: How to use PDF Templates with iText

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

Page 5: iText Summit 2014: Talk: How to use PDF Templates with iText

Copyright © 2013 Roots nv, Belgium - All rights reserved

how it all startedcreate a PDF from code

Page 6: iText Summit 2014: Talk: How to use PDF Templates with iText

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

Page 7: iText Summit 2014: Talk: How to use PDF Templates with iText

Copyright © 2011 Roots nv, Belgium - All rights reserved

Some real-life examples

Page 8: iText Summit 2014: Talk: How to use PDF Templates with iText

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

Page 9: iText Summit 2014: Talk: How to use PDF Templates with iText

Copyright © 2013 Roots nv, Belgium - All rights reserved

topic of todayPDF templates & QR codes

Page 10: iText Summit 2014: Talk: How to use PDF Templates with iText

Copyright © 2013 Roots nv, Belgium - All rights reserved

before we startintroduce Car-Pass

Page 11: iText Summit 2014: Talk: How to use PDF Templates with iText

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

Page 12: iText Summit 2014: Talk: How to use PDF Templates with iText

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

Page 13: iText Summit 2014: Talk: How to use PDF Templates with iText

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

Page 14: iText Summit 2014: Talk: How to use PDF Templates with iText

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

Page 15: iText Summit 2014: Talk: How to use PDF Templates with iText

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

Page 16: iText Summit 2014: Talk: How to use PDF Templates with iText

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

Page 17: iText Summit 2014: Talk: How to use PDF Templates with iText

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

Page 18: iText Summit 2014: Talk: How to use PDF Templates with iText

Copyright © 2011 Roots nv, Belgium - All rights reserved

time for action

Page 19: iText Summit 2014: Talk: How to use PDF Templates with iText

Copyright © 2011 Roots nv, Belgium - All rights reserved

Car-Pass Template

Page 20: iText Summit 2014: Talk: How to use PDF Templates with iText

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();

}

Page 21: iText Summit 2014: Talk: How to use PDF Templates with iText

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

Page 22: iText Summit 2014: Talk: How to use PDF Templates with iText

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); !!

Page 23: iText Summit 2014: Talk: How to use PDF Templates with iText

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"));

}

}

}

Page 24: iText Summit 2014: Talk: How to use PDF Templates with iText

Copyright © 2011 Roots nv, Belgium - All rights reserved

iText RUPS

http://itextpdf.com/product/itext_rups

Page 25: iText Summit 2014: Talk: How to use PDF Templates with iText

Copyright © 2013 Roots nv, Belgium - All rights reserved

seeing, is believing

Page 26: iText Summit 2014: Talk: How to use PDF Templates with iText

Copyright © 2011 Roots nv, Belgium - All rights reserved

www.roots.be

[email protected]

!github.com/koendehaen/itext_summit_pdf_templates

Q&A


Recommended