如何从Android中的webview创建PDF?

前端之家收集整理的这篇文章主要介绍了如何从Android中的webview创建PDF?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在webview中显示网页.现在如何从webview创建PDF?

例如:webview加载URL是“www.google.co.in”.现在如何将此网页保存为图像并创建pdf?

任何帮助将不胜感激

解决方法

尝试这样

WebView有内置的方法称为setPictureListener使用该方法如下

webView1.setPictureListener(new PictureListener() {

            public void onNewPicture(WebView view,Picture picture) {
                if (picture != null) {
                    try {
                        bmp = pictureDrawable2Bitmap(new PictureDrawable(
                                picture));

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        });

为了获取位图,我已经使用了pictureDrawable2Bitmap,这里就是一个

private static Bitmap pictureDrawable2Bitmap(PictureDrawable pictureDrawable) {
        Bitmap bitmap = Bitmap.createBitmap(
                pictureDrawable.getIntrinsicWidth(),pictureDrawable.getIntrinsicHeight(),Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        canvas.drawPicture(pictureDrawable.getPicture());
        return bitmap;
    }

现在你的位图已经准备就绪,现在设置webview客户端如下

webView1.setWebViewClient(new myWebClient());

这里是myWebClient

public class myWebClient extends WebViewClient {
        @Override
        public void onPageStarted(WebView view,String url,Bitmap favicon) {
            // TODO Auto-generated method stub
            super.onPageStarted(view,url,favicon);
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view,String url) {
            // TODO Auto-generated method stub

            view.loadUrl(url);
            return true;

        }

        @Override
        public void onPageFinished(WebView view,String url) {
            // TODO Auto-generated method stub
            super.onPageFinished(view,url);
            Log.i("OnPageLoadFinished",url);
            img.setImageBitmap(bmp);
        }

页面加载完成后,我已经设置了图像位图,这是当前加载的网址在您的Webview

现在Bitmap可以通过使用IText Library将该位图传递给Pdf

下面是一个使用iText Use Below功能编写pdf的例子

public void SimplePDFTable() throws Exception {

    File direct = new File(Environment.getExternalStorageDirectory()
            .getAbsolutePath() + "/AamirPDF");
    if (!direct.exists()) {
        if (direct.mkdir()) {
            Toast.makeText(MainActivity.this,"Folder Is created in sd card",Toast.LENGTH_SHORT)
                    .show();
        }
    }
    String test = Environment.getExternalStorageDirectory()
            .getAbsolutePath() + "/AamirPDF";
    Document document = new Document();

    PdfWriter.getInstance(document,new FileOutputStream(test
            + "/mypdf.pdf"));

    document.open();
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.PNG,100,stream);
    byte[] byteArray = stream.toByteArray();
    Image image = Image.getInstance(byteArray);


    image.scaleToFit(PageSize.A4.getHeight(),PageSize.A4.getWidth());
    document.add(image);

    document.close();

}

祝你好运

猜你在找的Android相关文章