package org.ta.pddserver.lotto;

import com.google.zxing.WriterException;
import com.itextpdf.io.image.ImageData;
import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Image;
import com.itextpdf.layout.properties.HorizontalAlignment;

import java.awt.*;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

public class ImagePdfGenerator {

    public static byte[] generatePdfWithTextImage() throws Exception {
        // 1. 将文字转为图片（示例：韩文“안녕하세요，世界！”）
        byte[] imageBytes = TextToImageUtils.textToImage(
                "317758904820",
                "1172",
                "REN SUPING",
                "010-3619-****",
                "21353",
                "인천광역시 부평구 부평동 126-9 유진마젤란주상복합   오피스텔1205호",
                "PP25111223915187411540070",
                "B-26-평64",
                "X 부평구 부평5동",
                "부평(대)",
                "평64-이구용"
        );

        // 2. 初始化PDF
        ByteArrayOutputStream pdfBaos = new ByteArrayOutputStream();
        PdfWriter writer = new PdfWriter(pdfBaos);
        PdfDocument pdfDoc = new PdfDocument(writer);
        Document document = new Document(pdfDoc, new PageSize((int) (150 * 300 / 25.4), (int) (100 * 300 / 25.4)));

        // 3. 将图片嵌入PDF
        ImageData imageData = ImageDataFactory.create(imageBytes);
        Image image = new Image(imageData);
        // 设置图片在PDF中居中显示
        image.setHorizontalAlignment(HorizontalAlignment.LEFT);
        document.add(image);

        // 4. 关闭文档
        document.close();
        return pdfBaos.toByteArray();
    }
}