Creating a PDF file from Webview on Android

So, I'm trying to create a PDF from Webview. Right now I can create an image from a web view, but I am having problems decomposing a document on many pages.

First I create a bitmap from 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; } 

Secondly, I create and display 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 web view is too large to fit in the PDF. How can i solve this?

+3
source share
2 answers

WebView has built-in functions for creating PDF files, which are available through the PrintManager Service specifically for printing. But for your specific use, I suggest you write (save) the final output of the WebView PrintAdapter, which is a PDF file, to a local file and go from there.

This link will guide 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 compatibility level 19 API (KitKat) with a little customization for the above solution.

This should solve your problem, but if you have any problems with the implementation, let me know.

+4
source

to create a pdf file from a web browser 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 a problem creating an image from webview :))))

+3
source

Source: https://habr.com/ru/post/981563/


All Articles