fpdf2 icon indicating copy to clipboard operation
fpdf2 copied to clipboard

Adding SVG images from matplotlib

Open vicmayrink opened this issue 4 years ago β€’ 9 comments

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.

vicmayrink avatar May 24 '22 18:05 vicmayrink

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?

Lucas-C avatar May 24 '22 19:05 Lucas-C

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.

gmischler avatar May 25 '22 06:05 gmischler

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 avatar May 25 '22 07:05 Lucas-C

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 avatar May 27 '22 13:05 Lucas-C

I have found a fix in #448

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

Lucas-C avatar May 27 '22 14:05 Lucas-C

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?

Lucas-C avatar May 27 '22 14:05 Lucas-C

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!

gmischler avatar May 29 '22 21:05 gmischler

Thank you @gmischler 😊

Closing this now

Lucas-C avatar May 31 '22 06:05 Lucas-C

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

Lucas-C avatar Jun 17 '22 09:06 Lucas-C