简体   繁体   中英

iText7 don't work when I use CSS3 function target-counter in property content

I am very new to iText7. I am trying to create a pdf from a dynamic HTML string. So far I have been able to create the pdf using HtmlConverter.ConvertToPdf(). But the problem is that I need have a table of contents with chapters and page numbers at begin of the document. To do that, I wrote in my CSS file:

@page {
  margin: 40mm 17mm 17mm 17mm;
  size: A4 portrait;
  @top-center { 
    content: element(header); 
    width: 100%;
  }
  @bottom-right-corner {
    content: counter(page);
  }
}

a::after { 
  content: leader('.') target-counter(attr(href), page) 
}

And I wrote in my HTML file:

<!DOCTYPE HTML>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Good Thymes Virtual Grocery</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link rel="stylesheet" th:href="@{${baseUrl} + '/static/css/relatorio_fiscalizacao.css'}"/>
</head>
<body>
  <h1>ÍNDICE</h1>
  <ul style="page-break-after: always;">
    <li><a href="#ch1">STAFF</a></li>
    <li><a href="#ch2">OPERATION DATA</a></li>
  </ul>

  <h1 id="ch1" class="chapter">STAFF</h1>
  <p style="page-break-after: always;">....</p>

  <h1 id="ch3" class="chapter">OPERATION DATA</h1>
  <p style="page-break-after: always;">....</p>

</body>
</html>

And finally, I have a component in my Spring Boot application:

@Component
public class PdfGeneratorUtil {
@Autowired
private TemplateEngine templateEngine;

@Autowired
ServletContext servletContext;

@Autowired
private ApplicationContext context;

@Value("${baseUrl}")
private String baseUrl;

public ByteArrayOutputStream createPdf(String templateName, Map<String, Object> map, final HttpServletRequest request, final HttpServletResponse response) throws Exception {
    Assert.notNull(templateName, "The templateName can not be null");

    System.out.println(baseUrl);

    map.put("baseUrl", baseUrl);

    IWebContext ctx = new SpringWebContext(request, response, servletContext, LocaleContextHolder.getLocale(), map, context);


    String processedHtml = templateEngine.process(templateName, ctx);
    ByteArrayOutputStream os = new ByteArrayOutputStream();

    try {
        ConverterProperties converterProperties = new ConverterProperties();
        HtmlConverter.convertToPdf(processedHtml, os, converterProperties);
        System.out.println("PDF created successfully");
    }
    finally {
        if (os != null) {
            try {
                os.close();
            } catch (IOException e) { /*ignore*/ }
        }
    }

    return os;
    }
}

iText converts to Pdf fine. But the chapters from table of contens come up without the number of pages. And Log returns "Content property target-counter is either invalid or uses unsupported function."

I saw the file CssContentPropertyResolver.java and I realized that the code not treat the CSS function "target-counter". So, my question is: There is an other way to do that, maybe creating custom CSS appliers like this tutorial ? Or maybe other way? If not, anybody know any other library that I could use instead iTextPdf?

This works with recent versions of the html2pdf library - certainly 3.0.3 and above.

I have the same issue, only with the leader() function, which makes the line invalid. Try and remove the leader() and see if you get the page numbers. I'm trying to find another way to create the leading dots.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM