controlling table output: image pixel-dimensions vs cells defined in mm #990
-
I want to generate a table with controlled dimensions in mm (row height & column width). So these images will have different dimensions in pixels, and I want to show them in my table at controlled sizes instead of filling in the whole cell. Two reasons for this:
This is my sample code and my output table-image-test.pdf. from fpdf import FPDF
pdf = FPDF()
pdf.add_page()
pdf.set_font("Times", size=12)
url0 = 'https://upload.wikimedia.org/wikipedia/commons/3/32/Archive-ugent-be-500C7CB6-DFDB-11E5-9D50-9943D43445F2_DS-266_%28cropped%29.jpg'
url1 = 'https://api.gbif.org/v1/image/cache/fit-in/500x/occurrence/4022958319/media/e41e2174baa3c3898e9ce1fb4bc3b561'
url2 = 'https://api.gbif.org/v1/image/cache/fit-in/50x/occurrence/4022958319/media/e41e2174baa3c3898e9ce1fb4bc3b561'
url3 = 'https://api.gbif.org/v1/image/cache/fit-in/10x/occurrence/4022958319/media/e41e2174baa3c3898e9ce1fb4bc3b561'
TABLE_DATA = (
('title','image','description','width'),
('A big image',url0,'Calamintha spp.','1750 px'),
('A medium image',url1,'Encelia californica','500 px'),
('A small image',url2,'Encelia californica','50 px'),
('A dwarf image',url3,'Encelia californica','10 px'),
)
print(TABLE_DATA)
pdf.cell(text="A 130 mm wide table with columns of 35, 30, 45 and 20 mm wide", new_y = 'NEXT', align='C', border=0)
pdf.ln(); pdf.ln();
with pdf.table(line_height=33, width=130, col_widths=(35, 30, 45, 20)) as table:
for data_row in TABLE_DATA:
row = table.row()
for datum in data_row:
if str(datum)[:4]=="http":
row.cell(img=str(datum), padding=2, img_fill_width=False)
# I'd like control these images output size in mm
# I'd also like to put a footer here (or a header above the image)
else:
row.cell(str(datum))
pdf.add_page()
pdf.cell(text="A 130 mm wide html-table with columns of 35, 30, 45 and 20 mm wide")
pdf.ln(); pdf.ln();
# is there an easier way of setting 35,30,45 and 20 mm column widths for the html-table below?
# What's the proper way of setting full html-table width in mm? (130 mm, for example):
TABLE_DATA = [['<p><img src="'+c+'"></img></p>' if c[:4]=="http" else c for c in r] for r in TABLE_DATA]
pdf.write_html(f"""<table border="1" width="{int(130*130/46)}"><thead><tr>
<th width="{int(35/130*100)}%">{TABLE_DATA[0][0]}</th>
<th width="{int(30/130*100)}%">{TABLE_DATA[0][1]}</th>
<th width="{int(45/130*100)}%">{TABLE_DATA[0][2]}</th>
<th width="{int(20/130*100)}%">{TABLE_DATA[0][3]}</th>
</tr></thead><tbody><tr>
<td>{'</td><td>'.join(TABLE_DATA[1])}</td>
</tr><tr>
<td>{'</td><td>'.join(TABLE_DATA[2])}</td>
</tr><tr>
<td>{'</td><td>'.join(TABLE_DATA[3])}</td>
</tr><tr>
<td>{'</td><td>'.join(TABLE_DATA[4])}</td>
</tr></tbody></table>""",
table_line_separators=True,)
pdf.output('table-image-test.pdf') |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Note that Table cells are subject to substantial changes quite soon. If you look at #973 and #339 , then you can get an idea of how it might look in the future. Each table cell will essentialy become a single text column, with all the flexibility in content and formatting that includes. Until then, it is likely that only the most egregious bugs in the table code will be fixed.
In HTML, a unitless value always defines a size in pixels. Fpdf2 converts that to document units (eg. mm) with the standard PDF resolution of 72 dpi. |
Beta Was this translation helpful? Give feedback.
Not sure what is going on with the height of your first heading.Edit: Actually, I think the
line_height=33
is doing that.Note that
write_html()
always usesimg_fill_width=True
, which explains the different appearance. Both versions look correct in that regard.Table cells are subject to substantial changes quite soon. If you look at #973 and #339 , then you can get an idea of how it might look in the future. Each table cell will essentialy become a single text column, with all the flexibility in content and formatting that includes. Until then, it is likely that only the most egregious bugs in the table code will be fixed.