-
Notifications
You must be signed in to change notification settings - Fork 365
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
html without .ttf font #563
Comments
No, currently you cannot register a default font. However, you can mutate the document before rendering: private static final String EXTRA_STYLE =
"* { font-family: 'noto'; }";
builder.addDOMMutator(doc -> {
Element style = doc.createElement("style");
Node node = doc.createTextNode(EXTRA_STYLE);
style.appendChild(node);
doc.getElementsByTagName("head").item(0).appendChild(style);
});
builder.useFont(new File("path/to/noto.ttf"), "noto"); |
Still have problem with html without .ttf font. When mutate document and useFont java.lang.NullPointerException: null if use html with .ttf font and mutate document all works fine |
Any chance of aminimal sample? |
org.w3c.dom.Document doc = DOMBuilder.jsoup2DOM(
Jsoup.connect(url)
.timeout(timeout)
.validateTLSCertificates(false).get());
try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
PdfRendererBuilder builder = new PdfRendererBuilder();
builder.withW3cDocument(doc, url);
builder.toStream(os);
final String EXTRA_STYLE =
"* { font-family: 'raleway'; }";
builder.addDOMMutator(document -> {
Element style = document.createElement("style");
Node node = document.createTextNode(EXTRA_STYLE);
style.appendChild(node);
document.getElementsByTagName("head").item(0).appendChild(style);
});
builder.useFont(new File(Html2PdfService.class.getResource("/font/Raleway-Regular.ttf").toURI()), "raleway");
builder.run();
return os.toByteArray();
} |
As a first thought, you need to take into account that a resource is not always a file. You could use the inputstream method to add a font instead. Also, remember to use fast mode which will be the default after the next release: |
Hello, if html haven't .ttf font
Can i set some default font in code to solve this?
The text was updated successfully, but these errors were encountered: