How we create .pdf file from our application?
- First of all, we get WriteStorage Permission to create a file.
1 |
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> |
- Add itextpdf dependency on the build.gradle file
1compile 'com.itextpdf:itextg:5.5.10'
- Create directory and file name
12345678910111213class PdfCreator{public PdfCreator(JSONArray data, Context context) {this.data = data;this.context = context;if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {pdfFolder = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS), "UserPdf");} else{pdfFolder = new File(Environment.getExternalStorageDirectory(), "UserPdf");}myFile = new File(pdfFolder.getAbsolutePath() + File.separator + "Cart_Detail.pdf");}// Write our functions here.....}
- Write paragraph and Image in our created file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
public boolean createPdf() throws IOException, DocumentException { if (!pdfFolder.exists()) { success = pdfFolder.mkdirs(); Log.d("pdf Created-->", String.valueOf(pdfFolder.mkdir())); } else{ Log.d("TAG", "createPdf: alredy exists"); } if (success) { Font First_Font = new Font(Font.FontFamily.TIMES_ROMAN, 18, Font.BOLD); myFile.createNewFile(); Font SecondFont = new Font(Font.FontFamily.TIMES_ROMAN, 16,Font.UNDERLINE); OutputStream output = new FileOutputStream(myFile); Rectangle rectangle = new Rectangle(216f, 720f); rectangle.setBorder(Rectangle.BOX); rectangle.setBorderWidth(4); Document document = new Document(PageSize.LETTER); PdfWriter.getInstance(document, output); document.open(); Paragraph p1=new Paragraph("OpenCart-Mobikul", First_Font); p1.setAlignment(Element.ALIGN_CENTER); document.add(p1); document.add(new Paragraph("Cart Items Detail", SecondFont)); String product_name = null; String product_price = null; String sub_total = null; String quantity = null; for (int i = 0; i < data.length(); i++) { try { JSONObject json_object = data.getJSONObject(i); String imageUrl = json_object.getString("thumb"); Image image = Image.getInstance(new URL(imageUrl)); Float width = image.getWidth(); image.scaleAbsolute(width/4,width/4); document.add(Chunk.NEWLINE); document.add(image); product_name = json_object.getString("name"); product_price = json_object.getString("price"); quantity = json_object.getString("quantity"); sub_total = json_object.getString("total"); document.add(new Paragraph("Product Name: " + product_name)); document.add(new Paragraph("Price: " + product_price)); document.add(new Paragraph("Quantity: " + quantity)); document.add(new Paragraph("Sub-Total: " + sub_total)); } catch (JSONException e) { e.printStackTrace(); } } document.close(); output.close(); return true; } else { Toast.makeText(context, "Pdf Not Created", Toast.LENGTH_SHORT).show(); return false; } } |
Reference: StackOverflow, itextpdf Developer Site, and android Developer Site.