英文:
HTML to PDF produces blank page when HTML gets large
问题
我正在尝试使用模板从我的数据生成报告。使用 WebView 生成的 PDF 在我的数据超过一定数量时(在一个 for 循环中多出 1 个),工作得非常好,但在这种情况下只会生成一个空白页面。我已经检查了所有可能的答案,包括 这个,这个,这个 和 这个,但都没有成功。
以下是我代码的一部分片段:
Bitmap bitmap = callback.getMapImage();
WebView wv = new WebView(activity);
wv.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
// 打印
PrintManager printManager = (PrintManager) activity
.getSystemService(Context.PRINT_SERVICE);
String jobName = activity.getString(R.string.app_name) + " Document";
PrintDocumentAdapter printAdapter = wv.createPrintDocumentAdapter(jobName);
printManager.print(jobName, printAdapter, new PrintAttributes.Builder().build());
reference = null;
}
});
PrepareDocumentForPrintTask printTask = new PrepareDocumentForPrintTask(wv, bitmap);
printTask.execute();
而且 HTML 是在异步任务中创建的,并且如下加载:
@Override
protected void onPostExecute(Triple<Boolean, String, File> result) {
activity.runOnUiThread(() -> {
if (result.getFirst()) {
wv.loadDataWithBaseURL("", result.getSecond(), "text/html", "UTF-8", null);
reference = wv;
} else {
Toast.makeText(activity, result.getSecond(), Toast.LENGTH_LONG).show();
}
});
}
我已经添加了 largeHeap="true"
并禁用了硬件加速,但仍然没有成功。
有没有什么办法来解决这个问题?
英文:
I am trying to make a report from my data using templates. The PDF using WebView works perfectly fine until my data gets bigger than a certain number (1 more in a for-loop), then it only produces a single blank page. I've checked all possible answers here; this, this, this, and this, but no success.
Here is a snippet of my code:
Bitmap bitmap = callback.getMapImage();
WebView wv = new WebView(activity);
wv.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
//print
PrintManager printManager = (PrintManager) activity
.getSystemService(Context.PRINT_SERVICE);
String jobName = activity.getString(R.string.app_name) + " Document";
PrintDocumentAdapter printAdapter = wv.createPrintDocumentAdapter(jobName);
printManager.print(jobName, printAdapter, new PrintAttributes.Builder().build());
reference = null;
}
});
PrepareDocumentForPrintTask printTask = new PrepareDocumentForPrintTask(wv, bitmap);
printTask.execute();
And the html is created in an asyncTask and loaded as follows:
@Override
protected void onPostExecute(Triple<Boolean, String, File> result) {
activity.runOnUiThread(() -> {
if (result.getFirst()){
wv.loadDataWithBaseURL("", result.getSecond(), "text/html", "UTF-8", null);
reference = wv;
} else
Toast.makeText(activity, result.getSecond(), Toast.LENGTH_LONG).show();
});
}
I have added largeHeap= "true" and deactivated hardware acceleration but still no success.
Any idea how to fix the problem?
答案1
得分: 0
好的,这是翻译后的内容:
好的,我找到了一个解决方案来解决我的问题,使用以下代码:
String urlDoc = saveHtmlFile("report", document);
wv.loadUrl("file://" + urlDoc);
其中,saveHtmlFile() 函数如下所示:
private @Nullable String saveHtmlFile(String fileName, String html) {
try {
File outputDir = this.activity.getCacheDir();
File outputFile = File.createTempFile(fileName, ".html", outputDir);
FileOutputStream out = new FileOutputStream(outputFile);
byte[] data = html.getBytes();
out.write(data);
out.close();
return outputFile.getPath();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
所以基本上,将 HTML 保存为临时文件,然后使用 loadUrl() 加载它解决了我的问题,这让我相信这应该是 WebView 中的一个错误!!!
英文:
OK, I found a work around to my problem using the following code:
String urlDoc = saveHtmlFile("report",document);
wv.loadUrl("file://" + urlDoc);
where the function saveHtmlFile() is as follows:
private @Nullable String saveHtmlFile(String fileName, String html) {
try {
File outputDir = this.activity.getCacheDir();
File outputFile = File.createTempFile(fileName, ".html", outputDir);
FileOutputStream out = new FileOutputStream(outputFile);
byte[] data = html.getBytes();
out.write(data);
out.close();
return outputFile.getPath();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
So basically, storing the html as temp file and loading it using the loadUrl() fixed my problem which makes me believe this should be a bug in WebView!!!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论