Reputation: 68
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.
Upvotes: 4
Views: 8911
Reputation: 263
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;
The complete code link: https://gist.github.com/Ravikharatmal/66d4954822f406ca0761e624e205bb30
Upvotes: 1
Reputation: 769
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.
Upvotes: 0
Reputation: 96
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/).
Upvotes: 6
Reputation: 75426
The PDF format is quite complex and is not simple to generate.
You should strongly consider using a good library to help you.
Upvotes: 1
Reputation: 19
There are a few different ways you can do this.
Here are some different libraries you can use for styling, etc.
Upvotes: 1