我需要在完成课程后生成PDF证书.这些pdf是由带有百里叶的HTML模板生成的.通过转换一切正常,但结果显示背景图像周围出现白色边框.背景图像分辨率为1920x1440. 下面是我在Spring Boot 3.1.0上的代码:

public String parseThymeleafTemplate(String verificationCode, Long certificateId) {
        ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
        templateResolver.setSuffix(".html");
        templateResolver.setTemplateMode(TemplateMode.HTML);

        TemplateEngine templateEngine = new SpringTemplateEngine();
        templateEngine.setTemplateResolver(templateResolver);

        User student = User.builder()
                .surname("Test_surname").name("Test_name").build();
        User mentor = User.builder()
                .surname("Test_mentor").name("Test_mentor").position("Java dev").build();
        Course course = Course.builder()
                .mentor(mentor)
                .name("Java Developer for 3 weeks").build();
        }

        Certificate certificate = Certificate.builder()
                .id(54L)
                .course(course)
                .student(student)
                .verificationCode(verificationCode)
                .issuedDate(LocalDate.now())
                .build();
        Context context = new Context();
        context.setVariable("company_name", "test_company");
        context.setVariable("student_name", certificate.getStudent().getSurname() + " " + certificate.getStudent().getName());
        context.setVariable("course_name", certificate.getCourse().getName());
        context.setVariable("mentor_name", certificate.getCourse().getMentor().getSurname() + " " + certificate.getCourse().getMentor().getName());
        context.setVariable("issued_date", certificate.getIssuedDate());
        context.setVariable("verification_code", certificate.getVerificationCode());
        String generatedFileName = this.generatePdfFromHtml(templateEngine.process("pdf_cert_temp", context),
                "Java", certificate);
        certificate.setFileName(generatedFileName);
        return generatedFileName;
    }

    public String generatePdfFromHtml(String html, String courseTag, Certificate certificate) {
        String fileName = courseTag + "_" +
                transliterateUtil.transliterateTextFromRussianToLatin(certificate.getStudent().getName()) + "_" +
                certificate.getIssuedDate().toString() + "_" + certificate.getId() +
                CertStrings.CERT_EXTENSION;
        String outputFolder = CertStrings.PATH_TO_PDF_CERTS + File.separator + fileName;
        try (OutputStream outputStream = new FileOutputStream(outputFolder)) {
            ITextRenderer renderer = new ITextRenderer();
            renderer.setDocumentFromString(html);
            renderer.layout();
            renderer.createPDF(outputStream);
        } catch (IOException e) {
            throw new RuntimeException(CertStrings.ERROR_WHILE_OUTPUT_STREAM + e);
        } catch (DocumentException e) {
            throw new RuntimeException(CertStrings.ERROR_CREATING_PDF + e);
        }
        return fileName;
    }

这是我的Thymeleaf模板,用于创建PDF

<html xmlns:th="http://www.thymeleaf.org">
<head>
    <style th:inline="text">
        @page {
            size: 1920px 1440px;
        }

        body {
            font-family: Arial, sans-serif;
            background-image: url(http://some_url/files/cert_templates/java_template_bg.jpg);
            background-size: cover;
            background-position: center;
            background-repeat: no-repeat;
            width: 100%;
            height: 100%;
            margin: 0px;
            padding: 0px;
            box-sizing: border-box;
        }

        .company-name {
            text-align: center;
            color: #D9AF58;
            font-style: italic;
            font-size: 70px;
            font-weight: 100;
            position: absolute;
            margin-top: 5%;
            left: 44%;
        }

        .student-name {
            text-align: center;
            color: #D9AF58;
            font-family: "Times New Roman", Times, serif;
            font-size: 70px;
            font-weight: 100;
            position: absolute;
            margin-top: 31%;
            left: 38.5%;
        }

        .course-completing {
            text-align: center;
            color: #D9AF58;
            font-family: "Times New Roman", Times, serif;
            font-weight: 100;
            font-size: 51px;
            position: absolute;
            margin-top: 36%;
            left: 30%;
        }

        .course-description {
            text-align: center;
            color: #D9AF58;
            font-family: "Times New Roman", Times, serif;
            font-size: 50px;
            font-weight: 100;
            position: absolute;
            margin-top: 40%;
            left: 35%;
        }

        .mentor-name {
            text-align: center;
            color: #D9AF58;
            font-family: "Times New Roman", Times, serif;
            font-size: 30px;
            font-weight: 100;
            position: absolute;
            margin-top: 44%;
            left: 39%;
        }

        .verification-code {
            text-align: center;
            color: #D9AF58;
            font-size: 25px;
            font-weight: 100;
            position: absolute;
            margin-top: 46%;
            left: 34.5%;
        }

        .issued-date {
            text-align: left;
            color: #D9AF58;
            font-size: 50px;
            font-weight: 100;
            position: absolute;
            margin-top: 55%;
            left: 21.1%;
        }

        .signature {
            text-align: left;
            color: #D9AF58;
            font-size: 50px;
            font-weight: 100;
            position: absolute;
            margin-top: 55%;
            right: 21.5%;
        }
    </style>

</head>
<body>
<div>
    <div class="company-name">
        <span th:text="${company_name}"></span>
    </div>
    <div class="student-name">
        <span th:text="${student_name}"></span>
    </div>
    <div class="course-completing">
        <span>has successfully completing course</span>
    </div>
    <div class="course-description">
        <u th:text="${course_name}"></u>
    </div>
    <div class="mentor-name">
        Taught by <span th:text="${mentor_name}"></span>
    </div>
    <div class="verification-code">
        Verification code: <span th:text="${verification_code}"></span>
    </div>
    <div>
        <div class="issued-date">
            <span th:text="${issued_date}"></span>
        </div>
        <div class="signature">
            <span th:text="'Test S.'"></span>
        </div>
    </div>
</div>
</body>
</html>

这是我的pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.1.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>PdfGeneratorTest</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>PdfGeneratorTest</name>
    <description>PdfGeneratorTest</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.ibm.icu</groupId>
            <artifactId>icu4j</artifactId>
            <version>69.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.xhtmlrenderer</groupId>
            <artifactId>flying-saucer-pdf</artifactId>
            <version>9.1.22</version>
        </dependency>
        <dependency>
            <groupId>ognl</groupId>
            <artifactId>ognl</artifactId>
            <version>3.2.10</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.12.0</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

这是一个示例,如下所示: Example of generated PDF

try 修改HTMLtempalte,但不起作用.调整图像或输出页面的大小也不起作用.

推荐答案

这是正常的打印行为,它只是添加正常的打印机"卡爪边距"(这样油墨就不会污染滚筒).您将需要添加css以将打印机介质边距减少到"无边框"

enter image description here

一种方法是将正文的@Media Print设置为Zer0

enter image description here

这可能不会精确地删除所有白色区域(没有微小的像素或两个变化),因为zoom 比例通常会有一些单位舍入,这可能会因浏览器而异,并且通常取决于当前页面zoom .

下面是我用来减小打印机边框的粗略值

<head>
    <style th:inline="text">
        @media print {
        @page {
        size: 1920px 1440px;
        border: 0px;
        margin: 0px;
        padding: 0px;
        }
        }

Java相关问答推荐

scanner 如何在执行hasNextLine一次后重新读取整个文件?

尽管类型擦除,instanceof与泛型在Java中如何工作?

';com.itextpdf.ext.html.WebColors已弃用

无法在WebSocket onMessage中捕获错误

Chunk(Int)已弃用并标记为要删除

如何正确创建序列图?

解释左移在Java中的工作原理

更新AWS凭据

SpringBoot:在条件{Variable}.isBlank/{Variable}.isEmpty不起作用的情况下进行路径变量验证

使用While循环打印素数,无法正常工作

Java中不兼容的泛型类型

Quarkus:运行时出现EnumConstantNotPresentException

有没有办法在o(log(N))中以系统的方式将数组中的小块元素复制和移动到新增长的数组中的左侧?

基于Java中mm/dd/yy格式的最近日期对数组列表进行排序

在ECLIPSE上的M1 Pro上运行JavaFX的问题

在权限列表中找不到我的应用程序

org.springframework.web.HttpRequestMethodNotSupportedException:请求方法';帖子';不支持

当我try 返回可选时,Mock无法正常工作

为什么当我输入变量而不是直接输入字符串时,我的方法不起作用?

UuidGenerator Bean 类型不匹配?