简体   繁体   English

如何使用spring boot将生成的PDF文件保存到MySQL db?

[英]How to save generated PDF files to MySQL db using spring boot?

I have a function which is generating PDF file using iText libary.我有一个使用 iText 库生成 PDF 文件的功能。 My idea is that convert document to byte array but I always get a error: com.itextpdf.text.Document@2805d0d4. The file cannot be found我的想法是将文档转换为字节数组,但我总是收到一个错误: com.itextpdf.text.Document@2805d0d4. The file cannot be found com.itextpdf.text.Document@2805d0d4. The file cannot be found . com.itextpdf.text.Document@2805d0d4. The file cannot be found

Here is my PDF generation function:这是我的 PDF 生成功能:

    @Override
    public Boolean createdPDF() throws Exception {
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream("iTextHelloWorld.pdf"));

        document.open();
        Font font = FontFactory.getFont(FontFactory.COURIER, 16, BaseColor.BLACK);
        Chunk chunk = new Chunk("Hello World", font);

        document.add(chunk);
        document.close();
        getByteArrayFromFile(document);

        return true;
    }

Here is my convert byte array from file function:这是我从文件函数转换字节数组:

    private byte[] getByteArrayFromFile(Document handledDocument) throws IOException {
         ByteArrayOutputStream baos = new ByteArrayOutputStream();
         InputStream in = new FileInputStream(String.valueOf(handledDocument));
         byte[] buffer = new byte[500];

        int read = -1;
        while ((read = in.read(buffer)) > 0) {
            baos.write(buffer, 0, read);
        }
        in.close();

        Ticket newTicket = new Ticket();
        newTicket.setFileName("example");
        newTicket.setData(baos.toByteArray());
        ticketRepository.save(newTicket);

        return baos.toByteArray();
    }

Ticket entity:票务实体:

    @Data
    @Entity
    public class Ticket {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String fileName;

    @Lob
    private byte[] data;

    @NotNull
    @JsonIgnore
    @Column(updatable = false)
    private LocalDateTime createAt;

    @NotNull
    @JsonIgnore
    private LocalDateTime updatedAt;
}

Here is an example how you can get a byte[] from PdfDocument:以下是如何从 PdfDocument 获取 byte[] 的示例:

ByteArrayOutputStream baos = new ByteArrayOutputStream();
Documentdocument = new Document();

PdfWriter pdfWriter = PdfWriter.getInstance(document, baos);

document.open();
Font font = FontFactory.getFont(FontFactory.COURIER, 16, BaseColor.BLACK);
Chunk chunk = new Chunk("Hello World", font);

document.add(chunk);
document.close(); 

pdfWriter.flush();

byte[] pdfAsBytes = baos.toByteArray();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Java 将生成的 PDF 文件保存到 MySQL 数据库? - How to save generated PDF files to MySQL database using Java? 如何使用Spring Boot JpaRepository及其MySQL脚本保存多个表 - How to save multiple tables using Spring Boot JpaRepository and their MySQL script 如何使用 Spring 数据 Z9CE3D1BD8890F16A0C7C64809359508 将 JSON 数据保存到 MySQL 数据库中 - How to save JSON Data into MySQL DB using Spring Data JPA 如何使用java将doc、pdf和图像文件保存到mysql数据库? - How to save doc, pdf, and image files to mysql database using java? 如何使用Servlet批量上传PDF文件并保存到MySQL? - How to mass upload PDF files and save into MySQL using Servlet? 如何在 Spring Boot 中以 Restfull 方式将生成的 PDF 文档发送到前端? - How to send generated PDF document to frontend in Restfull way in Spring Boot? 如何使用spring boot和jpa在db中上传文件和保存文件? - how to upload file and save file in db use spring boot and jpa? 如何通过spring boot将日志保存在所有记录器的数据库中 - How to save logs in DB on all loggers via spring boot 没有生成实体的空数据库,带有 Spring 引导 - Empty DB without generated entities with Spring Boot 使用 Spring Boot RestController 下载多个 pdf 文件 - Downloading multiple pdf files using Spring Boot RestController
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM