【问题标题】:Open several pages in webview and create PDF在 webview 中打开多个页面并创建 PDF
【发布时间】:2015-12-11 05:40:08
【问题描述】:

我正在尝试构建一个应用程序来从 webview 中的多个页面创建 PDF。

作为初学者,我很高兴我发现了这个: Android create pdf document from webview with multiple pages

但是我无法正确处理。 在 PDF 中创建的页面数量很好,但内容却不是。 创建了一个包含(在这种情况下)3 个相似页面的 PDF。所有显示该方法被调用之前的webview。 创建 PDF 后,将加载 url 数组的最后一个 url。

如何实现只有在 webview 中加载新的 url 后才创建 pdf 页面?

我确实尝试使用延迟,但这似乎不是问题。 如果延迟,则首先创建 pdf,然后才打开页面。

我确实尝试从 onPageFinished() 中调用 pdf 创建。 它也没有完成这项工作。

也许我确实用错了所有这些。 这是我的代码现在的样子:

            //Create folder
            String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
            String dirName = "exampledirectory";
            File newdir = new File(baseDir + File.separator + dirName);
            newdir.mkdirs();
            //Create PDF
            String fileName = "example.pdf";
            String fileNameWithPath = newdir + File.separator + fileName;
            //Create document
            PdfDocument document = new PdfDocument();

            String[] urlArr = {"exampleurl1.com", "exampleurl2.com", "exampleurl3.com"};

            for (int i = 0; i < urlArr.length; i++) {
                mWebView.loadUrl(urlArr[i]);
                PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(mWebView.getMeasuredWidth(), mWebView.getContentHeight(), i).create();
                // start [i]st page
                PdfDocument.Page page = document.startPage(pageInfo);
                // draw on the page
                View content = mWebView;
                content.draw(page.getCanvas());
                // finish [i]st page
                document.finishPage(page);
            }

            FileOutputStream fos;
            try {
                fos = new FileOutputStream(fileNameWithPath, false);
                // write the document content
                document.writeTo(fos);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            document.close();

【问题讨论】:

    标签: android pdf webview


    【解决方案1】:

    我找到了适合我的解决方案。 感觉有点奇怪,但确实有效。

    正如我上面提到的 onPageFinished() 在我的第一次尝试中不起作用,因为调用时页面尚未呈现。 现在我再次尝试从 onPageFinished() 调用另一个方法,然后调用 PDFbuilder 方法。通过这种“解决方法”,我能够从所有 URL 构建 PDF。 代码如下:

    private class printPDFFromWebcontent {
    
        int i = 0;
        public String pageURL = "exampleURL";
        public PdfDocument document = new PdfDocument();
        public String fileNameWithPath ="blank.pdf";
    
        private void runPDFBuilder() {
        zWebView = (WebView) findViewById(R.id.activity_main_webview);
        WebSettings webSettings = zWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        zWebView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(zWebView, url);
                i++;
                openNextPage();
            }
            private void openNextPage() {
                String nextPage = pageURL + "?page=" + String.valueOf(i + 1); //This works with my specific target URLs. You may want to use an URL array.
                if (i == 1) {
                    zWebView.loadUrl(nextPage);
                    createPDFNow();
                }
                if (i == 2) {
                    zWebView.loadUrl(nextPage);
                    addPageToPDF();
                }
                if (i == 3) {
                    zWebView.loadUrl(nextPage);
                    addPageToPDF();
                }
                if (i == 4) {
                    mWebView.loadUrl("whateverYouWantToShowIfFinishedURL"); 
                    //Not added to the PDF
                    //Reason is to trigger onPageFinished() once more. You could probably i++ here and call openNextPage() from within.
                    addPageToPDF(); //Add the last webview content to PDF.
                }
                if (i == 5) {
                    createPDFNow();
                    Toast.makeText(getApplicationContext(), "Finito.", Toast.LENGTH_SHORT).show();
                    savePDF();
                    return;
                }
            }
            public void createPDFNow(){
                String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
                String dirName = "example";
                File newdir = new File(baseDir + File.separator + dirName);
                newdir.mkdirs();
                String fileName = "example.pdf";
                fileNameWithPath = newdir + File.separator + fileName;
            }
            public void addPageToPDF() {
                PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(zWebView.getMeasuredWidth(), zWebView.getContentHeight(), i).create();
                PdfDocument.Page page = document.startPage(pageInfo);
                View content = zWebView;
                content.draw(page.getCanvas());
                document.finishPage(page);
            }
            public void savePDF(){
                FileOutputStream fos;
                try {
                    fos = new FileOutputStream(fileNameWithPath, false);
                    document.writeTo(fos);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                document.close();
            }
        });
        zWebView.getSettings().setUseWideViewPort(true);
        zWebView.getSettings().setLoadWithOverviewMode(true);
        zWebView.loadUrl("URL1");
        return;
    }
    

    但是,如果有更聪明的解决方案:我想学习。

    【讨论】:

      猜你喜欢
      • 2015-03-25
      • 2012-09-06
      • 1970-01-01
      • 1970-01-01
      • 2020-02-17
      • 1970-01-01
      • 1970-01-01
      • 2014-08-29
      • 1970-01-01
      相关资源
      最近更新 更多