java - 将 HTML 转换为 -> PDF -> 转换为 MULTIPARTFILE SpringBoot 和 Thymeleaf

标签 java spring-boot itext html2pdf

我有一个在契约(Contract)管理上使用 React 和 SpringBoot 的 Web 应用程序。用户应该能够添加新契约(Contract),然后将契约(Contract)下载为 PDF 文件,这样他就可以签署契约(Contract),然后上传已签署的 contract.pdf。我使用 Java 中的 MultipartFile 完成了上传和下载部分,并将 pdf 存储在 MySql 数据库中。

PDF 是在 SpringBoot 服务器中使用 Thymeleaf 从 HTML 文件创建的。我不清楚的是如何将 PDF 文件转换为 MultipartFile,以便将其保存在数据库中。

而且我还想将文件转换为 PDF,而不是将其保存在本地。 我使用了 iText HtmlConverter.convertToPdf(html, new FileOutputStream(name)); (com.itextpdf.html2pdf),我不知道如何从中获取 PDF 文件...然后转换为 MultipartFile。

这是我将数据从 Controller 传递到 HTML 文件然后将其转换为 PDF 的服务

@Service
public class PdfContentBuilder {

    private TemplateEngine templateEngine;

    @Autowired
    public PdfContentBuilder(TemplateEngine templateEngine) {
        this.templateEngine = templateEngine;
    }

    public String buildContract(Contract contract) {

        Context context = new Context();
        context.setVariable("data_contract", contract.getData());
        context.setVariable("number", contract.getNumber());

        return templateEngine.process("contract", context);
    }

    public void generatePdfFromHtml(String html, String name) throws IOException  {
        
        //here I would need to return a MultipartFile
        HtmlConverter.convertToPdf(html, new FileOutputStream(name));
    }
}

在这里我尝试生成 PDF

public MultipartFile createPDF(Contract contract){
    
    contract.setNumber(25314);
    Date myDate2 = new Date(System.currentTimeMillis());
    contract.setData(myDate2);


    String htmlString = pdfContentBuilder.buildContractTerti(contract);
    try {
        //here I don't know how to take the PDF as file and not save it local
        pdfContentBuilder.generatePdfFromHtml(htmlString, "filename-contract.pdf");

        return pdfMultipartFile;

    } catch (Exception e) {
        e.printStackTrace();
        return pdfMultipartFile;
    }
}

我搜索了 HtmlConverter.convertToPdf,但没有一个版本返回文件,所有版本都返回无效。 如果有人可以帮忙,好吗?

最佳答案

先将 pdf 写入一个字节数组,然后存储在文件中并创建响应:

public byte[] generatePdfFromHtml(String html, String name) throws IOException  {
  ByteArrayOutputStream buffer = new ByteArrayOutputStream();              
  HtmlConverter.convertToPdf(html, buffer);
  byte[] pdfAsBytes = buffer.toByteArray();
  try (FileOutputStream fos = new FileOutputStream(name)) {
   fos.write(pdfAsBytes);
  }
  return pdfAsBytes.
}

下载使用 HttpEntity 而不是 MultipartFile,例如

HttpHeaders header = new HttpHeaders();
header.setContentType(MediaType.APPLICATION_PDF);
header.set(HttpHeaders.CONTENT_DISPOSITION,
                   "attachment; filename=" + fileName.replace(" ", "_"));
header.setContentLength(documentBody.length);

HttpEntity pdfEntity = new HttpEntity<byte[]>(pdfAsBytes, header);

关于java - 将 HTML 转换为 -> PDF -> 转换为 MULTIPARTFILE SpringBoot 和 Thymeleaf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65611509/

相关文章:

java - 属性 'spring.cloud.config.server.git.privateKey' 不是有效的私钥

java - 在 Swagger API 响应中设置对象列表

java - 序列化收到的数据类型为 'LocalDateTime' 的 JSON 对象

java - 如何获取每个分区当前最新的偏移量,然后只消耗该偏移量?

java - 在 Postgres 和 JSP 中存储和检索不同语言字符

java - 使用 Spring Data MongoDB 驱动程序处理未知属性

java - Primefaces tabView activeIndex 属性总是为空

java - 将页面添加为第 n 页

c# - 找到 iTextSharp 对象的页码

java - IText Stamper 为特定文本添加下划线