Get Started

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

Android examples on how to convert HTML to PDF

Wanting to convert HTML to PDF files on Android? A few lines of code and your underway with our free web service, in use by commercial applications and platforms. You do not need to install or download any special libraries or components to make this work.

Especially great for reporting, brochures and invoicing - create one HTML webpage and use it as both as a source to generate your downloadable PDF or for users to view directly.

In addition to the snippets below, we have a full downloadable example here.

    • Example #1 - How to convert a webpage to a PDF using Android

    • public void ConvertWebPageToPDF()
      {
      // note: be sure to copy the helper function ConvertHTMLStringToPDF() from this webpage
      // a url starting with http or an HTML string
      String value = "http://www.google.com"
      String apiKey = "ABCD-1234";
      String apiURL = "https://api.html2pdfrocket.com/pdf";
      HashMap<string, string> params = new HashMap<string, string>();
      params.put("apiKey", apiKey);
      params.put("value", value);
      // Call the API convert to a PDF
      InputStreamReader request = new InputStreamReader(Request.Method.Post, apiURL, new Response.Listener<byte[]>(){
      @Override
      public void onResponse(byte[] response)
      {
      try
      {
      if(response != null)
      {
      File localFolder = new File(Environment.getExternalStorageDirectory(), "WebPageToPDF");
      if(!localFolder.exists())
      {
      localFolder.mkdirs();
      }
      // Write stream output to local file
      File pdfFile = new File (localFolder, "MySamplePDFFile.pdf");
      OutputStream opStream = new FileOutputStream(pdfFile);
      pdfFile.setWritable(true);
      opStream.write(response);
      opStream.flush();
      op.close();
      }
      } catch (Exception ex)
      {
      Toast.makeText(getBaseContext(), "Error while generating PDF file!!", Toast.LENGTH_LOG).show();
      }
      }
      });
      }
    • Example #2 - How to convert a webpage to an image using Android

    • Line #9 allows you to change the default output format so the html is converted into an image. You can use "png", "bmp" or "svg" formats for images.

    • public void ConvertWebPageToImage()
      {
      // note: be sure to copy the helper function ConvertHTMLStringToPDF() from this webpage
      String apiKey = "YOUR-API-KEY";
      String value = "http://www.google.com" // an HTML string
      String apiURL = "https://api.html2pdfrocket.com/pdf";
      HashMap<string, string> params = new HashMap<string, string>();
      params.put("apiKey", apiKey);
      params.put("value", value);
      params.put("OutputFormat", "jpg"); // Set Output format as JPG to convert page into image.
      // Call the API convert to a PDF
      InputStreamReader request = new InputStreamReader(Request.Method.Post, apiURL,
      new Response.Listener<byte[]>(){
      @Override
      public void onResponse(byte[] response)
      {
      try
      {
      if(response != null)
      {
      File localFolder = new File(Environment.getExternalStorageDirectory(), "WebPageToPDF");
      if(!localFolder.exists())
      {
      localFolder.mkdirs();
      }
      // Write stream output to local file
      File pdfFile = new File (localFolder, "MySampleImageFile.jpg");
      OutputStream opStream = new FileOutputStream(pdfFile);
      pdfFile.setWritable(true);
      opStream.write(response);
      opStream.flush();
      op.close();
      }
      } catch (Exception ex)
      {
      Toast.makeText(getBaseContext(), "Error while generating PDF file!!", Toast.LENGTH_LOG).show();
      }
      }});
      }
    • Example #3 - How to convert an HTML string to a PDF using Android

    • You can also convert an HTML string to a PDF by supplying the HTML directly. It can be as complex as you like and may include image references and stylesheeet information -- just make sure it is valid HTML

    • public void ConvertHTMLStringToPDF()
      {
      // note: be sure to copy the helper function ConvertHTMLStringToPDF() from this webpage
      String apiKey = "ABCD-1234";
      String value = "<h1>An <strong>Example</strong>HTML String</h1>" // an HTML string
      String apiURL = "https://api.html2pdfrocket.com/pdf";
      HashMap<string, string> params = new HashMap<string, string>();
      params.put("apiKey", apiKey);
      params.put("value", value);
      // Call the API convert to a PDF
      InputStreamReader request = new InputStreamReader(Request.Method.Post, apiURL,
      new Response.Listener<byte[]>(){
      @Override
      public void onResponse(byte[] response)
      {
      try
      {
      if(response != null)
      {
      File localFolder = new File(Environment.getExternalStorageDirectory(), "WebPageToPDF");
      if(!localFolder.exists())
      {
      localFolder.mkdirs();
      }
      // Write stream output to local file
      File pdfFile = new File (localFolder, "MySamplePDFFile.pdf");
      OutputStream opStream = new FileOutputStream(pdfFile);
      pdfFile.setWritable(true);
      opStream.write(response);
      opStream.flush();
      op.close();
      }
      } catch (Exception ex)
      {
      Toast.makeText(getBaseContext(), "Error while generating PDF file!!", Toast.LENGTH_LOG).show();
      }
      }});
      }
    • Helper Class

    • This helper class is common to all examples

    • // Helper class to send request to web API
      class InputStreamReader extends Request<byte[]> {
      private final Response.Listener<byte[]> mListener;
      private Map<string, string> mParams;
      public Map<string, string> responseHeaders;
      public InputStreamReader(int method, String mUrl, Response.Listener<byte[]> listener,
      Response.ErrorListener errorListener, HashMap<string, string> params) {
      super(method, mUrl, errorListener);
      // Every time it should make new request, should not use cache
      setShouldCache(false);
      mListener = listener;
      mParams = params;
      }
      @Override
      protected Map<string, string> getParams()
      throws com.android.volley.AuthFailureError {
      return mParams;
      }
      @Override
      protected void deliverResponse(byte[] response) {
      mListener.onResponse(response);
      }
      @Override
      protected Response<byte[]> parseNetworkResponse(NetworkResponse response) {
      //Initialise local responseHeaders map with response headers received
      responseHeaders = response.headers;
      //Pass the response data here
      return Response.success(response.data, HttpHeaderParser.parseCacheHeaders(response));
      }
      }

Need another Android 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.