Solution 1 :
I have a similar problem with fonts when I try to call activity.getSystemService(android.content.Context.PRINT_SERVICE)).print()
with an object returned from an android.webkit.WebView
‘s createPrintDocumentAdapter()
, so I think the problem is not in PdfView but in the Android system itself.
In my case, if in the CSS I try to say font-family: Times New Roman
(which normally maps to NotoSerif, but you can’t ask for NotoSerif by name, you have to ask for something like Times New Roman), it works both on-screen and in-PDF on a simulated Pixel 2 running either Android 10 or Android 12, and on a real Samsung Galaxy S9 running Android 10, but when I move over to a Samsung Galaxy S21 running Android 12, it works on-screen but not in-PDF and the font that gets substituted in the PDF is missing characters like ǎ (which is a big deal in my case as I’m trying to write Pinyin pronunciation aids for Chinese and I end up with missing letters).
Both devices have the same version of the Android System Webview app (102.0.5005.78), so I guess something must be different in the print service of Android 12 on Samsung which Webview has not yet been updated to match.
At least I don’t get letters missing in PDF if I set font-family: sans-serif
instead (also works if I just put it into an @media(print)
section of the CSS), so I will probably be working around my missing letters by adding that to the CSS if I detect API level 31 or above (or perhaps level 30 if nobody can confirm that Android 11 doesn’t have this problem). Unfortunately I’m not sure how to raise a bug with the relevant developers properly.
Problem :
I’m using PdfView.createWebPrintJob to convert Webview into PDF. So far I can successfully convert except that font style is not loading properly when viewing the PDF. I’m using google font Noto-Serif, and it looks like it works only when font-weight is bold or above 600.
Here is my html:
<html>
<head>
<style type="text/css">
@font-face {
font-family: 'Noto';
font-style: normal;
src: url('NotoSerif-Regular.ttf');
}
p.normal {
font-family: 'Noto';
font-size:14px;
}
p.bold {
font-family: 'Noto';
font-size:18px;
font-weight: bold;
}
p.large{
font-family: 'Noto';
font-weight: 600;
font-size:18px;
}
</style>
</head>
<body>
<p class="normal">Normal - not working</p>
<p class="bold">Bold - working</p>
<p class="large">W600 - working</p>
</body>
</html>
In the webview, there’s no issue loading the custom font:
Now when viewing the PDF, the font is not displaying correctly for the normal font style in most PDF viewers. But it displays properly when font style is bold or weight>=600.
Tested in Android 10 and there’s no issue. So far, it only happens in Android 12.
What could be the issue? Any help will be appreciated.
Thanks!