我需要使用数据库中的数据生成 pdf 文档。我看过一堆例子,但我无法将这些数据从数据库中获取为 pdf。
我有两个模型 Receipt 和 Logs(Receipt has_many Logs)。我想将 Receipt 中的数据获取到 pdf 中,然后将所有属于 Receipt 的日志获取到该 pdf 中。之后,我需要在 android 设备上打印该 pdf 文档。
这是我的收据模型。
@Table(name = "Receipt")
public class Receipt extends Model {
@Column(name="Place")
String place;
@Column(name="ShippingNumber")
String shippingNumber;
@Column(name="Warehouse")
String warehouse;
@Column(name="Carrier")
String carrier;
@Column(name="LicencePlate")
String licencePlate;
@Column(name = "Driver")
String driver;
@Column(name = "Customer")
String customer;
@Column(name= "DestWarehouse")
String destWarehouse;
@Column(name = "Employee")
String employee;
@Column(name = "PriceType")
String priceType;
@Column(name = "PriceCorrection")
Integer priceCorrection;
@Column(name = "CreatedAt")
Date createdAt;
public List<Logs> getLogs(){
return getMany(Logs.class,"Receipt");
}
这是我的日志模型。
@Table(name = "Logs")
public class Logs extends Model {
@Column(name="PlateNumber")
String plate_number;
@Column(name="SortID")
String sort_id;
@Column(name="Grade")
String grade;
@Column(name = "Diametar")
double diameter;
@Column(name="Length")
double length;
@Column(name="CreatedAt")
Date createdAt;
@Column(name="Receipt")
Receipt receipt;
}
我还将粘贴代码,以便您了解我如何显示收据和日志。
这就是我显示日志的方式。
public LogsArrayAdapter(List<Logs> logsList) {
inflater = LayoutInflater.from(DisplayLogs.this);
this.logsList = logsList;
}
@Override
public int getCount() {
return logsList.size();
}
@Override
public Object getItem(int position) {
return logsList.get(position);
}
@Override
public long getItemId(int position) {
return logsList.get(position).getId();
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.activity_display_logs, parent, false);
}
Logs log = logsList.get(position);
((TextView) convertView.findViewById(R.id.textNumber)).setText(log.plate_number);
((TextView) convertView.findViewById(R.id.textSort)).setText(log.sort_id);
((TextView) convertView.findViewById(R.id.textGrade)).setText(log.grade);
((TextView) convertView.findViewById(R.id.textDiameter)).setText(log.diameter + "cm");
((TextView) convertView.findViewById(R.id.textLength)).setText(log.length + "cm");
Log.d("Value", log.createdAt.toString());
((TextView) convertView.findViewById(R.id.textAmount)).setText(String.format("%.2f m3", log.getM3()));
return convertView;
}
这就是我显示收据的方式。
public ReceiptAdapter(List<Receipt> receiptList) {
inflater = LayoutInflater.from(DisplayRecepit.this);
this.receiptList = receiptList;
}
@Override
public int getCount() {
return receiptList.size();
}
@Override
public Object getItem(int position) {
return receiptList.get(position);
}
@Override
public long getItemId(int position) {
return receiptList.get(position).getId();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.activity_display_recepit, parent, false);
}
Receipt receipt = receiptList.get(position);
((TextView) convertView.findViewById(R.id.rPlace)).setText(receipt.place);
((TextView) convertView.findViewById(R.id.rShipNumb)).setText(receipt.shippingNumber);
((TextView) convertView.findViewById(R.id.rEmployee)).setText(receipt.employee);
((TextView) convertView.findViewById(R.id.rWarehouse)).setText(receipt.warehouse);
((TextView) convertView.findViewById(R.id.rCarrier)).setText(receipt.carrier);
((TextView) convertView.findViewById(R.id.rLicence)).setText(receipt.licencePlate);
((TextView) convertView.findViewById(R.id.rDriver)).setText(receipt.driver);
((TextView) convertView.findViewById(R.id.rCustomer)).setText(receipt.customer);
((TextView) convertView.findViewById(R.id.rDestWareh)).setText(receipt.destWarehouse);
((TextView) convertView.findViewById(R.id.rCreatedAt)).setText(new SimpleDateFormat("dd.MM.yyyy HH:mm").format(receipt.createdAt));
return convertView;
}
任何建议和帮助都非常受欢迎。我真的很困惑,我需要帮助。
问题:使用数据库中的数据生成 pdf 文档然后打印或使用 android printDocument() 并直接使用数据库是否更好更容易?
如何将变量从数据库传递到 pdfDocument 或 printDocument?