Cómo convertir Android View a PDF

He creado una aplicación de factura Android. La factura generada es un diseño estándar de Android con vistas anidadas. Estoy buscando una biblioteca que pueda utilizar para convertir esta vista a un documento pdf.

Estoy sorprendido de que no hay una opción directa que viene en mi búsqueda o tal vez he hecho lo primero que pasó. O tal vez lo que estoy buscando no es posible.

¿Podría alguien ayudarme a señalar una herramienta que me ayudará a convertir o generar un PDF desde una vista de Android? Estoy abierto a la opción pagada libre y modesta. O déjeme saber si lo que estoy buscando no es posible.

Tome una pantalla en su dispositivo:

Bitmap screen; View v1 = MyView.getRootView(); v1.setDrawingCacheEnabled(true); screen= Bitmap.createBitmap(v1.getDrawingCache()); v1.setDrawingCacheEnabled(false); 

Si tiene ScrollView como vista raíz, haga lo siguiente:

 LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE); RelativeLayout root = (RelativeLayout) inflater.inflate(R.layout.activity_main, null); //RelativeLayout is root view of my UI(xml) file. root.setDrawingCacheEnabled(true); Bitmap screen= getBitmapFromView(this.getWindow().findViewById(R.id.relativelayout)); // here give id of our root layout (here its my RelativeLayout's id) 

Aquí está el método getBitmapFromView() :

 public static Bitmap getBitmapFromView(View view) { //Define a bitmap with the same size as the view Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),Bitmap.Config.ARGB_8888); //Bind a canvas to it Canvas canvas = new Canvas(returnedBitmap); //Get the view's background Drawable bgDrawable =view.getBackground(); if (bgDrawable!=null) //has background drawable, then draw it on the canvas bgDrawable.draw(canvas); else //does not have background drawable, then draw white background on the canvas canvas.drawColor(Color.WHITE); // draw the view on the canvas view.draw(canvas); //return the bitmap return returnedBitmap; } 

Se mostrará toda la pantalla incluyendo el contenido oculto en su ScrollView . Ahora que tenemos nuestra pantalla bitmap vamos a guardarlo en pdf (tienes que descargar el archivo itextpdf-5.3.2.jar y adjuntarlo en tu proyecto ..)

 private static String FILE = "mnt/sdcard/invoice.pdf"; // add permission in your manifest... try { Document document = new Document(); PdfWriter.getInstance(document, new FileOutputStream(FILE)); document.open(); ByteArrayOutputStream stream = new ByteArrayOutputStream(); screen.compress(Bitmap.CompressFormat.PNG, 100, stream); byte[] byteArray = stream.toByteArray(); addImage(document,byteArray); document.close(); } catch (Exception e) { e.printStackTrace(); } private static void addImage(Document document,byte[] byteArray) { try { image = Image.getInstance(byteArray); } catch (BadElementException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } // image.scaleAbsolute(150f, 150f); try { document.add(image); } catch (DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } } 

No he probado nada. Aquí están todas las fuentes que usé: source1 , source2 , source3 .

FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.