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 an SVG image displays a black square #1036

Closed
gbgssoftware opened this issue Nov 21, 2023 · 6 comments
Closed

adding an SVG image displays a black square #1036

gbgssoftware opened this issue Nov 21, 2023 · 6 comments

Comments

@gbgssoftware
Copy link

gbgssoftware commented Nov 21, 2023

Hello there!
Thanks for the community members who develop and support fpdf2.

I'm using this version pip install git+https://github.com/PyFPDF/fpdf2.git@master and this code

from fpdf import FPDF

def add_image(image_path):
     pdf = FPDF()
     pdf.allow_images_transparency = True
     pdf.add_page()
     pdf.image(image_path)
     pdf.output("doc-with-svg.pdf")

if __name__ == '__main__':
     image_path = '../1.svg'
     add_image(image_path)

When I open the pdf file, the image appears as a black square.

output pdf:
image

svg image:
2

What can I do?

@Lucas-C
Copy link
Member

Lucas-C commented Nov 21, 2023

Welcome and thank you for the report @gbgssoftware

Sadly, the visual rendering of your SVG file depends heavily on a <style type="text/css"> tag defined on line 8.

CSS styling is one of the SVG features currently not supported by fpdf2:
https://py-pdf.github.io/fpdf2/SVG.html#currently-unsupported-notable-svg-features

The only quick solution for you now is to modify your SVG file.
Here is a modified version that gets rendered perfectly well by fpdf2:
issue-1036

from fpdf import FPDF

pdf = FPDF()
pdf.add_page()
pdf.image("issue-1036.svg", w=pdf.epw)
pdf.output("issue_1036.pdf")

For the record, this SVG file seems to have been generated by CorelDRAW 2017

You can also make a contribution to fpdf2 and submit a PR to progressively add support for <style> SVG tags in fpdf2.

Does this comment answer your questions? 🙂

@gmischler
Copy link
Collaborator

FWIW, Firefox also displays the original file with a black square.

@gbgssoftware
Copy link
Author

Welcome and thank you for the report @gbgssoftware

Sadly, the visual rendering of your SVG file depends heavily on a <style type="text/css"> tag defined on line 8.

CSS styling is one of the SVG features currently not supported by fpdf2: https://py-pdf.github.io/fpdf2/SVG.html#currently-unsupported-notable-svg-features

The only quick solution for you now is to modify your SVG file. Here is a modified version that gets rendered perfectly well by fpdf2: issue-1036

from fpdf import FPDF

pdf = FPDF()
pdf.add_page()
pdf.image("issue-1036.svg", w=pdf.epw)
pdf.output("issue_1036.pdf")

For the record, this SVG file seems to have been generated by CorelDRAW 2017

You can also make a contribution to fpdf2 and submit a PR to progressively add support for <style> SVG tags in fpdf2.

Does this comment answer your questions? 🙂

In the modified SVG,
the style and class tags have been removed.
Fill, stroke... tags added
Is there a way to convert these svg files?
Or would it be better to convert SVG to png and then add it to the PDF?

To make a contribution, I would like to :-)

Thanks

@Lucas-C
Copy link
Member

Lucas-C commented Nov 23, 2023

In the modified SVG,
the style and class tags have been removed.
Fill, stroke... tags added
Is there a way to convert these svg files?

You'll have to do your own research on this, but a quick check on a web search engine reveals some options:

Or would it be better to convert SVG to png and then add it to the PDF?

Of course, that is an option.
But PNG is a raster image format, so you'll get a finite details resolution,
whereas SVG being vector graphics will not have this limitation (i.e. you can zoom forever an never see a pixel).
It's a matter of choice depending on your own needs.

To make a contribution, I would like to :-)

You can start to read our documentation page on development guidelines:
https://py-pdf.github.io/fpdf2/Development.html

Second, you can read fpdf2 code source, and get an understanding on how SVG files are rendered:
https://github.com/py-pdf/fpdf2/blob/master/fpdf/svg.py

@gbgssoftware

This comment was marked as resolved.

@Lucas-C
Copy link
Member

Lucas-C commented Dec 5, 2023

@gbgssoftware : feel free to ask any questions you would have regarding this 🙂

Without any news from you, we will close this issue at some point.

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