Hi I have a requirement to generate a pdf file in the file system from a byte array. The input is an html string. The pdf that is generated is corrupted and acrobat does not open it. Below is my code. Also attached the file. Could anyone please suggest what is it that I am doing wrong:
* public* *class* MakePDF { *public* *static* *void* main(String[] args) { Document doc = *new* Document(); ByteArrayOutputStream baosPDF = *new* ByteArrayOutputStream(); File outFile = *new* File("\\try\\frombytes.pdf"); *try* { // Creating *pdf* document from byte array FileOutputStream file = *new* FileOutputStream(outFile); PdfWriter.*getInstance*(doc, *new* FileOutputStream("\\try\\test.pdf")); PdfWriter.*getInstance*(doc, baosPDF); doc.open(); String htmlStr = "<html><head><title>Test HTML</title></head><body>This is just a test document</body></html>"; StringReader strReader = *new* StringReader(htmlStr); *ArrayList* p = HTMLWorker.*parseToList*(strReader, *null*); *for* (*int* k = 1; k < p.size(); ++k) { Element element = (Element)p.get(k); doc.add(element); // Add a new page in the *pdf* for new section doc.newPage(); } file.write(baosPDF.toByteArray()); file.flush(); baosPDF.close(); file.close(); System.*out*.println("...Done"); } *catch*(DocumentException e) { e.printStackTrace(); } *catch*(IOException e) { e.printStackTrace(); } *finally* { doc.close(); } } }
MakePDF.java
Description: Binary data
------------------------------------------------------------------------------ This SF.net email is sponsored by Sprint What will you do first with EVO, the first 4G phone? Visit sprint.com/first -- http://p.sf.net/sfu/sprint-com-first
_______________________________________________ iText-questions mailing list iText-questions@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/itext-questions Buy the iText book: http://www.itextpdf.com/book/ Check the site with examples before you ask questions: http://www.1t3xt.info/examples/ You can also search the keywords list: http://1t3xt.info/tutorials/keywords/