Android PDF字体不可读取

发布于 2025-02-10 13:55:24 字数 2778 浏览 1 评论 0原文

我正在使用React-Native-HTML-TO-PDF将HTML转换为PDF和React-Native-PDF以查看PDF。

在过去的几天中,一些Android用户报告说,生成的PDF是不可读的。下图是一个示例:

​第一天,PDF在该手机中可以阅读,没有任何问题。一段时间后,PDF在新手机上也变得不可读。

另一方面,有一些Android手机“部分地”面对此问题。在某些Android手机中,如果我们设置HTML 字体重量:BOLD PDF显示完美。但是不幸的是,它在所有手机中都没有起作用。

字体重量:BOLD的电话:

  1. Redmi
  2. Samsung

电话,我们找不到任何解决方案:

  1. Vivo
  2. Oppo

,我猜这可能是由于某些Android更新而发生的。挖掘后,我们发现react-native-html-to-pdfreact-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:

enter image description here

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:

  1. Redmi
  2. Samsung

Phones where we could not find any solution:

  1. Vivo
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一萌ing 2025-02-17 13:55:24

遇到了同样的问题。此问题是因为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.

海未深 2025-02-17 13:55:24

我已经成功地为iOS和Android实施了这一点。

以下是

Libs:React-native-html-to-pdf,react-native print(请使用该库代替打印,更容易)

导入

import RNHTMLtoPDF from 'react-native-html-to-pdf';
import RNPrint from 'react-native-print';

选项(甚至可以嵌入样式)

let options = {
    html: '...', //plain html like <div style="width: 100%; display: inline-flex; margin-top: -20px; margin-bottom: 20px;"><div style="flex: 1; margin: 5px;"><h5>FİRMA: ' + printedEnterpriseName + '</h5></div></div></div>
    fileName: 'test'
};

打印

let file = await RNHTMLtoPDF.convert(options)
await RNPrint.print({filePath: file.filePath});

进一步读取

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

import RNHTMLtoPDF from 'react-native-html-to-pdf';
import RNPrint from 'react-native-print';

Options (You can even embed styling)

let options = {
    html: '...', //plain html like <div style="width: 100%; display: inline-flex; margin-top: -20px; margin-bottom: 20px;"><div style="flex: 1; margin: 5px;"><h5>FİRMA: ' + printedEnterpriseName + '</h5></div></div></div>
    fileName: 'test'
};

Print

let file = await RNHTMLtoPDF.convert(options)
await RNPrint.print({filePath: file.filePath});

Further reading

https://reactnativeexample.com/print-documents-using-react-native/

https://aboutreact.com/react-native-print-html/

汐鸠 2025-02-17 13:55:24

现在没有解决方案。 @saikrishna也得到了证实。但是,我找到了另一种选择。

我已经分享了解决方案在这里

There is no solution for this right now. @Saikrishna confirmed also. However, I found an alternative.

I have shared the solution here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文