gpt4 book ai didi

java - OpenHTMLToPDF:将自定义字体嵌入到由 HTML 创建的 PDF 中

转载 作者:行者123 更新时间:2023-11-30 01:53:18 28 4
gpt4 key购买 nike

我使用 Jsoup 和 OpenHTMLToPDF 从 HTML 创建 PDF 。我必须在 PDF 中使用不同的字体来覆盖非拉丁字形(请参阅 here )。如何正确嵌入我的字体?

重现问题的简化程序:

src/main/resources/test.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Font Test</title>
<style>
@font-face {
font-family: 'source-sans';
font-style: normal;
font-weight: 400;
src: url(fonts/SourceSansPro-Regular.ttf);
}
</style>
</head>
<body>
<p style="font-family: 'source-sans',serif">Latin Script</p>
<p style="font-family: 'source-sans',serif">Είμαι ελληνικό κείμενο.</p>
</body>
</html>
  • 此文件应编写为 PDF
  • 在浏览器中它看起来正确并使用 Source Sans 字体。

src/main/java/main.java:

import com.openhtmltopdf.extend.FSSupplier;
import com.openhtmltopdf.pdfboxout.PdfRendererBuilder;
import org.jsoup.Jsoup;
import org.jsoup.helper.W3CDom;
import org.w3c.dom.Document;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Objects;

public class main {
public static void main(String[] args) {
System.out.println("Starting");

try {

final W3CDom w3cDom = new W3CDom();
final Document w3cDoc = w3cDom.fromJsoup(Jsoup.parse(readFile()));
final OutputStream outStream = new FileOutputStream("test.pdf");

final PdfRendererBuilder pdfBuilder = new PdfRendererBuilder();
pdfBuilder.useFastMode();
pdfBuilder.withW3cDocument(w3cDoc, "/");
pdfBuilder.useFont(new File(main.class.getClassLoader().getResource("fonts/SourceSansPro-Regular.ttf").getFile()), "source-sans");
pdfBuilder.toStream(outStream);

pdfBuilder.run();
outStream.close();

} catch (Exception e) {
System.out.println("PDF could not be created: " + e.getMessage());
}

System.out.println("Finish.");
}


private static String readFile() throws IOException {
final ClassLoader classLoader = main.class.getClassLoader();
final InputStream inputStream = classLoader.getResourceAsStream("test.html");
final StringBuilder sb = new StringBuilder();
final Reader r = new InputStreamReader(Objects.requireNonNull(inputStream), StandardCharsets.UTF_8);
char[] buf = new char[1024];
int amt = r.read(buf);
while(amt > 0) {
sb.append(buf, 0, amt);
amt = r.read(buf);
}
return sb.toString();
}
}
  • 不用担心第二个函数,它只是读取 HTML 文件,并且仅包含在此处,以获得完整的程序。

src/main/resources/fonts/SourceSansPro-regular.ttf

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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>paf</groupId>
<artifactId>test</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>7</source>
<target>7</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.openhtmltopdf</groupId>
<artifactId>openhtmltopdf-pdfbox</artifactId>
<version>0.0.1-RC18</version>
</dependency>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.11.2</version>
</dependency>
</dependencies>
</project>

程序输出:

Starting
com.openhtmltopdf.load INFO:: TIME: parse stylesheets 148ms
com.openhtmltopdf.match INFO:: media = print
com.openhtmltopdf.match INFO:: Matcher created with 147 selectors
com.openhtmltopdf.load INFO:: Loading font(source-sans) from InputStream supplier now.
com.openhtmltopdf.exception WARNING:: bad URL given: /fonts/SourceSansPro-Regular.ttf
com.openhtmltopdf.exception WARNING:: Could not load @font-face font: /fonts/SourceSansPro-Regular.ttf
com.openhtmltopdf.exception WARNING:: Font metrics not available. Probably a bug.
com.openhtmltopdf.exception WARNING:: Font metrics not available. Probably a bug.
com.openhtmltopdf.render WARNING:: Font is null.
com.openhtmltopdf.render WARNING:: Font is null.
com.openhtmltopdf.render WARNING:: Font is null.
com.openhtmltopdf.render WARNING:: Font is null.
com.openhtmltopdf.render WARNING:: Font is null.
com.openhtmltopdf.render WARNING:: Font is null.
com.openhtmltopdf.render WARNING:: Font is null.
Finish.

生成的 PDF

Latin Script
##### ######## #######.
  • 衬线字体。
<小时/>

编辑1:根据评论中链接的页面进行各种更改并更新到RC18。现在有新输出,但 PDF 中的字体仍然不正确。

<小时/>

编辑2:尝试快速渲染器

最佳答案

好的。感谢 @Tilman Hausherr 的评论,我在 openhtmltopdf 的 GitHub-Issue Tracker and got some help 中询问。 .

如果有人对此感兴趣,这些更改使其发挥作用:

src/main/java/main.java(仅更改部分,请参阅上面的其余部分):

    public static void main(String[] args) {
System.out.println("Starting");

try {

final W3CDom w3cDom = new W3CDom();
final Document w3cDoc = w3cDom.fromJsoup(Jsoup.parse(readFile()));
final OutputStream outStream = new FileOutputStream("test.pdf");

final PdfRendererBuilder pdfBuilder = new PdfRendererBuilder();
pdfBuilder.useFastMode();
pdfBuilder.withW3cDocument(w3cDoc, "/");
pdfBuilder.useFont(new File(main.class.getClassLoader().getResource("fonts/SourceSansPro-Regular.ttf").getFile()), "source-sans");
pdfBuilder.toStream(outStream);

pdfBuilder.run();
outStream.close();

} catch (Exception e) {
System.out.println("PDF could not be created: " + e.getMessage());
}

System.out.println("Finish.");
}

src/main/resources/fonts/SourceSansPro-regular.ttf

来自 src/main/resources/test.html(仅更改部分,请参阅上面的其余部分)

        @font-face {
font-family: 'source-sans';
font-style: normal;
font-weight: 400;
src: url(fonts/SourceSansPro-Regular.ttf);
-fs-font-subset: complete-font;
}

关于java - OpenHTMLToPDF:将自定义字体嵌入到由 HTML 创建的 PDF 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55339298/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com