How to create PDF from webview in android?

前端 未结 2 1413
春和景丽
春和景丽 2020-12-07 23:21

I am displaying webpage in webview. Now how to create PDF from webview ?

For Example : webview loads URL is \"www.g

相关标签:
2条回答
  • 2020-12-08 00:06

    Try like this

    WebView have inbuilt method called setPictureListener Use that method as below

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

    For obtaining bitmap i have used pictureDrawable2Bitmap and here is that one

    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;
        }
    

    Now Your Bitmap is ready,Now set webview client as below

    webView1.setWebViewClient(new myWebClient());
    

    And here is 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);
            }
    

    As shown on page load finished i have set image bitmap which is snap of current loaded url on your webview

    Now Bitmap is ready pass that bitmap to Pdf using IText Library

    Here is an example of writing pdf with image on iText Use Below function for that

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

    Good Luck

    0 讨论(0)
  • 2020-12-08 00:19

    As of API 19 (KitKat), Android now lets you print a webview directly. Moreover, the same PrintManager API can be used to generate a PDF from a WebView without any external libraries.

    As the example code shows, just get the PrintManager, create a PrintDocumentAdapter from the WebView in question, then create a new PrintJob and your user should see the option to save it to a file as PDF or print to a cloud printer. On newer Androids than 4.4 they'll also get a visual preview of what will be printed/PDF'd.

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