I have a huge string of text that is apparently raw data for a PDF file, and I need to make it back into a PDF.
Currently I'm reading the string into a StringBuffer but if I need to I can change that. From there I have tried just writing it out to a file and changing the extension (I really hoped that worked, but I kinda knew it wouldn't), I've tried taking it to a String then getting a byte[] out of it and writing that to the file or using a DataOutputStream to put the bytes into the file. None of these has seemed to work.
I've also tried using the iText plugin, I tried just writing it to a pdf through that and I also tried reading the text as a pdf and then copying it page by page to a new pdf. Neither of these have returned very good results.
It's Friday afternoon, I'm tapped, any suggestions will be a huge help!
A PDF is a binary object. You need to write the bytes directly to a file.
Turning into text will probably break it. Does it start with
%%PDF-
and end with%%EOF
?The iText approach is the right one. You can do something like this :
Okay, well after a lot of research I found out that to preserve the binary data in the string that typically you convert it to Base64 encoding. On a complete guess I decoded the string out of Base64 and dropped the bytes into the pdf file, and lo and behold I had a pdf that could be opened!
Thanks for the answers and I hope this helps someone in the future!
How did you come across this string? If it is a raw ASCII string, you will be missing a large amount of binary data that is embedded within the PDF.
If you have a unicode string, you may be able to write it to a file directly using an OutputStream (not a Writer as you don't actually want to write character data).