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

Adding SVG images from matplotlib #446

Closed
vicmayrink opened this issue May 24, 2022 · 9 comments
Closed

Adding SVG images from matplotlib #446

vicmayrink opened this issue May 24, 2022 · 9 comments
Assignees

Comments

@vicmayrink
Copy link

vicmayrink commented May 24, 2022

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.

@Lucas-C
Copy link
Member

Lucas-C commented May 24, 2022

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?

@gmischler
Copy link
Collaborator

gmischler commented May 25, 2022

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.

@Lucas-C
Copy link
Member

Lucas-C commented May 25, 2022

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

@Lucas-C
Copy link
Member

Lucas-C commented May 27, 2022

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:
issue-446
Resulting PDF: issue-446.pdf

We need to dig a bit deeper to understand what is missing in our code supporting SVG.

@Lucas-C
Copy link
Member

Lucas-C commented May 27, 2022

I have found a fix in #448

The resulting PDF is a lot prettier: doc-with-svg.pdf

@Lucas-C Lucas-C self-assigned this May 27, 2022
@Lucas-C
Copy link
Member

Lucas-C commented May 27, 2022

I also added the recipe in the documentation in 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?

@gmischler
Copy link
Collaborator

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!

@Lucas-C
Copy link
Member

Lucas-C commented May 31, 2022

Thank you @gmischler 😊

Closing this now

@Lucas-C Lucas-C closed this as completed May 31, 2022
@Lucas-C
Copy link
Member

Lucas-C commented Jun 17, 2022

The fix to support "x" & "y" attributes in SVG <use> tags has been included in release 2.5.5

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

3 participants