Java examples to convert HTML strings to a PDF or a url to PDF

If you are planning to use Java to convert your webpages to pdfs, you can use our web service which is free in small volumes for commercial applications and platforms. No special special components or libraries are needed.

It is quite useful for brochures, reporting and invoicing - create a HTML webpage and use it for both a downloadable PDF or for the users to view.

The service will convert webpages to pdfs online by reading your webpage or taking the HTML you send it, and dynamically generate a PDF or an image. It will handle fairly complex documents and layouts including images and executing javascript, although as always the simpler the better.

    • Convert a URL or HTML to a file

    • package com.mypackage;
      
      import java.io.BufferedInputStream;
      import java.io.BufferedOutputStream;
      import java.io.FileOutputStream;
      import java.io.IOException;
      import java.io.InputStream;
      import java.net.MalformedURLException;
      import java.net.URL;
      import java.net.URLConnection;
      import java.net.URLEncoder;
      
      public class RocketAPI {
      
      	/**
      	 * Rocket Java API Example
      	 * Run thru commandline 
      	 * Param 1: APIKey
      	 * Param 2: URL or HTML add quote if you have spaces. use single quotes instead of double
      	 * Param 3: Any extra params you want to add i.e &extra1=value&extra2=value
      	 * 
      	 */
      
      	static String BaseURL="http://api.html2pdfrocket.com/pdf";
      
      	public static void main(String[] args) {
      		String API="";
      		String Value="";
      		String FileName="";
      		String ExtraParams="";
      		if(args.length>0) API=args[0];
      		if(args.length>1) Value=args[1];
      		if(args.length>2) FileName=args[2];
      		if(args.length>3) ExtraParams=args[3];
      		getFile(API,Value,FileName,ExtraParams);
      	}
      
      	private static void getFile(String APIKey,String value,String Filename,String ExtraParams){
      		URL url;
      		String Params="";
      		try {
      			if(ExtraParams!=null&&!"".equals(ExtraParams)){
      				Params=ExtraParams;
      				if(!Params.substring(0,1).equals("&")){
      					Params="&"+Params;
      				}
      			}
      			
      			value=URLEncoder.encode(value,java.nio.charset.StandardCharsets.UTF_8.toString() );
      			value+=Params;
      
      			// Validate parameters
      			if(APIKey==null||"".equals(APIKey)) throw(new Exception("API key is empty"));
      			if(Filename==null||"".equals(Filename))	throw(new Exception("Filename is empty"));
      
      			// Append parameters for API call
      			url = new URL(BaseURL+"?apikey="+APIKey+"&value="+value);
      		
      			// Download PDF file
      			URLConnection connection = url.openConnection();
      			InputStream Instream = connection.getInputStream();
      
      			// Write PDF file 
      			BufferedInputStream BISin = new BufferedInputStream(Instream);
      			FileOutputStream FOSfile = new FileOutputStream(Filename);
      			BufferedOutputStream out = new BufferedOutputStream(FOSfile);
      			
      			int i;
      			while ((i = BISin.read()) != -1) {
      				out.write(i);
      			}
      
      			// Clean up
      			out.flush();
      			out.close();
      			System.out.println("File "+Filename+" created");
      
      		} catch (MalformedURLException e) {
      			e.printStackTrace();
      		} catch (IOException e) {
      			e.printStackTrace();
      		}catch (Exception e){
      			e.printStackTrace();
      		}
      	}
      }
      
      

Need another java example? Would you mind dropping us a quick one line note to say which example you would have liked and we'll put the most commonly used ones here.

Get Started

Don't waste your day reinventing the wheel, be converting in 3 minutes from now

Conversion Tip


The number #1 reason a conversion fails is because it contains an references to external links or images that doesn't exist. If there is a conversion error, please check the page using your webbrowsers 'developer' network tab to ensure there are no 404 errors.