SelectPdf API is the best when it comes to converting web pages to pdf. Just send your url or html string to our API and get back the perfect PDF document. Externalize the load from your servers. Outsource the html to pdf conversion to SelectPdf REST API and get the best results no matter the platform you are using: Java, PHP, .NET, Ruby, Perl or anything else.
SelectPdf HTML To PDF Online REST API is a professional solution that lets you create PDF from web pages and raw HTML code in your applications. The API is easy to use and the integration takes only a few lines of code. The following Java code samples use v2 of SelectPdf Online HTML to PDF REST API.
It’s very easy to convert any URL or raw HTML string to PDF in Java using SelectPdf HTML To PDF API.
Later Edit: The recommended approach to access SelectPdf Online REST API from Java is to use the dedicated Java Client Library for SelectPdf API. Alternatively, you can write your own code to access the API. Below are some examples for this approach.
Example #1 – How to use Java to convert a webpage to PDF and save it on the disk
This code converts an url to pdf in Java using SelectPdf REST API through a GET request. The content is saved into a file on the disk.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
import java.io.*; import java.net.*; import java.util.*; public class TestSelectPdf1 { public static void main(String[] args) throws Exception { HttpURLConnection urlConnection = null; try { String apiEndpoint = "https://selectpdf.com/api2/convert/"; String key = "your license key here"; String testUrl = "https://selectpdf.com"; File localFile = new File("test.pdf"); Map<String,Object> parameters = new LinkedHashMap<>(); parameters.put("key", key); parameters.put("url", testUrl); String encodedParameters = encodeParameters(parameters); URL apiUrl = new URL(apiEndpoint + "?" + encodedParameters); urlConnection = (HttpURLConnection)apiUrl.openConnection(); BufferedInputStream inputStream = new BufferedInputStream(urlConnection.getInputStream()); BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(localFile)); byte[] b = new byte[8 * 1024]; int read = 0; while ((read = inputStream.read(b)) > -1) { outputStream.write(b, 0, read); } outputStream.flush(); outputStream.close(); inputStream.close(); System.out.println("Test pdf document generated successfully!"); } catch (Exception ex) { System.out.println("Error: " + ex.getMessage()); if (urlConnection != null) { if (urlConnection.getResponseCode() != 200) { System.out.println("HTTP Response Code: " + urlConnection.getResponseCode()); System.out.println("HTTP Response Message: " + urlConnection.getResponseMessage()); } } } } private static String encodeParameters(Map<String,Object> params) throws Exception { try { StringBuilder data = new StringBuilder(); for (Map.Entry<String,Object> param : params.entrySet()) { if (data.length() != 0) data.append('&'); data.append(URLEncoder.encode(param.getKey(), "UTF-8")); data.append('='); data.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8")); } return data.toString(); } catch (Exception ex) { throw(ex); } } } |
Example #2 – How to use Java to convert a webpage to PDF with a POST API request and save it on the disk
This code converts an url to pdf in Java using SelectPdf HTML To PDF REST API through a POST request. The parameters are url encoded. The content is saved into a file on the disk.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
import java.io.*; import java.net.*; import java.util.*; public class TestSelectPdf2 { public static void main(String[] args) throws Exception { HttpURLConnection urlConnection = null; try { String apiEndpoint = "https://selectpdf.com/api2/convert/"; String key = "your license key here"; String testUrl = "https://selectpdf.com"; File localFile = new File("test.pdf"); Map<String,Object> parameters = new LinkedHashMap<>(); parameters.put("key", key); parameters.put("url", testUrl); String encodedParameters = encodeParameters(parameters); byte[] postDataBytes = encodedParameters.getBytes("UTF-8"); URL apiUrl = new URL(apiEndpoint); urlConnection = (HttpURLConnection)apiUrl.openConnection(); urlConnection.setRequestMethod("POST"); urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); urlConnection.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length)); urlConnection.setDoOutput(true); urlConnection.getOutputStream().write(postDataBytes); BufferedInputStream inputStream = new BufferedInputStream(urlConnection.getInputStream()); BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(localFile)); byte[] b = new byte[8 * 1024]; int read = 0; while ((read = inputStream.read(b)) > -1) { outputStream.write(b, 0, read); } outputStream.flush(); outputStream.close(); inputStream.close(); System.out.println("Test pdf document generated successfully!"); } catch (Exception ex) { System.out.println("Error: " + ex.getMessage()); if (urlConnection != null) { if (urlConnection.getResponseCode() != 200) { System.out.println("HTTP Response Code: " + urlConnection.getResponseCode()); System.out.println("HTTP Response Message: " + urlConnection.getResponseMessage()); } } } } private static String encodeParameters(Map<String,Object> params) throws Exception { try { StringBuilder data = new StringBuilder(); for (Map.Entry<String,Object> param : params.entrySet()) { if (data.length() != 0) data.append('&'); data.append(URLEncoder.encode(param.getKey(), "UTF-8")); data.append('='); data.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8")); } return data.toString(); } catch (Exception ex) { throw(ex); } } } |
The above samples use only the 2 mandatory parameters (key and url or html) for the html to pdf conversion. For the full list of optional parameters, take a look at SelectPdf HTML To PDF API page. Using the API optional parameters, you can control the page size, orientation and margins in the pdf document, the document information and security, the headers and footers and a lot more.
Hopefully these examples will help you to be on your way using the API for URL to PDF conversion in Java.
The above Java samples can also be found in the following GitHub repository:
https://github.com/selectpdf/selectpdf-api-java-samples