-2

I Want to print html as a pdf. Here is my code. It was not throwing any error but when i open pdf it is showing error

Here is my java code

URL u = new URL("http://localhost/printPdf.Html");
URLConnection uc = u.openConnection();
BufferedInputStream is = new BufferedInputStream(uc.getInputStream());
File outs = new File("D:/HtmlToPdf.pdf")
BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(outs));
byte[] b = new byte[8 * 1024];
int read = 0;
while ((read = is.read(b)) > -1) {
bout.write(b, 0, read);
}
bout.flush();
bout.close();
is.close();

Here is my html file

<html>
<head></head>
<body><div>Hi !!! Example PDF </div></body>
</html>

When i open pdf i am getting this error

Adobe Reader could not open because it is either not a supported file type or because the file has been damaged (for example, it was sent as an email attachment and wasn't correctly decoded).

Krishnan
  • 185
  • 3
  • 11
  • 2
    Sending the data to a file named .PDF will not generate a PDF file. There are several questions about PDF generation on SO. Such as this: http://stackoverflow.com/questions/7355025/create-pdf-with-java –  Mar 03 '14 at 15:47

1 Answers1

0

You cannot simply write one file format to another. You need to write the HTML in a way according to the PDF specification.

Here is a PDF library you can use: http://pdfbox.apache.org/

Sujen
  • 1,614
  • 5
  • 19
  • 25