Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to put a PNG image into a table, using an IO buffer? #680

Closed
1 of 2 tasks
eabase opened this issue Feb 5, 2023 · 7 comments
Closed
1 of 2 tasks

How to put a PNG image into a table, using an IO buffer? #680

eabase opened this issue Feb 5, 2023 · 7 comments

Comments

@eabase
Copy link

eabase commented Feb 5, 2023

Ok, these are really 2 questions:

  • 1. How can I put multiple PNG images into a table, in lets say a (10 rows x 5 columns)?
  • 2. How can I get the image from an IO buffer?

Using segno I can create a PNG IO buffer like this:

qrcode  = segno.make(str(i), micro=False, mode=qr_mode)
buff = io.BytesIO()
qrcode.save(buff, kind='png', **qr_args)
buff_val = str(buff.getvalue())

Your documentation for image() say to use:

def image(self, name, x=None, y=None, w=0, h=0, type='', link='', title=None, alt_text=None, dims=None) 

name
either a string representing a file path to an image, an URL to an image, an io.BytesIO, or a instance of PIL.Image.Image

But I am not able to get this to work.


UPDATE
I managed to get the image from the buffer, but I am not able to get the image into a table cell.

@Lucas-C
Copy link
Member

Lucas-C commented Feb 6, 2023

Hi @eabase!
Glad that you found how to insert an image from an IO buffer, well done.

There is an example of putting images side by side in a table using write_html() there:
https://github.com/PyFPDF/fpdf2/blob/2.6.1/test/html/test_html.py#L285

Does that help you doing what you want?

This project being based on volunteering, if you want you can submit a Pull Request to also add an example in our documentation 😊
A good place to add a section about this could be https://github.com/PyFPDF/fpdf2/blob/master/docs/Images.md

@eabase
Copy link
Author

eabase commented Feb 11, 2023

Hi @Lucas-C
Thanks for the feedback.

Does that help you doing what you want?

Unfortunately not, as I have no need or intention to use HTML.

What I need is a simple example for generating 4-6 columns, in which every other has a QR code, like this:

# QR # QR
1 <QR image1> 2 <QR image2>
55 <QR image3> 404 <QR image4>

PS. All QR images are square and of equal size, and possibly with some additional padding between columns, perhaps via an empty column.

@Lucas-C Lucas-C added the table label Feb 14, 2023
@Lucas-C
Copy link
Member

Lucas-C commented Feb 14, 2023

Hi @eabase!

Currently, fpdf2 main FPDF class does not have a table() method.
Several users asked for it, so I'm seriously considering implementing it in the library.
I'm think of something like this:

from fpdf import FPDF

pdf = FPDF()
with pdf.table() as table:
    table.set_col_widths(...)  # optional
    with table.row() as row:
        row.cell(...)  # or row.image(...)

For now, we only have a list of useful recipes in our documentation: https://pyfpdf.github.io/fpdf2/Tables.html#recipes
You can for example try @bvalgard create_table() method: https://github.com/bvalgard/create-pdf-with-python-fpdf2/blob/main/create_table_fpdf2.py It may not support images though.

You can also easily achieve your goal using write_html():

from fpdf import FPDF

pdf = FPDF()
pdf.add_page()
IMG_URL = "https://upload.wikimedia.org/wikipedia/commons/thumb/6/61/QR_deWP.svg/270px-QR_deWP.svg.png"
pdf.write_html(f"""
  <table>
    <thead>
      <tr>
        <th width="10%">#</th>
        <th width="40%">QR</th>
        <th width="10%">#</th>
        <th width="40%">QR</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>1</td>
        <td><img src="{IMG_URL}"></td>
        <td>2</td>
        <td><img src="{IMG_URL}"></td>
      </tr>
      <tr>
        <td>55</td>
        <td><img src="{IMG_URL}"></td>
        <td>404</td>
        <td><img src="{IMG_URL}"></td>
      </tr>
    </tbody>
  </table>
""")
pdf.output("issue_680.pdf")

Does that answer your initial questions?

@eabase
Copy link
Author

eabase commented Feb 16, 2023

Hi @Lucas-C
Again thanks so much for trying to help.
A native Table implementation would be fantastic!

As for the 2nd option of using HTML, that is an absolute no-go, as I have 100's of rows, and really really don't want to see any HTML, anywhere! 😱 👹

@Lucas-C
Copy link
Member

Lucas-C commented Feb 20, 2023

I'm closing this issue in favour of #701,
as I don't see how to better answer the initial questions / need of @eabase here

@Lucas-C Lucas-C closed this as completed Feb 20, 2023
@eabase
Copy link
Author

eabase commented Feb 25, 2023

@Lucas-C
Hi Lucas,
Thank you so much for the rapid development and listening to our issues. 💯
I see the PR 701 is still open, so is this ready to be used yet?

Then, given your sample snippet above, how could I put this to practical use?
(What should I replace the ... with?)


OMG! I just found PR #703 ❤️
With new docs here
Wow, that is awesome!

@Lucas-C
Copy link
Member

Lucas-C commented Feb 27, 2023

@eabase : you should be able to test PR #701 by installing fpdf2 from the table branch of this repo:

pip install git+https://github.com/PyFPDF/fpdf2.git@table

I'd be happy to have your feedbacks in #701, once you have tested the table() method 😊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants