Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a PDF from Webview on Android

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.

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

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:

enter image description here

So my problem is: The content of the Webview is too big to fit in the PDF. How can I solve this?

like image 511
Leandro Borges Ferreira Avatar asked Feb 21 '17 19:02

Leandro Borges Ferreira


People also ask

Can I save web page as PDF in Android?

After going to the webpage, click the three-dot menu and choose Print. In the Print box, drop down the Destination menu and select Save as PDF. Also, drop down the Layout menu and choose Landscape. Click the Print button when ready.

Can we open PDF in WebView in android?

Opening a PDF file in Android using WebView All you need to do is just put WebView in your layout and load the desired URL by using the webView. loadUrl() function. Now, run the application on your mobile phone and the PDF will be displayed on the screen.


Video Answer


2 Answers

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.

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/

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.

like image 145
Rakesh Gopathi Avatar answered Sep 30 '22 14:09

Rakesh Gopathi


to create pdf from webview you need android>kitkat -> sdk>=19

  btnSave.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) {

                createWebPrintJob(webView);
            } else {



            }

        }
    });

//// Function:

private void createWebPrintJob(WebView webView) {

    PrintManager printManager = (PrintManager) this
            .getSystemService(Context.PRINT_SERVICE);

    PrintDocumentAdapter printAdapter =
            webView.createPrintDocumentAdapter();

    String jobName = getString(R.string.app_name) + " Print Test";

    if (printManager != null) {
        printManager.print(jobName, printAdapter,
                new PrintAttributes.Builder().build());
    }
}

I have problem in create image from webview :))))

like image 44
Mehrdad Avatar answered Sep 30 '22 15:09

Mehrdad