HTML转PDF时,当HTML内容较大时会生成空白页面。

huangapple go评论119阅读模式
英文:

HTML to PDF produces blank page when HTML gets large

问题

我正在尝试使用模板从我的数据生成报告。使用 WebView 生成的 PDF 在我的数据超过一定数量时(在一个 for 循环中多出 1 个),工作得非常好,但在这种情况下只会生成一个空白页面。我已经检查了所有可能的答案,包括 这个这个这个这个,但都没有成功。

以下是我代码的一部分片段:

  1. Bitmap bitmap = callback.getMapImage();
  2. WebView wv = new WebView(activity);
  3. wv.setWebViewClient(new WebViewClient() {
  4. @Override
  5. public void onPageFinished(WebView view, String url) {
  6. super.onPageFinished(view, url);
  7. // 打印
  8. PrintManager printManager = (PrintManager) activity
  9. .getSystemService(Context.PRINT_SERVICE);
  10. String jobName = activity.getString(R.string.app_name) + " Document";
  11. PrintDocumentAdapter printAdapter = wv.createPrintDocumentAdapter(jobName);
  12. printManager.print(jobName, printAdapter, new PrintAttributes.Builder().build());
  13. reference = null;
  14. }
  15. });
  16. PrepareDocumentForPrintTask printTask = new PrepareDocumentForPrintTask(wv, bitmap);
  17. printTask.execute();

而且 HTML 是在异步任务中创建的,并且如下加载:

  1. @Override
  2. protected void onPostExecute(Triple<Boolean, String, File> result) {
  3. activity.runOnUiThread(() -> {
  4. if (result.getFirst()) {
  5. wv.loadDataWithBaseURL("", result.getSecond(), "text/html", "UTF-8", null);
  6. reference = wv;
  7. } else {
  8. Toast.makeText(activity, result.getSecond(), Toast.LENGTH_LONG).show();
  9. }
  10. });
  11. }

我已经添加了 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:

  1. Bitmap bitmap = callback.getMapImage();
  2. WebView wv = new WebView(activity);
  3. wv.setWebViewClient(new WebViewClient() {
  4. @Override
  5. public void onPageFinished(WebView view, String url) {
  6. super.onPageFinished(view, url);
  7. //print
  8. PrintManager printManager = (PrintManager) activity
  9. .getSystemService(Context.PRINT_SERVICE);
  10. String jobName = activity.getString(R.string.app_name) + &quot; Document&quot;;
  11. PrintDocumentAdapter printAdapter = wv.createPrintDocumentAdapter(jobName);
  12. printManager.print(jobName, printAdapter, new PrintAttributes.Builder().build());
  13. reference = null;
  14. }
  15. });
  16. PrepareDocumentForPrintTask printTask = new PrepareDocumentForPrintTask(wv, bitmap);
  17. printTask.execute();

And the html is created in an asyncTask and loaded as follows:

  1. @Override
  2. protected void onPostExecute(Triple&lt;Boolean, String, File&gt; result) {
  3. activity.runOnUiThread(() -&gt; {
  4. if (result.getFirst()){
  5. wv.loadDataWithBaseURL(&quot;&quot;, result.getSecond(), &quot;text/html&quot;, &quot;UTF-8&quot;, null);
  6. reference = wv;
  7. } else
  8. Toast.makeText(activity, result.getSecond(), Toast.LENGTH_LONG).show();
  9. });
  10. }

I have added largeHeap= "true" and deactivated hardware acceleration but still no success.

Any idea how to fix the problem?

答案1

得分: 0

好的,这是翻译后的内容:

好的,我找到了一个解决方案来解决我的问题,使用以下代码:

  1. String urlDoc = saveHtmlFile("report", document);
  2. wv.loadUrl("file://" + urlDoc);

其中,saveHtmlFile() 函数如下所示:

  1. private @Nullable String saveHtmlFile(String fileName, String html) {
  2. try {
  3. File outputDir = this.activity.getCacheDir();
  4. File outputFile = File.createTempFile(fileName, ".html", outputDir);
  5. FileOutputStream out = new FileOutputStream(outputFile);
  6. byte[] data = html.getBytes();
  7. out.write(data);
  8. out.close();
  9. return outputFile.getPath();
  10. } catch (FileNotFoundException e) {
  11. e.printStackTrace();
  12. } catch (IOException e) {
  13. e.printStackTrace();
  14. }
  15. return null;
  16. }

所以基本上,将 HTML 保存为临时文件,然后使用 loadUrl() 加载它解决了我的问题,这让我相信这应该是 WebView 中的一个错误!!!

英文:

OK, I found a work around to my problem using the following code:

  1. String urlDoc = saveHtmlFile(&quot;report&quot;,document);
  2. wv.loadUrl(&quot;file://&quot; + urlDoc);

where the function saveHtmlFile() is as follows:

  1. private @Nullable String saveHtmlFile(String fileName, String html) {
  2. try {
  3. File outputDir = this.activity.getCacheDir();
  4. File outputFile = File.createTempFile(fileName, &quot;.html&quot;, outputDir);
  5. FileOutputStream out = new FileOutputStream(outputFile);
  6. byte[] data = html.getBytes();
  7. out.write(data);
  8. out.close();
  9. return outputFile.getPath();
  10. } catch (FileNotFoundException e) {
  11. e.printStackTrace();
  12. } catch (IOException e) {
  13. e.printStackTrace();
  14. }
  15. return null;
  16. }

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!!!

huangapple
  • 本文由 发表于 2020年9月18日 00:15:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/63942157.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定