|
| 1 | +import io.github.cdimascio.dotenv.Dotenv; |
| 2 | +import java.io.File; |
| 3 | +import java.io.IOException; |
| 4 | +import java.util.concurrent.TimeUnit; |
| 5 | +import okhttp3.*; |
| 6 | +import org.json.JSONArray; |
| 7 | +import org.json.JSONObject; |
| 8 | + |
| 9 | +public class Markdown { |
| 10 | + |
| 11 | + // Specify the path to your file here, or as the first argument when running the program. |
| 12 | + private static final String DEFAULT_FILE_PATH = "/path/to/file.pdf"; |
| 13 | + |
| 14 | + // Specify your API key here, or in the environment variable PDFREST_API_KEY. |
| 15 | + // You can also put the environment variable in a .env file. |
| 16 | + private static final String DEFAULT_API_KEY = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"; |
| 17 | + |
| 18 | + public static void main(String[] args) { |
| 19 | + File inputFile; |
| 20 | + if (args.length > 0) { |
| 21 | + inputFile = new File(args[0]); |
| 22 | + } else { |
| 23 | + inputFile = new File(DEFAULT_FILE_PATH); |
| 24 | + } |
| 25 | + final Dotenv dotenv = Dotenv.configure().ignoreIfMalformed().ignoreIfMissing().load(); |
| 26 | + |
| 27 | + String uploadString = uploadFile(inputFile); |
| 28 | + JSONObject uploadJSON = new JSONObject(uploadString); |
| 29 | + if (uploadJSON.has("error")) { |
| 30 | + System.out.println("Error during upload: " + uploadString); |
| 31 | + return; |
| 32 | + } |
| 33 | + JSONArray fileArray = uploadJSON.getJSONArray("files"); |
| 34 | + JSONObject fileObject = fileArray.getJSONObject(0); |
| 35 | + String uploadedID = fileObject.get("id").toString(); |
| 36 | + |
| 37 | + String JSONString = |
| 38 | + String.format("{\"id\":\"%s\", \"page_break_comments\":\"on\"}", uploadedID); |
| 39 | + final RequestBody requestBody = |
| 40 | + RequestBody.create(JSONString, MediaType.parse("application/json")); |
| 41 | + |
| 42 | + Request request = |
| 43 | + new Request.Builder() |
| 44 | + .header("Api-Key", dotenv.get("PDFREST_API_KEY", DEFAULT_API_KEY)) |
| 45 | + .url("https://api.pdfrest.com/markdown") |
| 46 | + .post(requestBody) |
| 47 | + .build(); |
| 48 | + try { |
| 49 | + OkHttpClient client = |
| 50 | + new OkHttpClient().newBuilder().readTimeout(60, TimeUnit.SECONDS).build(); |
| 51 | + |
| 52 | + Response response = client.newCall(request).execute(); |
| 53 | + System.out.println("Markdown Result code " + response.code()); |
| 54 | + if (response.body() != null) { |
| 55 | + System.out.println(prettyJson(response.body().string())); |
| 56 | + } |
| 57 | + } catch (IOException e) { |
| 58 | + throw new RuntimeException(e); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + private static String prettyJson(String json) { |
| 63 | + // https://stackoverflow.com/a/9583835/11996393 |
| 64 | + return new JSONObject(json).toString(4); |
| 65 | + } |
| 66 | + |
| 67 | + // This function is just a copy of the 'Upload.java' file to upload a binary file |
| 68 | + private static String uploadFile(File inputFile) { |
| 69 | + final Dotenv dotenv = Dotenv.configure().ignoreIfMalformed().ignoreIfMissing().load(); |
| 70 | + final RequestBody requestBody = |
| 71 | + RequestBody.create(inputFile, MediaType.parse("application/pdf")); |
| 72 | + |
| 73 | + Request request = |
| 74 | + new Request.Builder() |
| 75 | + .header("Api-Key", dotenv.get("PDFREST_API_KEY", DEFAULT_API_KEY)) |
| 76 | + .header("Content-Filename", "File.pdf") |
| 77 | + .url("https://api.pdfrest.com/upload") |
| 78 | + .post(requestBody) |
| 79 | + .build(); |
| 80 | + try { |
| 81 | + OkHttpClient client = new OkHttpClient().newBuilder().build(); |
| 82 | + Response response = client.newCall(request).execute(); |
| 83 | + System.out.println("Upload Result code " + response.code()); |
| 84 | + if (response.body() != null) { |
| 85 | + return response.body().string(); |
| 86 | + } |
| 87 | + } catch (IOException e) { |
| 88 | + throw new RuntimeException(e); |
| 89 | + } |
| 90 | + return ""; |
| 91 | + } |
| 92 | +} |
0 commit comments