我正在使用reporlab在我的django项目中生成pdf。我有多个数据,我想使用reportlab以表格格式存储。我创建了一个表。但该表仅显示表格行中没有进一步数据更新的第一个数据。
表格如下:
Inspection_Id | Licence_Plate |图片|评论
100 |测试| http://www.test.com/image.jpg | QWERTY
代码段位于:
from django.http import HttpResponse
from rest_framework import generics
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import A4, cm
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.platypus import Paragraph, Table, TableStyle
from reportlab.lib.enums import TA_JUSTIFY, TA_LEFT, TA_CENTER
from reportlab.lib import colors
class DamageExportViewSet(generics.ListAPIView):
renderer_classes = [PDFRenderer,]
def get(self, request):
response = HttpResponse(content_type='application/pdf')
response['Content-Disposition'] = 'attachment; filename="Damage Report File.pdf"'
width, height = A4
styles = getSampleStyleSheet()
styleN = styles["BodyText"]
styleN.alignment = TA_LEFT
styleBH = styles["Normal"]
styleBH.alignment = TA_CENTER
def coord(x, y, unit=1):
x, y = x * unit, height - y * unit
return x, y
inspection = Paragraph('''<b>Inspection Id</b>''', styleBH)
licplt = Paragraph('''<b>Licence Plate</b>''', styleBH)
imgs = Paragraph('''<b>Images</b>''', styleBH)
cmnts = Paragraph('''<b>Comments</b>''', styleBH)
buffer = BytesIO()
p = canvas.Canvas(buffer, pagesize=A4)
p.drawString(20, 800, "Report generated at " + timezone.now().strftime('%b %d, %Y %H:%M:%S'))
damage_data = Damage.objects.all()
try:
for i in damage_data:
inspection_data = str(i.inspection_id).encode('utf-8')
licence_plate = str(i.inspection.vehicle.licence_plate).encode('utf-8')
images = str(i.image).encode('utf-8')
comments = str(i.comment).encode('utf-8')
inspcdata = Paragraph(inspection_data, styleN)
lncplt = Paragraph(licence_plate, styleN)
img = Paragraph(images, styleN)
cmt = Paragraph(comments, styleN)
data = [[inspection, licplt, imgs, cmnts],
[inspcdata, lncplt, img, cmt]]
except:
pass
table = Table(data, colWidths=[4 * cm, 4 * cm, 5 * cm, 4 * cm])
table.setStyle(TableStyle([
('INNERGRID', (0, 0), (-1, -1), 0.25, colors.black),
('BOX', (0, 0), (-1, -1), 0.25, colors.black),
]))
table.wrapOn(p, width, height)
table.wrapOn(p, width, height)
table.drawOn(p, *coord(1.8, 9.6, cm))
p.showPage()
p.save()
pdf = buffer.getvalue()
buffer.close()
response.write(pdf)
return response
我不明白如何在表格中显示 Damage 模型中的所有数据。图像数据应该是超链接。
提前致谢:)
答案 0 :(得分:1)
您在每个循环步骤覆盖BitmapFactory.decodeStream(new FileInputStream(file[i]));
private OpenDownloadedImages MyContext;
ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>();
public buttonAdapter(OpenDownloadedImages c){
MyContext = c;
}
@Override
public int getCount() {
return file.length;
}
@Override
public Object getItem(int i) {
return file[i];
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
LayoutInflater inflater = getLayoutInflater();
View vie = inflater.inflate(R.layout.row_list_fav,null);
ImageView imageView = (ImageView) vie.findViewById(R.id.imgFav);
imageView.setLayoutParams(new GridView.LayoutParams(180,250));
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setPadding(0,0,0,0);
try {
File f=new File("sdcard/wallpaperHD");
File file[] = f.listFiles();
Bitmap b = BitmapFactory.decodeStream(new FileInputStream(file[i]));
//ImageView img=(ImageView)findViewById(R.id.dImage);
//img.setImageBitmap(b);
imageView.setImageBitmap(b);
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
return imageView;
}
File f=new File("sdcard/wallpaperHD");
File file[] = f.listFiles();
}
数组。
你需要它是这样的:
data
这样你就可以得到像
这样的东西data=[
["Inspection_Id", "Licence_Plate", "Images", "Comment"],
["100", "TEST", "http://url.to/png", "Qwerty"],
["200", "2nd data row", "http://url.to/gif", "Dvorak"]
]
因此,您的循环需要如下所示:
| Inspection_Id | Licence_Plate | Images | Comment |
| ------------- |-------------- | ----------------- | ------- |
| 100 | TEST | http://url.to/png | Qwerty |
| 200 | 2nd data row | http://url.to/gif | Dvorak |
那应该是它:)
图像数据应该是超链接。
这是什么意思?
干杯!
答案 1 :(得分:0)
如果您的主要目标是迭代并在表格中显示damage_data记录,请尝试以下操作:
(...)
damage_data = Damage.objects.all()
data = []
data.append(["Inspection_Id", "License_Plate", "Images", "Comment"])
try:
for i in damage_data:
row = []
inspection_data = str(i.inspection_id).encode('utf-8')
licence_plate = str(i.inspection.vehicle.licence_plate).encode('utf-8')
images = str(i.image).encode('utf-8')
comments = str(i.comment).encode('utf-8')
row.append(inspection_data)
row.append(license_plate)
row.append(images)
row.append(comments)
data.append(row)
(...)
这应该遍历你的damage_data查询集并填充你的表。