简体   繁体   English

如何从webView创建PDF(已隐藏)?

[英]How to create PDF from webView, which is hidden?

In my android application, I've implemented generating pdf from html content, by displaying the html content in a WebView and then creating pdf document using PdfDocument . 在我的Android应用程序中,我已经实现了从html内容生成pdf的方法,即在WebView中显示html内容,然后使用PdfDocument创建pdf文档。 It works perfectly fine (code samples below), but I would like to know if it is possible to genereate pdf file from html content which is not visible on the screen? 它工作得很好(下面的代码示例),但是我想知道是否可以从屏幕上不可见的html内容生成pdf文件? When I set visibility of mPdfPreviewWebView to GONE , then the created pdf is empty. 当我将mPdfPreviewWebView可见性设置为GONE ,创建的pdf为空。

In other words, I need to generate pdf from html, but I don't want html to be visible (or take any space) on the screen. 换句话说,我需要从html生成pdf,但是我不希望html在屏幕上可见(或占用任何空间)。

Remark: I don't want to use iText library because of its license. 备注:由于其许可证,我不想使用iText库。

private void createPDF() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        PdfDocument pdfDocument = new PdfDocument();

        PdfDocument.PageInfo pageInfo = new PdfDocument
                .PageInfo.Builder(A4_WIDTH, A4_HEIGHT, 1).create();

        PdfDocument.Page page = pdfDocument.startPage(pageInfo);

        Canvas canvas = page.getCanvas();
        int webViewWidth = mPdfPreviewWebView.getWidth();
        float scale = A4_WIDTH / (float) webViewWidth;
        canvas.save();
        canvas.scale(scale, scale);

        mPdfPreviewWebView.draw(canvas);
        canvas.restore();
        pdfDocument.finishPage(page);

        String dir = Environment.getExternalStorageDirectory()
                + getString(R.string.three_clicks_folder);

        try {
            FileOutputStream fileOutputStream = null;
            try {
                File file = (new File(dir, "share.pdf"));
                if (!file.exists()) {
                    file.createNewFile();
                }

                fileOutputStream = new FileOutputStream(file);

                pdfDocument.writeTo(fileOutputStream);

                pdfDocument.close();
                Log.i(TAG, "PDF created");
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } finally {
                if (fileOutputStream != null) {
                    fileOutputStream.close();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

It is possible. 有可能的。 Simply make a fragment on top of your activity and call the code in your activity that handles your pdf routine from the fragment. 只需在活动之上创建一个片段,然后在活动中调用该片段中处理pdf例程的代码。 Once the conversion is finished, dismiss the fragment. 转换完成后,关闭片段。 The fragment can be a "processing...." message, and when done, dismiss it, then do whatever you like to do in your fragment. 该片段可以是“正在处理...”消息,完成后将其关闭,然后执行您想在片段中执行的任何操作。 Or may be you want to do something with your converted pdf before you dismiss the fragment. 或者,您可能想在关闭片段之前对转换后的pdf进行处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM