Adding SVG images from matplotlib
Hello there! Thanks for the community members who develop and support fpdf2, it has been a handful tool generate analytical reports.
I'm using matplotlib to save figures in SVG format for better quality. Find below a sample code:
from fpdf import FPDF
import matplotlib.pyplot as plt
import numpy as np
#Generate sample svg figure
plt.figure(figsize=[2, 2])
x = np.arange(0, 10, 0.00001)
y = x*np.sin(2* np.pi * x)
plt.plot(y)
plt.savefig("vector.svg", format="svg")
#Add SVG figure
pdf = FPDF()
pdf.add_page()
pdf.image("vector.svg")
pdf.output("doc-with-svg.pdf")
The result, however, was not actually what I was expecting: a big black box. (output pdf is here)
I noticed that fpdf2 does not support some SVG features (list of unsupported features). Then, I'm wondering if outputs from matplotlib contains any feature that is not supported by FPDF2.
Obs.: I would guess that it would be simpler to save the figure in pdf format (rather than svg). This issue is actually a suggestion from this discussion.
Good news!
I tested your code using the latest, unreleased, version 2.5.5 and it produced this PDF file that seems perfectly fine: doc-with-svg.pdf
This is thanks to the work of @RedShy in #419 ππππΌπ
I'm planning to perform a new release once the 2 PRs currently open are merged,
but in the meantime you can test the latest version from git using this command:
pip install git+https://github.com/PyFPDF/fpdf2.git@master
Does that solves your issue?
I had already been wondering if the svg output of mathplotlib would be a working replacement for the currently documented way of using image files.
I tested your code using the latest, unreleased, version 2.5.5 and it produced this PDF file that seems perfectly fine: doc-with-svg.pdf
The graph as such looks ok, but the character placement in the axis labelling is rather wonky. Is that a problem on our end, or does mathplotlib do weird things there? (The latter seems rather unlikely, to be honest.)
Added:
The documentation currently states that our SVG input supports "CSS styling of SVG elements".
This should be corrected to say "Inline CSS styling of SVG elements".
In the list of unsupported features, it should then be noted that "external CSS style definitions" (both as seperate <style> tags and in seperate files) are still not supported, and probably never will.
The documentation currently states that our SVG input supports "CSS styling of SVG elements". This should be corrected to say "Inline CSS styling of SVG elements".
Agreed. Could you please submit a PR?
Also, I think we should include @vicmayrink recipe as part of our documentation, once we have clarified the pertinent question you asked @gmischler
The graph as such looks ok, but the character placement in the axis labelling is rather wonky. Is that a problem on our end, or does mathplotlib do weird things there?
Given that vector.svg is perfectly fine, there is indeed an issue with our SVG rendering of this file, matplotlib is not the culprit.
After looking at the SVG content, it seems like all text elements are in fact rendered as paths.
I have isolated a minimal SVG file that does not get rendered well by fpdf2:
Resulting PDF: issue-446.pdf
We need to dig a bit deeper to understand what is missing in our code supporting SVG.
I also added the recipe in the documentation in https://github.com/PyFPDF/fpdf2/commit/304685: https://pyfpdf.github.io/fpdf2/Maths.html#using-matplotlib
@vicmayrink : can we close this issue or do you have any other question/request regarding the subject?
The resulting PDF is a lot prettier: doc-with-svg.pdf
Very cool! (SVG is really a tricky beast...)
Using SVG output from mathplotlib should result in much smaller (and crisper) PDFs in most cases so we clearly have a winner here!
Thank you @gmischler π
Closing this now
The fix to support "x" & "y" attributes in SVG <use> tags has been included in release 2.5.5