首页 > 解决方案 > Android Java 从 webview 生成带有单独页眉和页脚的 PDF 文件

问题描述

尝试从 webview 转换为 pdf 时,尝试在 pdf 文件的多页中包含页眉和页脚时遇到了一些问题。这是我如何形成要在 webview 中显示的内容:

 public String toHtml() {
    StringBuilder htmlStr = new StringBuilder("");
    htmlStr.append("<html><head><meta http-equiv=\"Content-Type\" content=\"text/html\"; charset=utf-8>");
    htmlStr.append("<title>").append("").append("</title>");
    htmlStr.append("</head>\n");
    htmlStr.append("<body>").append("").append("</body>");
    htmlStr.append("<footer>").append("").append("</footer>");
    htmlStr.append("</html>");
    return htmlStr.toString();
 }

我按照本指南从 webview 生成 PDF。它确实设法生成了 PDF 文件。但是,页眉只出现在 PDF 文件的第一页,页脚只出现在 PDF 文件的最后一页。任何想法如何在 PDF 文件的所有页面中包含页眉和页脚?

谢谢!

标签: javaandroidpdf

解决方案


我有同样的问题。

我的工作解决方案:

  1. 从 Webview 生成 Pdf
  2. 保存此 PDF
  3. 使用 PdfBox GitHub 重新打开 Pdf - TomRoush/PdfBox
  4. 添加页眉和页脚(使用 addCenterText)PdfBox - 如何居中...
  5. 保存扩展版本

推荐阅读