4

So.. I can only use the default Java API, and can't use external ones.

How do I write to a PDF?

I've tried just writing with a FileOutputStream, and it didn't work.

I heard that it has to be written using a byte, so I tried:

byte[] buffer = new String("Test");

When I tried opening the file, it said it was corrupted and couldn't be opened.

3
  • 1
    PDF format is owned by the iron proprietary fist of Adobe and it's also a complex image file, not text. Commented Jun 10, 2013 at 20:34
  • What kinds of documents do you want to create? As long as it is only about a few small ones only using Standard 14 Fonts, no images, a lightweight PDF creation class is feasible. Otherwise... well, be prepared to spend some years creating your own PDF library. The PDF specification is only one part of what you need to understand and put into code. Commented Jun 11, 2013 at 6:50
  • 1
    @SlaterTyranus it is an ISO standard too. Most people can live with that Commented Jun 11, 2013 at 7:49

5 Answers 5

6

Creating a PDF is not simply a matter of generating a file with a PDF extention. There is header and footer information that needs to be embedded within the file.

If you really need to do this manually you can read the PDF 1.7 specification at http://www.adobe.com/devnet/acrobat/pdfs/PDF32000_2008.pdf.

I would suggest you use a 3rd party API like iText instead (http://itextpdf.com/).

Sign up to request clarification or add additional context in comments.

Comments

1

There are a few different ways you can do this.

Here are some different libraries you can use for styling, etc.

Create PDF with Java

1 Comment

@ChrisShafer unless your requirement is to write a pdf api, you'll have to use an existing one.
1

The PDF format is quite complex and is not simple to generate.

You should strongly consider using a good library to help you.

2 Comments

I can't use a library though. :( Can you point me in a direction?
Then you will need to implement everything yourself - this will most likely be a large task. What do you need the PDF to have? Just text? Images? Is there nothing you do not have to write yourself?
1

Try this one. The author only uses the default Java APIs, which fits your need:

import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

https://itsallbinary.com/generate-pdf-using-java-from-scratch-without-any-library-multiple-pages-and-graphics/

The complete code link: https://gist.github.com/Ravikharatmal/66d4954822f406ca0761e624e205bb30

Comments

0

Actually, you can do this with just basic JAVA (or any other language) but you really don't want to do this unless you absolutely have to. The trick is that you have to write out a binary data stream with a very specific structure. There's probably better documentation URLs out there, but this was the first one I encountered that seems to provide the kind of information you seem to need...

http://resources.infosecinstitute.com/pdf-file-format-basic-structure/

If you can possibly get around the requirement of not using external external libraries I'd recommend having a look at the free Apache PDFBox library linked below.

http://pdfbox.apache.org/

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.