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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
| import org.apache.commons.io.FileUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
public class DocumentConverter {
private static String submitPdfConversionTask(CloseableHttpClient httpClient, String filePathToConvert, String rapidApiKey) throws IOException {
File fileToConvert = new File(filePathToConvert);
HttpPost submitPDFConversionTaskPost = new HttpPost("https://petadata-document-conversion-suite.p.rapidapi.com/SubmitPDFConversionTask");
submitPDFConversionTaskPost.addHeader("X-RapidAPI-Key", rapidApiKey);
byte[] bytes = FileUtils.readFileToByteArray(fileToConvert);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
builder.addBinaryBody("file", bytes, ContentType.DEFAULT_BINARY, fileToConvert.getName());
builder.addTextBody("authorName", "Sebastian", ContentType.TEXT_PLAIN);
builder.addTextBody("title", "Final report", ContentType.TEXT_PLAIN);
builder.addTextBody("keywords", "Reports, Final", ContentType.TEXT_PLAIN);
builder.addTextBody("name", "Quarterly Final Report", ContentType.TEXT_PLAIN);
HttpEntity requestEntity = builder.build();
submitPDFConversionTaskPost.setEntity(requestEntity);
HttpResponse response = httpClient.execute(submitPDFConversionTaskPost);
if (response.getStatusLine().getStatusCode() != 200) {
throw new IOException("Cannot post conversion task");
}
HttpEntity responseEntity = response.getEntity();
return EntityUtils.toString(responseEntity, "UTF-8");
}
private static String getConversionTaskStatus(CloseableHttpClient httpClient, String taskId, String rapidApiKey) throws IOException {
HttpGet getConversionTaskStatus = new HttpGet("https://petadata-document-conversion-suite.p.rapidapi.com/GetConversionTaskStatus?taskId=" + taskId);
getConversionTaskStatus.addHeader("X-RapidAPI-Key", rapidApiKey);
HttpResponse response = httpClient.execute(getConversionTaskStatus);
if (response.getStatusLine().getStatusCode() != 200) {
throw new IOException("Cannot get conversion task status");
}
HttpEntity responseEntity = response.getEntity();
return EntityUtils.toString(responseEntity, "UTF-8");
}
private static byte[] downloadResult(CloseableHttpClient httpClient, String taskId, String rapidApiKey) throws IOException {
HttpGet downloadResult = new HttpGet("https://petadata-document-conversion-suite.p.rapidapi.com/DownloadResult?taskId=" + taskId);
downloadResult.addHeader("X-RapidAPI-Key", rapidApiKey);
HttpResponse response = httpClient.execute(downloadResult);
if (response.getStatusLine().getStatusCode() != 200) {
throw new IOException("Cannot get converted file");
}
HttpEntity responseEntity = response.getEntity();
return EntityUtils.toByteArray(responseEntity);
}
public static void main(String[] args) throws Exception {
String rapidApiKey = "<YOUR RAPIDAPI KEY HERE>";
String filePathToConvert = "<FILE PATH TO CONVERT>";
CloseableHttpClient httpClient = HttpClients.createDefault();
String taskId = submitPdfConversionTask(httpClient, filePathToConvert, rapidApiKey);
int retryCount = 0;
while (retryCount < 100)
{
retryCount++;
Thread.sleep(5000);
String status = getConversionTaskStatus(httpClient, taskId, rapidApiKey);
if (status.equals("Completed"))
{
byte[] fileBytes = downloadResult(httpClient, taskId, rapidApiKey);
File outputFile =new File("final_report.pdf");
Files.write(outputFile.toPath(), fileBytes);
break;
}
else if (status.equals("Waiting"))
{
continue;
}
else if (status.equals("Failed"))
{
throw new Exception("Cannot convert file");
}
else
{
throw new Exception("Invalid status");
}
}
}
}
|