Create a PDF from Webview on Android

后端 未结 2 1870
失恋的感觉
失恋的感觉 2020-12-03 13:02

So I am trying to create a PDF from a Webview. Right now I can create an image from the webview, but I am having some problems to split my document in many pages.

Fi

相关标签:
2条回答
  • 2020-12-03 13:10

    to create pdf from webview you need android>kitkat -> sdk>=19

      btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
    
                if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) {
    
                    createWebPrintJob(webView);
                } else {
    
    
    
                }
    
            }
        });
    

    //// 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());
        }
    }
    

    I have problem in create image from webview :))))

    0 讨论(0)
  • 2020-12-03 13:30

    WebView has built-in functionality to generate PDF's which is made available by using PrintManager Service specifically for the purpose of printing. But for your specific usecase I would suggest you to write(store) the final output of WebView's PrintAdapter which is a PDF file to a local file and go from there.

    This link will walk you through the details and implementation. http://www.annalytics.co.uk/android/pdf/2017/04/06/Save-PDF-From-An-Android-WebView/

    You can achieve API level 19(KitKat) compatibility with small tweaks for the above solution.

    This should solve your problem but incase you face any issue with the implementation let me know.

    0 讨论(0)
提交回复
热议问题