Skip to content
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

Open
testpasha777 opened this issue Sep 30, 2020 · 5 comments
Open

html without .ttf font #563

testpasha777 opened this issue Sep 30, 2020 · 5 comments

Comments

@testpasha777
Copy link

testpasha777 commented Sep 30, 2020

Hello, if html haven't .ttf font
Can i set some default font in code to solve this?

@danfickle
Copy link
Owner

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");

@testpasha777
Copy link
Author

Still have problem with html without .ttf font. When mutate document and useFont java.lang.NullPointerException: null
at com.openhtmltopdf.pdfboxout.PdfBoxFontResolver.resolveFont
but .ttf font loaded com.openhtmltopdf.load INFO:: Loading font(raleway) from PDFont supplier now.

if use html with .ttf font and mutate document all works fine
com.openhtmltopdf.load INFO:: Loading font(raleway) from PDFont supplier now.
com.openhtmltopdf.load INFO:: Loading font(sansationregular) from InputStream supplier now.
com.openhtmltopdf.load INFO:: Loading font(sansationbold) from InputStream supplier now.

@danfickle
Copy link
Owner

Any chance of aminimal sample?

@testpasha777
Copy link
Author

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();
            }

@danfickle
Copy link
Owner

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:
builder.useFastMode().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants