如何在使用SpringBoot中的itext将html页转换为pdf时隐藏此按钮

p1iqtdky  于 2021-07-23  发布在  Java
关注(0)|答案(1)|浏览(335)

我面临的问题,在pdf生成时,我转换为pdf的html页的时间按钮也显示,但要求是按钮不应显示,但按钮应在同一页上,点击后pdf应生成。我正在使用itext生成pdf。我用的是百里香和Spring Boot。

我用这个代码生成pdf。

@RequestMapping("download-pdf/{refno}")
public ResponseEntity<?> getPDF(HttpServletRequest request, HttpServletResponse response,@PathVariable("refno") Long refno) throws IOException, IllegalAccessException, InvocationTargetException {

    complaintDto = complaintRepo.findById(refno).orElse(null);
    complaintPdfBean = pdfService.getComplaintInfo(complaintDto);

    WebContext context = new WebContext(request, response, servletContext);
    context.setVariable("complaintPdfBean", complaintPdfBean);
    String grievanceHtml = templateEngine.process("complant-privew", context);

    ByteArrayOutputStream target = new ByteArrayOutputStream();
    ConverterProperties converterProperties = new ConverterProperties();
    converterProperties.setBaseUri("http://localhost:8080");
    HtmlConverter.convertToPdf(grievanceHtml, target, converterProperties);
    byte[] bytes = target.toByteArray();

    return ResponseEntity.ok().header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=grievance.pdf")
            .contentType(MediaType.APPLICATION_PDF).body(bytes);

}


我在pom.xml文件中使用此maven依赖项:

<dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>itext7-core</artifactId>
        <version>7.1.0</version>
        <type>pom</type>
    </dependency>
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>html2pdf</artifactId>
        <version>2.0.0</version>
    </dependency>
    <!-- pdf dependency -->
11dmarpk

11dmarpk1#

把你的纽扣包起来 <th:block> 和一个 th:unless 声明:

<th:block th:unless="${isPdfExport}">
 ...
</th:block>

设置 isPdfExport 上下文变量到 true 就在pdf生成之前。

相关问题