Maria
Maria

Reputation: 77

Convert HTML to -> PDF ->to MULTIPARTFILE SpringBoot and Thymeleaf

I have an web app with React and SpringBoot on contract management. The user should be able to add a new contract and then download the contract as PDF file so he can sign it and then upload the contract.pdf signed. I did the part of upload and download, using MultipartFile from java, and storing the pdfs in MySql DataBase.

The PDF is created in the SpringBoot server from HTML file with Thymeleaf. What is unclear to me is how to transform the PDF file to MultipartFile, so I would be able to save it in the DB.

And also I would like to transform the file to PDF and not save it localy. I used iText HtmlConverter.convertToPdf(html, new FileOutputStream(name)); (com.itextpdf.html2pdf ) and I don't know how to take the PDF file from it...and then transform to MultipartFile.

This is the Service where I pass the data from controller to HTML file and then transform it to 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));
    }
}

And here I try to generate the 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;
    }
}

I searched about HtmlConverter.convertToPdf but none of it's versions return a file, all versions returns void. If someone can help, please?

Upvotes: 4

Views: 8381

Answers (1)

Marc Stroebel
Marc Stroebel

Reputation: 2357

write the pdf to a byte array first, then store in file and create response:

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.
}

For download use HttpEntity instead of MultipartFile, e.g.

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);

Upvotes: 2

Related Questions