Java code to convert HTML to PDF

The Pdfcrowd API is an online tool that lets you easily save web pages and raw HTML code to PDF in your Java applications. PDFs are created in the cloud, no 3rd party libraries are needed. All you need is a tiny Java API client library.

Have a look at the following samples created with the API. Click a thumbnail to open the PDF.


Status report

JavaScript vector chart

Wikipedia page


Code samples

The following code converts a web page to PDF and writes it to a file:

import com.pdfcrowd.*;
// create an API client instance
Client client = new Client("username", "apikey");

// convert a web page and save the PDF to a file
FileOutputStream fileStream = new FileOutputStream("google_com.pdf");
client.convertURI("", fileStream);

You can also convert raw HTML code, just use the convertHtml() method instead of convertURI():

FileOutputStream fileStream = new FileOutputStream("MyHtml.pdf");
client.convertHtml("<body>My HTML Layout</body>", fileStream);

Or use convertFile() to convert a local HTML file:

fileStream = new FileOutputStream("MyLayout.pdf");
client.convertFile("/path/to/MyLayout.html", fileStream);

Did you know ...

  • PDF supports security. The author of a PDF file can set various security options. It is possible to protect a PDF so it can only be viewed with a password. It is also possible to forbid changing the content of a PDF or disable the option to print a PDF file.
  • PDF encapsulates a complete description of fixed-layout flat document.
  • PDF can embed used fonts and thus allow fonts to travel with the documents.

Other Languages