我使用Jsoup和OpenHTMLToPDF从HTML创建PDF。 我必须在PDF中使用其他字体来覆盖非拉丁字形(请参见此处 )。 如何正确嵌入我的字体?
简化程序再现了该问题:
SRC /主/资源/ test.html中
Font Test@font-face {
font-family: 'source-sans';
font-style: normal;
font-weight: 400;
src: url(fonts/SourceSansPro-Regular.ttf);
}
Latin Script
Είμαι ελληνικό κείμενο.
该文件应以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 /主/资源/字体/ SourceSansPro-regular.ttf
的pom.xml
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">
4.0.0
paf
test
1.0-SNAPSHOT
org.apache.maven.plugins
maven-compiler-plugin
7
7
com.openhtmltopdf
openhtmltopdf-pdfbox
0.0.1-RC18
org.jsoup
jsoup
1.11.2
程序输出:
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:尝试快速渲染器