Today, since this stand that it is our blog, we are going to tell you how generate pdf reports with dffierent views that our Android app has.
There are a huge number of libraries that we can include in our project for generating pdf files. Some of them are:
iText 5.0+ Licencia AGPL iText 4.2 Licencia MPL/LGPL PDF Box Apache License, Version 2.0 JPedal Licencia LGPL FOP Licencia Apache, Version 2.0 gnujpdf Licencia LGPL PJX Licencia GPLv2 jPod Licencia BSD Android PDF Writer Licencia BSD PdfDocument (Propia de Android. Requiere un nivel de API 19 o superior)
An important aspect that we must have into account before choosing one of them is the license and overall, if they are compatible with Android without using swing library for instance.
In the following example, we use the Android PDF Writer (APW) library, that has BSD license.
First of all, we must generate the corresponding bitmap for each view that we want to include on the report.
LinearLayout linLayoutView = new LinearLayout(this); View child1 = LayoutInflater.from(this).inflate(R.layout.activity_main, null); linLayoutView.addView(child1); // Create a bitmap from a passed view linLayoutView.setDrawingCacheEnabled(true); linLayoutView.buildDrawingCache(true); linLayoutView.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_AUTO); // Quality of the snapshot linLayoutView.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); linLayoutView.layout(0, 0, linLayoutView.getMeasuredWidth(),linLayoutView.getMeasuredHeight()); Bitmap bitmap1 = Bitmap.createBitmap(loadBitmapFromView(linLayoutView)); linLayoutView.setDrawingCacheEnabled(false); // clear drawing cache /** * Load the bitmap referred to the passed view * * @param v: view * @return Bitmap */ public static Bitmap loadBitmapFromView(View v) { Bitmap b = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888); Canvas c = new Canvas(b); v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredWidth()); v.draw(c); return b; }
Afterwards, we define a PDFWriter object in which we define the document characteristics to generate the PDF reports:
PDFWriter mPDFWriter = new PDFWriter(PaperSize.A4_WIDTH, PaperSize.A4_HEIGHT);
If we need compress and scale the bitmaps, we can do that as follows:
// Compress and scale the bitmap to the paper size ByteArrayOutputStream out = new ByteArrayOutputStream(); bitmap1.compress(Bitmap.CompressFormat.PNG, 50, out); Bitmap decodedBitmap = Bitmap.createScaledBitmap(bitmap1, PaperSize.A4_WIDTH, PaperSize.A4_HEIGHT, true); mPDFWriter.addImage(10, 10, decodedBitmap);
If we need create more than one page in our pdf file, we can do that using the newPage() method of APW library and in each page we will include the differents views.
mPDFWriter.newPage(); mPDFWriter.addImage(10, 10, bitmap2);
Finally, we only have to create the file and store it into device’s storage.
File newFile = new File(this.getReportPDFFile()); newFile.createNewFile(); FileOutputStream pdfFile = new FileOutputStream(newFile); pdfFile.write(mPDFWriter.asString().getBytes("ISO-8859-1")); pdfFile.close();
As result, we have the PDF file composed by the different views included.
Related articles
Use Android Priority Job Queue library for your background tasks
Thank you sir so much!