Create PDF with Java

Create PDF with Java

I prefer outputting my data into XML (using Castor, XStream or JAXB), then transforming it using a XSLT stylesheet into XSL-FO and render that with Apache FOP into PDF. Worked so far for 10-page reports and 400-page manuals. I found this more flexible and stylable than generating PDFs in code using iText.

How to create pdf files in memory

If I look at the subject of your question, then the answer is simple. You are creating a PDF on the file system using a FileOutputStream. In the subject you ask: "How to create pdf files in memory?" That is simple. iText accepts any OutputStream, so the answer is: use a ByteArrayOutputStream instead of a FileOutputStream.

See Using iText, generate on memory a PDF that is generated on disk instead

Document document = new Document();
ByteArrayOutputStream out = new ByteArrayOutputStream();
PdfWriter writer = PdfWriter.getInstance(documento, out);
(...)
byte[] pdf = out.getBytes();

However, why would you want to create a PDF in memory on an Android device? What are you going to do with those bytes? You can't show the PDF if you only have a byte[]. That is explained in my answer to this question: Display PDF document stored in a ByteArrayOutputStream (not in a file)

You will also notice that the following question remained unanswered (because there is no answer): How to visualize a ByteArrayOutputStream as PDF on Android?

Your question changes subject towards the end, where you write: "The application can run normally, but can't create pdf file and I can't find." This sounds as if you don't know how to write a PDF to the file system on Android. That's a different question than what you mention in the subject. That question has been answered many times before:

  • Creating PDFs in android
  • how to solve read only file system error in android programming with eclipse
  • How to save PDF file on Sdcard created using iText?
  • Not able to run simple program of iText library in android
  • Save iText created PDF to internal memory as MODE_WORLD_READABLE

Creating complex pdf using java

When you talk about Certificates, I think of standard sheets that look identical for every receiver of the certificate, except for:

  • the name of the receiver
  • the course that was followed by the receiver
  • a date

If this is the case, I would use any tool that allows you to create a fancy certificate (Acrobat, Open Office, Adobe InDesign,...) and create a static form (sometimes referred to as an AcroForm) containing three fields: name, course, date.

I would then use iText to fill in the fields like this:

PdfReader reader = new PdfReader(pathToCertificateTemplate);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(pathToCertificate));
AcroFields form = stamper.getAcroFields();
form.setField("name", name);
form.setField("course", course);
form.setField("date", date);
stamper.setFormFlattening(true);
stamper.close();
reader.close();

Creating such a certificate from code is "the hard way"; creating such a certificate from XML is "a pain" (because XML isn't well-suited for defining a layout), creating a certificate from (HTML + CSS) is possible with iText's XML Worker, but all of these solutions have the disadvantage that it's hard work to position every item correctly, to make sure everything fits on the same page, etc...

It's much easier to maintain a template with fixed fields. This way, you only have to code once. If for some reason you want to move the fields to another place, you only have to change the template, you don't have to worry about messing around in code, XML, HTML or CSS.

See http://www.manning.com/lowagie2/samplechapter6.pdf for some more info (section 6.3.5).

create pdf from binary data in java

That is Base64 encoded (most probably UTF-8) data, you must decode it before using; such as:

import sun.misc.BASE64Decoder;

...

BASE64Decoder decoder = new BASE64Decoder();
byte[] decodedBytes = decoder.decodeBuffer(biteToRead);

....

Edit:
For java >= 1.8, use:

byte[] decodedBytes = java.util.Base64.getDecoder().decode(biteToRead);


Related Topics



Leave a reply



Submit