在Android上从Webview创建PDF [英] Create a PDF from Webview on Android

查看:374
本文介绍了在Android上从Webview创建PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在尝试从Webview创建PDF.现在,我可以从Web视图中创建图像,但是在将文档拆分为多个页面时遇到了一些问题.

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.

首先,我从Web视图中创建一个位图:

First, I create a Bitmap from the webview:

public static Bitmap screenShot(View view) {
        Bitmap bitmap = Bitmap.createBitmap(view.getWidth(),
                view.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        canvas.drawColor(Color.WHITE);
        view.draw(canvas);
        return bitmap;
    }

第二,创建并显示PDF:

Second, I create and I show the PDF:

public void criaPdf(){
        Bitmap bitmap = Utils.screenShot(mContratoWebview);

        Document doc = new Document();


        File dir = new File(getFilesDir(), "app_imageDir");

        if(!dir.exists()) {
            dir.mkdirs();
        }

        File file = new File(dir, "contratoPdf.pdf");

        try {
            FileOutputStream fOut = new FileOutputStream(file);

            PdfWriter.getInstance(doc, fOut);

            //open the document
            doc.open();

            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);

            byte[] byteArray = stream.toByteArray();
            Image image = Image.getInstance(byteArray);
            image.scaleToFit(PageSize.A4.getHeight(), PageSize.A4.getWidth());

            doc.newPage();
            doc.add(image);
        } catch (DocumentException de) {
            Log.e("PDFCreator", "DocumentException:" + de);
        } catch (IOException e) {
            Log.e("PDFCreator", "ioException:" + e);
        }
        finally {
            doc.close();
        }

        mPdfView.fromFile(file)
                .pages(0, 1) // all pages are displayed by default
                .enableSwipe(true)
                .load();
        mPdfView.setVisibility(View.VISIBLE);
}

这是我到目前为止得到的:

This is what I got so far:

所以我的问题是: Webview的内容太大,无法容纳在PDF中.我该如何解决?

推荐答案

WebView具有生成PDF的内置功能,该功能是通过使用PrintManager Service专门用于打印目的而提供的.但是对于您的特定用例,我建议您将WebView的PrintAdapter的最终输出(一个PDF文件)写入(存储)到本地文件,然后从那里移出.

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.

此链接将引导您完成详细信息和实现. http://www.annalytics.co.uk/android/pdf/2017/04/06/Save-PDF-From-An-Android-WebView/

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/

对于上述解决方案,您可以通过一些小的调整来实现API级别19(KitKat)的兼容性.

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.

这篇关于在Android上从Webview创建PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆