JSOUPを使用してHTMLからPDFを作成し, OpenHTMLTopDF 。私はラテン語以外のGlyphcsをカバーするために私のPDFで異なるフォントを使用しなければなりません( ここ )。フォントを正しく埋め込むにはどうすればよいですか。
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>
_
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();
}
}
_
SRC /メイン/リソース/フォント/ SourceSansPro-Regulary.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.
_
_Latin Script
##### ######## #######.
_
編集1:コメントにリンクされているページに従ってさまざまな変更を加え、RC18に更新されます。 NEW OUTPUT現在、PDFではまだ正しくありません。
編集2:Fast Rendererを試しました
わかった。 @tilman Hausherrのコメントのおかげで、私はOpenHTMLTopDFのGithub-issue Trackerで尋ねました そしていくつかの助けを得た 。
ここに着陸している誰かが興味を持っているならば、Theeseの変化はそれを働かせました:
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 /メイン/リソース/フォント/ SourceSansPro-Regulary.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;
}
_