将 Android 视图转换为 A4 大小的 PDF。

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

Convert Android view to PDF of A4 size

问题

我有一个用于构建安卓简历的应用程序。我想从我的视图中生成一个大小为A4的PDF。这是我的布局外观 - 顶部有一个顶部应用栏,并且整个视图都封装在抽屉中。包含用户详细信息的主要部分封装在嵌套的滚动视图中,其中包含多个线性布局和文本视图。在下面的截图中,我用模拟数据填充了它,但实际上,我正在从Firebase实时数据库中获取数据并在UI上显示它。

(图片已省略)

我尝试理解iTextPdf的解决方案以及类似类型的多个问题,这些问题在这里已经被提出过,但我找不到确切的解决方法。请帮助我,这将非常有帮助。

另外,请不要因为问题中没有包含任何代码而关闭这个问题。这是因为我没有代码。我正在尝试从零开始解决这个问题。我已经尽量描述了我的问题。

英文:

I have an android resume building application. I want to generate a PDF of size A4 from my view. Here's how my layout looks like - At the top I have a Top App Bar, and the whole view in encapsulated in drawer. The main part which contains user's details is encapsulated in nestedScrollView, which contains multiple LinearLayout and TextView. In this screenshot below, I have populated it with mock data, but in actuality, I am fetching data from the Firebase Realtime Database and displaying it on the UI.

将 Android 视图转换为 A4 大小的 PDF。

I tried to understand iTextPdf solution and multiple question of similar type that has been asked here, but I couldn't find something solid. Please help me out, it would be of great help.

Also, please don't close this question by giving a reason that the question doesn't contain any code. It doesn't because I don't have any. I am trying to solve this problem from scratch. I have tried to describe my problem as much as I could.

答案1

得分: 1

尝试一下:

创建一个 WebView 并将您的 EditText 文本复制到其中:

webview.loadData(youredittext.getText().toString(), "text/html", "UTF-8");

然后通过以下函数将 WebView 转换为 PDF:

private void createWebPrintJob(WebView webView) {
    PrintManager printManager = (PrintManager) this
            .getSystemService(Context.PRINT_SERVICE);
    
    PrintDocumentAdapter printAdapter =
            webView.createPrintDocumentAdapter();
    
    String jobName = getString(R.string.app_name) + " 打印测试";
    
    if (printManager != null) {
        printManager.print(jobName, printAdapter,
                new PrintAttributes.Builder().build());
    }
}

之后用户可以选择页面大小,例如 A4。

英文:

try this:

create a WebView and copy the text of your edittext in it:

webview.loadData(youredittext.gettext().tostring, "text/html", "UTF-8");

and convert webview to pdf by below function:

private void createWebPrintJob(WebView webView) {

PrintManager printManager = (PrintManager) this
        .getSystemService(Context.PRINT_SERVICE);

PrintDocumentAdapter printAdapter =
        webView.createPrintDocumentAdapter();

String jobName = getString(R.string.app_name) + " Print Test";

if (printManager != null) {
    printManager.print(jobName, printAdapter,
            new PrintAttributes.Builder().build());
}
}

after that user can select page size for example A4

答案2

得分: 0

有很多库可以将布局转换为PDF,但让我们选择一个流行的,这样我们如果遇到问题就能找到答案。

我列出的这些库的工作原理是这样的:它们将您的布局截取为位图图像,然后将其转换为PDF。

- 第一种解决方案: iTextPDF https://github.com/itext/itext7 (新版本)。

查看这个详细的教程,它还介绍了如何截取滚动视图的屏幕截图 https://www.codeproject.com/Articles/989236/How-to-Convert-Android-View-to-PDF-2

以及这个StackOverflow的答案 https://stackoverflow.com/a/29731275/12802591

- 第二种解决方案: PdfMyXML库 https://github.com/HendrixString/Android-PdfMyXml,只需按照文档中的步骤进行操作。

可能还有其他解决方案,但这些是流行的解决方案。

如果对您有用,或者如果您遇到问题,请告诉我。谢谢!

英文:

There are a lot of libraries that convert layouts to PDF, but let's opt for popular one so we could find answers if we're stuck.

The libraries I listed works like so : They take screenshot of your layout as bitmap image and convert it to pdf.

- 1st solution: iTextPDF https://github.com/itext/itext7 (New Version).

check this detailed tutorial which treates also the case of taking screenshot of a scrollview https://www.codeproject.com/Articles/989236/How-to-Convert-Android-View-to-PDF-2

and this stackoverflow answer https://stackoverflow.com/a/29731275/12802591

- 2nd solution: PdfMyXML library https://github.com/HendrixString/Android-PdfMyXml just follow the steps in the documentation.

They may be other solutions, but these are the popular ones.

Let Me know if it works for you and also if you're stuck. Thank you!

huangapple
  • 本文由 发表于 2020年5月2日 12:45:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/61554589.html
匿名

发表评论

匿名网友

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

确定