[英]PdfConverter in Android won't load local images
I'm trying to generate a pdf from HTML code using images stored in memory.我正在尝试使用存储在 memory 中的图像从 HTML 代码生成 pdf。 The URIs are correct and the files exist.
URI 正确且文件存在。 With WebView, we can use
webView.getSettings().setAllowFileAccess(true);
使用 WebView,我们可以使用
webView.getSettings().setAllowFileAccess(true);
in order to allow it to access files to manage them properly but, when it comes to PdfConverter , it seems like there is no option to allow access to files and, thereby, the images don't appear at all in the pdf generated.为了允许它访问文件以正确管理它们,但是当涉及到PdfConverter时,似乎没有允许访问文件的选项,因此,图像根本不会出现在生成的 pdf 中。 The PdfConverter uses a WebView and I tried to add the prior line of code to it, but it didn't do the trick.
PdfConverter 使用 WebView,我尝试将前面的代码行添加到它,但它没有成功。
What's the workaround for this?解决方法是什么?
private void shareOrder(String html) {
PdfConverter converter = PdfConverter.getInstance();
String fileName = "foo.pdf"
File file = new File(getCacheDir(), fileName);
converter.convert(this, html, file);
Intent shareIntent = new Intent();
Uri uri = FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID + ".provider", file);
if (uri != null) {
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); // temp permission for receiving app to read this file
shareIntent.setDataAndType(uri, getContentResolver().getType(uri));
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(shareIntent, getResources().getString(R.string.send_to)));
} else {
Log.e("PreviewOrder", "URI not found");
}
send(true);
finish();
}
HTML to be converted (keep in mind that the APPLICATION_ID is the package name of the actual project):要转换的 HTML (请记住,APPLICATION_ID 是实际项目的 package 名称):
<!DOCTYPE html>
<html>
<head>
<tr>
<td width='100%' colspan='1' style='padding: 10px 0px;'>
<h3 style='position: absolute; top: -10px; color:red;'>Draft</h3>
</td>
<td width='100%' colspan='1' style='padding: 10px 0px;'>
<p align='right' style='position: relative;'>Generated by <a target='_blank' href='https://example.com/'><b>https://example.com/</b></a></p>
</td>
</tr>
<img
style='height:auto;max-width:150px;' src='file:///data/user/0/[APPLICATION_ID]/cache/orders_logo.jpg' onerror='this.style.display = \"none\"'>
<div style='font-size:25px;font-weight: bold;'>Company name</div>
</head>
<body>
<table width='100%'>
<tbody>
<tr>
<td width='100%' colspan='2'>
<hr style='border-color:black; border-width: 2px;'>
</td>
</tr>
<tr>
<td width='100%' colspan='2' style='padding: 10px 0px;'>COMPANY</td>
</tr>
<tr style="background-color:LightGray;">
<td width='100%' style='border-bottom: 1px solid black;font-weight:bold;' colspan='2'>Order Details</td>
</tr>
<tr>
<td width='100%' colspan='2'> </td>
</tr>
<tr>
<td width='50%'>Client:</td>
<td width='50%' style='font-weight:bold;'>CLIENT</td>
</tr>
<tr>
<td width='50%'>Date:</td>
<td width='50%' >DATE</td>
</tr>
<tr>
<td width='50%'>User:</td>
<td width='50%'>(44) <b>USER</b></td>
</tr>
<tr>
<td width='50%' style='vertical-align: top;'>Address:</td>
<td width='50%' id='dir'>ADDRESS</td>
</tr>
<tr>
<td width='50%' style='vertical-align:top;'>Map:</td>
<td width='50%'><a target='_blank' href='https://www.google.com/maps?q=0,0'><img width='100%' style='margin-top: 5px; max-width: 300px; height: auto;' src='file:///data/user/0/[APPLICATION_ID]/cache/map.png'></a></td>
</tr>
<tr>
<td width="50%">Client Id: </td>
<td width="50%">ID</td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr style="background-color:LightGray;">
<td width='100%' style='border-bottom: 1px solid black;font-weight:bold;' colspan='2'>Products details</td>
</tr>
<tr>
<td width='100%' colspan='2'> </td>
</tr>
<tr>
<td colspan='2'>
<table border='1' style='border-spacing:0px;border-collapse: collapse;' cellspacing='0'cellpadding='0' width='100%'>
<thead>
<tr>
<th width='10%'>SKU</th>
<th width='40%'>Product name</th>
<th width='25%'>Price</th>
<th width='10%'>Quantity</th>
<th width='10%'>Discount</th>
<th width='25%'>Total price</th>
</tr>
</thead>
<tbody>
<tr>
<td>ID</td>
<td>PRODUCT</td>
<td align="center">PRICE</td>
<td align="center">QUANTITY</td>
<td align="center">DISCOUNT</td>
<td align="right">TOTAL</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td width='20%'>Total:</td>
<td width='10%' style='text-align:right;'>Total: 989.85 €</td>
</tr>
<tr>
<td width='50%' style='vertical-align:top;'> </td>
</tr>
</tbody>
</table>
</body>
</html>
I managed to fix it implementing these two dependencies:我设法修复它实现这两个依赖项:
implementation 'com.tom-roush:pdfbox-android:1.8.10.3'
implementation 'com.facebook.react:react-native:+'
Then, in the PdfConverter
class, I replaced the one that I took from the link indicated in my question by this one .然后,在
PdfConverter
class 中,我用这个替换了我从问题中指出的链接中获取的那个。
To convert the HTML to pdf, I did the following:要将 HTML 转换为 pdf,我执行了以下操作:
PdfConverter converter = PdfConverter.getInstance();
String fileName = "foo.pdf";
File file = new File(getCacheDir(), fileName);
try {
converter.convert(this, html, file, true, null, null, null);
} catch (Exception e) {
Log.e(TAG, "Conversion of HTML to PDF unsuccessful");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.