import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;

public class PDFToMDTable {
    // 需要补充pdf文件路径，markdown文件路径，x-ti-app-id，x-ti-secret-code
    private static final String PDF_PATH = "demo.pdf";
    private static final String MD_PATH = "demo.md";
    private static final String APP_ID = ""; // Your x-ti-app-id
    private static final String SECRET_CODE = ""; // Your x-ti-secret-code

    private static final String URL_STRING = "https://api.textin.com/ai/service/v1/pdf_to_markdown?markdown_details=1&table_flavor=md";

    public static void main(String[] args) {
        try {
            String jsonResponse = sendRequest(PDF_PATH);
            if (jsonResponse != null) {
                writeMarkdown(jsonResponse, MD_PATH);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static String sendRequest(String inputPath) throws IOException {
        File file = new File(inputPath);
        FileInputStream fis = new FileInputStream(file);
        byte[] data = new byte[(int) file.length()];
        fis.read(data);
        fis.close();

        URL url = new URL(URL_STRING);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("POST");
        connection.setRequestProperty("x-ti-app-id", APP_ID);
        connection.setRequestProperty("x-ti-secret-code", SECRET_CODE);
        connection.setRequestProperty("Content-Type", "application/octet-stream");
        connection.setDoOutput(true);

        OutputStream os = connection.getOutputStream();
        os.write(data);
        os.flush();
        os.close();

        int responseCode = connection.getResponseCode();
        if (responseCode == HttpURLConnection.HTTP_OK) {
            BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String inputLine;
            StringBuilder response = new StringBuilder();

            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();

            return response.toString();
        } else {
            System.out.println("POST request failed. Response Code: " + responseCode);
            return null;
        }
    }

    private static void writeMarkdown(String jsonResponse, String outputPath) {
        // Parse the JSON response to extract the markdown text
        // Assuming you have a JSON library like org.json available
        try {
            org.json.JSONObject json = new org.json.JSONObject(jsonResponse);
            org.json.JSONArray details = json.getJSONObject("result").getJSONArray("detail");

            FileWriter fileWriter = new FileWriter(outputPath);

            for (int i = 0; i < details.length(); i++) {
                org.json.JSONObject detail = details.getJSONObject(i);
                if ("table".equals(detail.getString("type"))) {
                    fileWriter.write(detail.getString("text"));
                }
            }

            fileWriter.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
