Android PDF字体不可读取
我正在使用React-Native-HTML-TO-PDF
将HTML转换为PDF和React-Native-PDF
以查看PDF。
在过去的几天中,一些Android用户报告说,生成的PDF是不可读的。下图是一个示例:
第一天,PDF在该手机中可以阅读,没有任何问题。一段时间后,PDF在新手机上也变得不可读。
另一方面,有一些Android手机“部分地”面对此问题。在某些Android手机中,如果我们设置HTML 字体重量:BOLD
PDF显示完美。但是不幸的是,它在所有手机中都没有起作用。
字体重量:BOLD的电话:
- Redmi
- Samsung
电话,我们找不到任何解决方案:
- Vivo
- Oppo
,我猜这可能是由于某些Android更新而发生的。挖掘后,我们发现react-native-html-to-pdf
和react-native-native-pdf
都使用WebView分别用于生成和查看PDF。可能是因为一些Android WebView更新引起了这个问题?我计划在这些图书馆存储库中创建一个问题,但我认为它们都没有积极维护。
任何建议将被赞赏。
更新 在进行了更多的挖掘之后,我们发现这是由于将HTML转换为PDF而发生的。
生成PDF的代码:
// @author: https://github.com/mddanishansari/
var pdfGenerationStarted = false
try {
val pdfWebView = WebView(context)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
pdfWebView.settings.safeBrowsingEnabled = false
}
val attributes = getPrintAttributes()
val printAdapter = getPrintAdapter(pdfWebView, Math.random().toString())
pdfWebView.webViewClient = object : WebViewClient() {
override fun onPageFinished(view: WebView?, url: String?) {
super.onPageFinished(view, url)
if (pdfWebView.progress == 100 && !pdfGenerationStarted) {
pdfGenerationStarted = true
val pdfPrinter = PdfPrinter(attributes)
pdfPrinter.generate(printAdapter, pdfLocation, onPdfGenerated)
Log.i("PDFConverter","HTML has been loaded");
}
}
}
pdfWebView.loadDataWithBaseURL(baseUrl, htmlString, "text/html", "UTF-8", null)
打印适配器
printAdapter.onLayout(null, printAttributes, null, object : LayoutResultCallback() {
override fun onLayoutFinished(info: PrintDocumentInfo, changed: Boolean) {
printAdapter.onWrite(
arrayOf(PageRange.ALL_PAGES),
getOutputFile(path),
CancellationSignal(),
object : WriteResultCallback() {
override fun onWriteFinished(pages: Array<PageRange>) {
super.onWriteFinished(pages)
onPdfGenerated(path)
}
})
}
}, null)
I'm using react-native-html-to-pdf
to convert HTML to PDF and react-native-pdf
to view PDF.
For past few days, some of the Android users reported that the generated PDF is not readable. This image below is one the examples:
To debug this issue, we bought a new android 12 phone. First day the PDF was readable in that phone without any issue. After a while, the PDF became unreadable on the new phone also.
On the other side, there are some android phones are facing this issue "partially". In some android phones if we set the html font-weight:bold
the PDF is showing perfectly. But unfortunately it's not working in all the phones.
Phones where font-weight:bold is working:
- Redmi
- Samsung
Phones where we could not find any solution:
- Vivo
- Oppo
I'm guessing it's happening maybe because of some Android update. After some digging we found out that both react-native-html-to-pdf
and react-native-pdf
is using WebView for generating and viewing pdf respectively. Could it be because of some Android WebView update causing this issue? I planned create an issue in those library repositories but I think both of them are not actively maintained.
Any suggestion is appreciated.
UPDATE
After some more digging we found out that this happens due to converting HTML to PDF.
Code for Generating PDF:
// @author: https://github.com/mddanishansari/
var pdfGenerationStarted = false
try {
val pdfWebView = WebView(context)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
pdfWebView.settings.safeBrowsingEnabled = false
}
val attributes = getPrintAttributes()
val printAdapter = getPrintAdapter(pdfWebView, Math.random().toString())
pdfWebView.webViewClient = object : WebViewClient() {
override fun onPageFinished(view: WebView?, url: String?) {
super.onPageFinished(view, url)
if (pdfWebView.progress == 100 && !pdfGenerationStarted) {
pdfGenerationStarted = true
val pdfPrinter = PdfPrinter(attributes)
pdfPrinter.generate(printAdapter, pdfLocation, onPdfGenerated)
Log.i("PDFConverter","HTML has been loaded");
}
}
}
pdfWebView.loadDataWithBaseURL(baseUrl, htmlString, "text/html", "UTF-8", null)
Print Adapter
printAdapter.onLayout(null, printAttributes, null, object : LayoutResultCallback() {
override fun onLayoutFinished(info: PrintDocumentInfo, changed: Boolean) {
printAdapter.onWrite(
arrayOf(PageRange.ALL_PAGES),
getOutputFile(path),
CancellationSignal(),
object : WriteResultCallback() {
override fun onWriteFinished(pages: Array<PageRange>) {
super.onWriteFinished(pages)
onPdfGenerated(path)
}
})
}
}, null)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
遇到了同样的问题。此问题是因为Android系统WebView版本。当前处于稳定的版本中,将HTML转换为PDF时会出现问题。
该修复程序可在版本中提供:103.0.5060.70
请更新系统WebView版本和检查。
Encountered the same issue. This issue is because of Android System Webview version. The version which is currently in stable has issues when converting HTML to PDF.
The fix is available in version : 103.0.5060.70
Please update the system webview version and check.
我已经成功地为iOS和Android实施了这一点。
以下是
Libs:React-native-html-to-pdf,react-native print(请使用该库代替打印,更容易)
导入
选项(甚至可以嵌入样式)
打印
进一步读取
https://reaeactnativeexample.com/print-documents-using-react-native/
https://aboutreact.com/react-native-native-print-html/
I have implemented that for both iOS and Android successfully.
Here's how
Libs: react-native-html-to-pdf, react-native-print (please use that library instead for printing, much easier)
Imports
Options (You can even embed styling)
Print
Further reading
https://reactnativeexample.com/print-documents-using-react-native/
https://aboutreact.com/react-native-print-html/
现在没有解决方案。 @saikrishna也得到了证实。但是,我找到了另一种选择。
我已经分享了解决方案在这里
There is no solution for this right now. @Saikrishna confirmed also. However, I found an alternative.
I have shared the solution here