-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAiConvert.java
More file actions
42 lines (37 loc) · 1.66 KB
/
Copy pathAiConvert.java
File metadata and controls
42 lines (37 loc) · 1.66 KB
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
// Experimental: convert a plain PDF invoice to CII using AI.
// Review output manually before sending to a customer or tax authority.
//
// Get API key: https://www.invoicexml.com/account/authentication
// Docs: https://www.invoicexml.com/docs/api/transform/to/cii
//
// Dependency: com.squareup.okhttp3:okhttp:4.12.0
import okhttp3.*;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;
public class AiConvert {
public static void main(String[] args) throws Exception {
// Raw key only, without the "Bearer " prefix (it is added below).
String apiKey = "YOUR_API_KEY";
RequestBody body = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("file", "plain-invoice.pdf",
RequestBody.create(new File("plain-invoice.pdf"), MediaType.parse("application/pdf")))
.build();
Request request = new Request.Builder()
.url("https://api.invoicexml.com/v1/transform/to/cii")
.header("Authorization", "Bearer " + apiKey)
.post(body)
.build();
try (Response response = new OkHttpClient().newCall(request).execute()) {
String xml = response.body().string();
if (!response.isSuccessful()) {
System.err.println("InvoiceXML API error " + response.code() + ": " + xml);
System.exit(1);
}
Files.write(Paths.get("converted-cii.xml"), xml.getBytes("UTF-8"));
System.out.println("Saved converted-cii.xml (" + xml.length() + " chars)");
System.out.println("Review the output manually before sending.");
}
}
}