Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create PDF from webview in android?

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

For Example : webview loads URL is "www.google.co.in". Now how to save this webpage as image and create pdf ??

any help would be appreciated

like image 879
Hardik Joshi Avatar asked Dec 11 '13 06:12

Hardik Joshi


People also ask

Can we open PDF in WebView in android?

We can load PDF in android easily using WebView. We will be using the simplest way for displaying PDF file in android. Using the Google Docs PDF viewer we could do this easily. Now lets get in to the implementation and source code.

How do I create a PDF from HTML?

On a Windows computer, open an HTML web page in Internet Explorer, Google Chrome, or Firefox. On a Mac, open an HTML web page in Firefox. Click the “Convert to PDF” button in the Adobe PDF toolbar to start the PDF conversion. Enter a file name and save your new PDF file in a desired location.

How do I open local PDF in WebView Android programmatically?

setPluginsEnabled(true); webview. getSettings(). setAllowFileAccess(true); File file = new File(Environment. getExternalStorageDirectory() + "/test.


2 Answers

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

like image 189
Aamirkhan Avatar answered Nov 03 '22 13:11

Aamirkhan


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.

like image 30
fattire Avatar answered Nov 03 '22 13:11

fattire