Java code to convert HTML to PDF

The Pdfcrowd API is a web service that lets you easily make PDF from web pages and raw HTML code in your Java applications. PDFs are generated in the cloud, no 3rd party libraries are needed. All you need is a tiny Java API client library.

Here are some samples created with the API. Click a thumbnail to open the PDF.


Wikipedia page

JavaScript vector chart

Status report

Newsletter

Invoice

     

Code samples

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

import com.pdfcrowd.*;
import java.io.*;
 
// 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("http://google.com/", fileStream);
fileStream.close();

Another option is to 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);
fileStream.close();

The API lets you also convert a local HTML file:

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

Did you know ...

  • PDF supports security and permissions to allow the author to retain control of the document and associated rights.
  • PDF can embed used fonts and thus allow fonts to travel with the documents.
  • PDF preserves document fidelity independently of the device, platform, and software.

Other Languages