-
Notifications
You must be signed in to change notification settings - Fork 252
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
Doc: provide an usage example of combining fpdf2 with Pygal #652
Comments
Hi @Lucas-C , Would like to contribute in this issue as well. |
You are very welcome to do so! This task should be very similar to your great work on PR #746, |
1 similar comment
You are very welcome to do so! This task should be very similar to your great work on PR #746, |
@Lucas-C, Yeah, I started reading some basic snippets using
How I understood this line is - I need to generate a demo chart using The new section will be - Am I correct? |
You do not need to "embed" it in
Exactly! |
Hi @Lucas-C , I have tried almost everything in my mind. It comes down to the following. I found only one way to do that using import pygal
from reportlab.graphics import renderPM
from svglib.svglib import svg2rlg
from fpdf import FPDF
# Create a Pygal bar chart
bar_chart = pygal.Bar()
bar_chart.title = 'Sales by Year'
bar_chart.x_labels = ['2016', '2017', '2018', '2019', '2020']
bar_chart.add('Product A', [500, 750, 1000, 1250, 1500])
bar_chart.add('Product B', [750, 1000, 1250, 1500, 1750])
# Render the chart to an SVG and PNG file
bar_chart.render_to_file('bar_chart.svg')
drawing = svg2rlg("bar_chart.svg")
renderPM.drawToFile(drawing, "bar_chart.png")
# Set the position and size of the image in the PDF
x = 50
y = 50
w = 100
h = 70
pdf = FPDF()
pdf.add_page()
pdf.image('bar_chart.png', x=x, y=y, w=w, h=h)
pdf.output('bar_chart_pdf.pdf') I tried with the SVG file generated by the Will the above implementation be enough? or do you have any ideas other than this? You can try the following implementation which returns a Black Box- import pygal
from reportlab.graphics import renderPM
from svglib.svglib import svg2rlg
from fpdf import FPDF
# Create a Pygal bar chart
bar_chart = pygal.Bar()
bar_chart.title = 'Sales by Year'
bar_chart.x_labels = ['2016', '2017', '2018', '2019', '2020']
bar_chart.add('Product A', [500, 750, 1000, 1250, 1500])
bar_chart.add('Product B', [750, 1000, 1250, 1500, 1750])
# Render the chart to an SVG file
bar_chart.render_to_file('bar_chart.svg')
# Set the position and size of the image in the PDF
x = 50
y = 50
w = 100
h = 70
pdf = FPDF()
pdf.add_page()
pdf.image('bar_chart.svg', x=x, y=y, w=w, h=h)
pdf.output('bar_chart_pdf.pdf') I also tried the Please let me know any queries. |
Thank you for your research @ssavi-ict! It seems that Pygal introduces Sadly, As a consequence, I fear that we won't be able to embed Pygal graphs as SVG in PDF documents produced by Hence, your approach of embedding charts in a rasterized format seems like the best to me. However, to convert SVG to a raster image, I don't think that installing reportlabs is needed. img = BytesIO()
cairosvg.svg2png(bar_chart.render(), write_to=img)
pdf.image(img) Do you feel that you have enough information to start a Pull Request @ssavi-ict? 😊 |
@Lucas-C , I tried that also.
For this, I think we should provide both the solutions with enough explanations (I am OK with that). User can choose based on the requirements. Let me know what you think? I will provide the Doc after that. Thank you for the suggestion. |
Yes, explaining how to perform the SVG conversion to PNG with both libraries seem like a good idea! |
We already have a page about Charts & graphs in our documentation, and other ones about combining
fpdf2
with other libs.Pygal seems like a great charting library that can can produces SVG, even if it has received zero development updates for a year now.
It would be interesting to provide a documentation section on how to generate a chart with
Pygal
and embed it infpdf2
.By implementing this feature you, as a benevolent FLOSS developper, will provide access to the large community of fpdf2 users to a useful PDF functionality.
As a contributor you will get review feedbacks from maintainers & other contributors, and learn about the lifecycle & structure of a Python library on the way.
You will also be added into the contributors list & map.
This issue can count as part of hacktoberfest
The text was updated successfully, but these errors were encountered: