使用 thymeleaf 的 itext 生成 pdf

问题描述 投票:0回答:1

我想从我的模板 thymeleaf 生成 pdf,因为我的目标是在我的模板中插入数据,然后生成 pdf。但我不工作!

TestController.java---------------------------------------------------------- ------------------------------------------------------

package com.example.demo.test;

import com.example.demo.service.PdfGenerationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.thymeleaf.context.Context;

@Controller

public class TestController {

    @Autowired
    private PdfGenerationService pdfService;

    @GetMapping("/generate-pdf") // Correction de l'URL du point de terminaison
    public ResponseEntity<byte[]> downloadPdf() {
        Context context = new Context();
        context.setVariable("yassine", "Yassine ");
        context.setVariable("contrat", "Assurance");
        context.setVariable("numero", "1234567890");
        context.setVariable("date_debut", "2024-04-01");
        context.setVariable("date_fin", "2025-04-01");
        context.setVariable("currentDate", "2024-04-18");
        // Add other variables as needed

        try {
            byte[] pdfContents = pdfService.generatePdf("mapage.html", context);

            HttpHeaders headers = new HttpHeaders();
            headers.add(HttpHeaders.CONTENT_DISPOSITION, "inline; filename=mapage.pdf");
            headers.add(HttpHeaders.CONTENT_TYPE, "application/pdf");

            return ResponseEntity.ok().headers(headers).body(pdfContents);
        } catch (Exception e) {
            return ResponseEntity.internalServerError().build();
        }
    }


    @GetMapping("/mapage")

    public String attestation(Model model) {
        Souscripteur souscripteur = new Souscripteur("Yassine");
        model.addAttribute("souscripteur", souscripteur);  
        model.addAttribute("contrat", "Assurance ");
        model.addAttribute("numero", "1234567890");
        return "mapage";   resources/templates
    }}

PdfGeneration服务------------------------------------------------ --------------------------------------

package com.example.demo.service;

import com.itextpdf.html2pdf.ConverterProperties;
import com.itextpdf.kernel.pdf.PdfWriter;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import com.itextpdf.html2pdf.HtmlConverter;
import java.io.ByteArrayOutputStream;

@Service
public class PdfGenerationService {

    private final TemplateEngine templateEngine;

    public PdfGenerationService(TemplateEngine templateEngine) {
        this.templateEngine = templateEngine;
    }

    public byte[] generatePdf(String templateName, Context context) throws Exception {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        PdfWriter writer = new PdfWriter(outputStream);

        ConverterProperties converterProperties = new ConverterProperties();
        converterProperties.setBaseUri("http://localhost:8080"); 

        String htmlContent = templateEngine.process(templateName, context);

        // Call PdfConverter to convert HTML to PDF
        HtmlConverter.convertToPdf(htmlContent, writer, converterProperties);
        writer.close();

        return outputStream.toByteArray();
    }
}


mappage.html--------------------------------------------------------- -------------------------------------------------

<!DOCTYPE html>
<html lang="fr" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>ATTESTATION </title>
    
</head>
<body>
<header>
<!--    <img th:src="@{/images/logo.png}" alt="Logo mylogo">-->
    <div class="title-container">
        <h1>ATTESTATION </h1>
    </div>
</header>

<main>
    <section class="informations-souscripteur">
        <ul>
            <li><span class="label">Emeteur :</span><span class="value" th:text="yassine"></span></li>
            <li><span class="label">Contrat :</span><span class="value" th:text="${contrat}"></span></li>
            <li><span class="label">N° :</span><span class="value" th:text="${numero}"></span></li>
        </ul>
    </section>

    <div class="texte">
         <span style="color: blue; font-weight: bold; font-style: italic;" th:text="$assure"></span></p>
        <p>• Conjoint (s) :</p>
        <p>• Enfant (s) :</p>
        <p>• Parent (s) :</p>
         <span style="color: blue; font-weight: bold; font-style: italic;" th:text="$contrat"></span>, valable du <span style="color: blue; font-weight: bold;" th:text="datedebut"></span> au <span style="color: blue; font-weight: bold;" th:text="datefin"></span>
        
      
    <div class="signature">
        <div class="left">
            <span style="color: blue; font-weight: bold; font-style: italic;" th:text="currentdate"></span></p>
           
        </div>
        <div class="right">
            <p>Pour la compagnie</p>
<!--            <p><img th:src="@{/images/signature.png}" alt="Signature "></p>-->
        </div>
    </div>
</main>


</body>
</html>

my error 500

我想成功生成pdf!

java pdf itext thymeleaf itext7
1个回答
0
投票

https://springhow.com/spring-boot-pdf- Generation/我尝试了这个解决方案并且它有效

© www.soinside.com 2019 - 2024. All rights reserved.