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;
}
}
Be the first to comment.