Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Can't use own fonts #183

Open
Xiirs opened this issue Mar 15, 2018 · 8 comments
Open

Can't use own fonts #183

Xiirs opened this issue Mar 15, 2018 · 8 comments

Comments

@Xiirs
Copy link

Xiirs commented Mar 15, 2018

Hi,
I tried to add custom fonts, but always with the same error. Can you advise what I am doing wrong or whether it is an internal error?

java.lang.NullPointerException
at com.openhtmltopdf.pdfboxout.PdfBoxFontResolver.resolveFont(PdfBoxFontResolver.java:70)
at com.openhtmltopdf.layout.SharedContext.getFont(SharedContext.java:391)
at com.openhtmltopdf.pdfboxout.PdfBoxOutputDevice.processControls(PdfBoxOutputDevice.java:316)
at com.openhtmltopdf.pdfboxout.PdfBoxOutputDevice.finish(PdfBoxOutputDevice.java:1000)
at com.openhtmltopdf.pdfboxout.PdfBoxRenderer.writePDF(PdfBoxRenderer.java:607)
at com.openhtmltopdf.pdfboxout.PdfBoxRenderer.createPDF(PdfBoxRenderer.java:546)
at com.openhtmltopdf.pdfboxout.PdfBoxRenderer.createPDF(PdfBoxRenderer.java:485)
at com.openhtmltopdf.pdfboxout.PdfBoxRenderer.createPDF(PdfBoxRenderer.java:481)
at com.openhtmltopdf.pdfboxout.PdfRendererBuilder.run(PdfRendererBuilder.java:86)
at cz.codeberry.oneclick.example.SimpleUsage.exportToPdfBox(SimpleUsage.java:30)
at cz.codeberry.oneclick.example.SimpleUsage.main(SimpleUsage.java:17)

My CSS:
@font-face {
src: url("./OpenSans.ttf");
font-family: "Open Sans Condensed";
font-weight: 700;
}
h1 {
font-family: "Open Sans Condensed", "Arial Narrow", Arial, sans-serif;
}

I tried I add the font directly to the builder, but also unsuccessfully:
PdfRendererBuilder builder = new PdfRendererBuilder();
builder.withFile(file);
builder.useFont(new File("./OpenSans.ttf"), "Open Sans Condensed");
builder.toStream(os);
builder.run();

@ghost
Copy link

ghost commented Mar 23, 2018

Same problem... how can we solve this?

@Xiirs
Copy link
Author

Xiirs commented Mar 23, 2018

I found out that it does this mainly for. For example, if the border, height, etc are set here. But if I give the border directly to the style in the tag, so it works...

@ghost
Copy link

ghost commented Mar 23, 2018

My problem is that I define the font in css like this:

@font-face { font-family: "example"; font-style: normal; font-weight: normal; src: url("../../fonts/Pdf/example.ttf"); }

and a little bit later I call the font like this

body { font-family: "example"; font-size: 12px; line-height: 40px; margin: 0; text-align: left; }

but it doesn't use the font in the generated pdf.

@danfickle
Copy link
Owner

Hi @Xiirs

Thanks for providing a stack trace. The trace suggests you are using form input controls. If possible I would like a minimal example with form controls that reproduces the bug you are seeing, as I can't manage to reproduce. However, for the time being, can I suggest you make sure that input controls are not using a custom font. Example below:

<html>
<head>
<style>
@font-face {
  src: url(./fonts/JustAnotherHand.ttf);
  font-family: 'handwriting';
}
* {
	font-family: 'handwriting';
}
/* Put this last to make sure controls use a built-in font. */
input, select {
	font-family: serif !important;
}
</style>
</head>
<body>
<h1>Form controls with custom font example</h1>
Some text in handwriting font.
<form>
<input name="text-input" type="text" value="Some text in normal font!"/>
</form>
</body>
</html>

@danfickle
Copy link
Owner

Hi @kuhlware

I think your issue is different. Perhaps the font is not path is not resolving correctly relative to the provided or implicit base url? Can you provide a minimal example with html and builder code? Thanks.

@Xiirs
Copy link
Author

Xiirs commented Mar 25, 2018

It crash with this.

Css: input{border: 1px black solid; width: 100%; height: 30px; font-size:18px;}

html:

@danfickle
Copy link
Owner

Hi @Xiirs

I was able to reproduce! It appears that the built-in stylesheet uses an em unit to specify the width of an input. This forces a font to be resolved at an early stage (as em is relative to font size). As soon as it is changed to a percentage value it null exceptions because no font is resolved at that point. You can leave this issue open as a fix needs to be thought of, but for a quick fix, just use a em unit in one of the size properties. Probably min-height is the most logical to use:

input { ...other-properties... min-height: 1.1em; }

@Xiirs
Copy link
Author

Xiirs commented Mar 29, 2018

Thanks. I am gonna try it.

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

No branches or pull requests

2 participants