【问题标题】:Spring Boot + Thymeleaf + Flying Saucer imported css not workingSpring Boot + Thymeleaf + Flying Saucer 导入的 css 不工作
【发布时间】:2020-07-04 07:10:37
【问题描述】:

我不明白为什么 css 不起作用(我已经按照this thread 上所说的内容进行了操作) 我的资源有以下文件结构:

resources
    - static
         -style
            style.css
 - templates:
      - CV.html

在模板中,我验证了正确生成的 URL,它是。 CSS 也正确提供:当我在浏览器中使用生成的 URL 时,提供 CSS 文件。

我的模板代码:

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org" lang="fr">
<head>
    <title>CV ARSENE LAPOSTOLET</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <link type="stylesheet" href="../static/css/style.css" th:href="@{|${baseUrl}/css/style.css|}"/>

</head>

<body>

<span th:text="|${baseUrl}/css/style.css|"></span>

<!--/*@thymesVar id="abilities" type="List<Ability>"*/-->
<div class="rouge" th:each="ability: ${abilities}">
    <span th:text="${ability.getName()}"></span>
</div>
</body>
</html>

我的 Java 代码:

@RestController
@RequestMapping("/cv")
@AllArgsConstructor
public class CvController {

    private CvService cvService;
    private AbilityRepository abilityRepository;
    private ServletContext servletContext;

    @GetMapping("")
    public ResponseEntity<Resource> getCv(HttpServletRequest servletRequest, HttpServletResponse servletResponse){
        Locale locale = getLocale(servletRequest);
        WebContext context = new WebContext(servletRequest, servletResponse, servletContext, locale);
        context.setVariable("abilities", abilityRepository.findAll());
        context.setVariable("baseUrl",getCurrentBaseUrl());

        return ResponseEntity.ok()
                .contentType(MediaType.parseMediaType("application/pdf"))
                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + "CV_ARSENE_LAPOSTOLET.pdf" + "\"")
                .body(new ByteArrayResource(cvService.renderCv(context)));
    }

    private static String getCurrentBaseUrl() {
        ServletRequestAttributes sra = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest req = sra.getRequest();
        return req.getScheme() + "://" + req.getServerName() + ":" + req.getServerPort() + req.getContextPath();
    }
}
@Service
@AllArgsConstructor
@CommonsLog
public class CvService {

    private TemplateEngine templateEngine;

    public byte[] renderCv(WebContext ctx) {

        String processedHtml = templateEngine.process("CV", ctx);

        try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
            ITextRenderer renderer = new ITextRenderer();
            renderer.setDocumentFromString(processedHtml);
            renderer.layout();
            renderer.createPDF(os);

            return os.toByteArray();
        } catch (DocumentException | IOException e) {
            e.printStackTrace();
            return null;
        }
    }


}

【问题讨论】:

    标签: java css spring-boot itext thymeleaf


    【解决方案1】:

    问题

    您好,我不知道您是否已经解决了问题,但我会给您我的解决方案。同样的问题发生在我身上,我试图将 css 链接到 html 但没有任何警告就没有发生任何事情。此问题与您的 java 代码中的方法调用有关。

    renderer.setDocumentFromString(processedHtml);
    

    这个方法有一个重载

    public void setDocumentFromString(String content, String baseUrl);
    

    如您所见,它还接受选择正确文件所需的 baseUrl

    解决方案

    首先我生成了一个 baseUrl 我的资源文件所在的位置。

    private static void generatePdf(String html) throws Exception {
            String outputFolder = System.getProperty("user.home") + File.separator + "thymeleaf.pdf";
            OutputStream outputStream = new FileOutputStream(outputFolder);
    
            String baseUrl = FileSystems
                    .getDefault()
                    .getPath("src", "main", "resources")
                    .toUri()
                    .toURL()
                    .toString();
    
            ITextRenderer renderer = new ITextRenderer();
            renderer.setDocumentFromString(html, baseUrl);
            renderer.layout();
            renderer.createPDF(outputStream);
    
            outputStream.close();
    }
    

    其次,我插入了与 basePath

    相关的 css 文件
    <html xmlns:th="http://www.thymeleaf.org">
    <head>
        <link rel="stylesheet" type="text/css" media="all" href="style.css"/>
    </head>
    

    【讨论】:

      猜你喜欢
      • 2017-05-26
      • 1970-01-01
      • 2017-02-28
      • 2020-08-16
      • 2019-08-07
      • 2018-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多