/**
 * @ClassName PdfConverter
 * @Description TODO
 * @Author wangruwei
 * @Date 2024/9/6 15:06
 * @Version 1.0
 */
import cn.hutool.core.io.FileUtil;

import java.io.*;

public class PdfConverter {

    public static void main(String[] args) {
        String inputFilePath = "E:\\a\\b\\xia.txt"; // 保存乱码数据流的文本文件路径
        String outputFilePath = "E:\\a\\b\\xia.pdf"; // 输出PDF文件的路径

        try {
            FileUtil.writeFromStream(new FileInputStream(inputFilePath), outputFilePath);
            //byte[] pdfBytes = readBytesFromFile(inputFilePath);
            //savePdfToFile(pdfBytes, outputFilePath);
            System.out.println("PDF文件已成功保存到: " + outputFilePath);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static byte[] readBytesFromFile(String filePath) throws IOException {
        BufferedReader reader = new BufferedReader(new FileReader(filePath));
        StringBuilder stringBuilder = new StringBuilder();
        String line;

        while ((line = reader.readLine()) != null) {
            stringBuilder.append(line);
        }

        reader.close();

        // 将读取的字符串转换为字节数组
        return stringBuilder.toString().getBytes();
    }

    private static void savePdfToFile(byte[] pdfBytes, String outputFilePath) throws IOException {
        FileOutputStream fileOutputStream = new FileOutputStream(outputFilePath);
        fileOutputStream.write(pdfBytes);
        fileOutputStream.close();
    }
}