Skip to content

SVG image type support #1746

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
echaconairlan opened this issue Oct 24, 2019 · 10 comments · May be fixed by #2790
Open

SVG image type support #1746

echaconairlan opened this issue Oct 24, 2019 · 10 comments · May be fixed by #2790

Comments

@echaconairlan
Copy link

Is there any workaround to add this support? I really need it for adding some highcharts graphs.

@simogeo
Copy link

simogeo commented May 4, 2024

any news regarding this feature request ? Thanks in advance

@MegaChriz
Copy link
Contributor

MegaChriz commented Jun 14, 2024

I briefly looked at this today together with a collegae.

We found out that:

  • SVG data should be written to Word in a different way than a PNG or a JPEG:
    • PNG and JPEG are written in \PhpOffice\PhpWord\Writer\Word2007\Element\Image and are using (among more) a XML tag called 'w:pict'.
    • SVG needs to start with a XML tag called 'w:drawing'. This makes them closer related to \PhpOffice\PhpWord\Writer\Word2007\Element\Chart than to \PhpOffice\PhpWord\Writer\Word2007\Element\Image.
  • SVG data shouldn't be written to the Word XML as is. Instead, in the Word XML a reference (r:id) needs to be used.
  • \PhpOffice\PhpWord\Element\Image::checkImage() uses PHP image functions that don't work for SVG images, like getimagesizefromstring(). image_type_to_mime_type(), used to determine the mime type of the image, has no listing for SVG, see https://www.php.net/image_type_to_mime_type.
  • The mimetype for SVG should be 'image/svg+xml'.
  • In Word's openXML documentation we saw that we would at least need to use a XML tag called 'asvg:svgBlip'. See https://learn.microsoft.com/en-us/dotnet/api/documentformat.openxml.linq.asvg.svgblip?view=openxml-3.0.1#documentformat-openxml-linq-asvg-svgblip
  • We found a code example here for generating Word XML for SVG images: https://www.docx4java.org/forums/docx-java-f6/add-svg-to-document-t2927.html#p10155. It is in Java, but it does give an idea about which XML tags should be used to write the data.
  • By calling the getData() method on a \PhpOffice\PhpWord\Shared\XMLWriter object you can get an idea about what Word XML gets produced. I temporarily added var_dump($xmlWriter->getData());die(); to \PhpOffice\PhpWord\Writer\Word2007\Element\Image::writeImage() to see what was getting outputted.

That's how far as we got today.

Todo:

  • I think that in phpword/src/PhpWord/Writer/Word2007/Element we would need to add a new class called SvgImage. The code from \PhpOffice\PhpWord\Writer\Word2007\Element\Chart could probably be used as a starting point for this class.
  • Likewise, I think that we need to have a SvgImage class in phpword/src/PhpWord/Element so a svg image can be added by calling $phpWord->addSvgImage().

@Progi1984
Copy link
Member

Hi @simogeo @MegaChriz @echaconairlan , could you give me a sample file with a svg image, please ?

@Progi1984 Progi1984 added Change Request Open XML (Word 2007+) Status: Waiting for feedback Question has been asked, waiting for response from PR author labels Aug 10, 2024
@MegaChriz
Copy link
Contributor

I found a few SVG images:
waardering-82
ukkie
tux
Piramade groen

@MegaChriz
Copy link
Contributor

Hm, apparently GitHub transforms them. Try again:
svgs.zip

@Progi1984
Copy link
Member

Not a Svg file but a docx with a svg image embed.

@MegaChriz
Copy link
Contributor

I haven't one ready right now, because I don't have access to Word currently. Maybe in a few days.

@Progi1984
Copy link
Member

Thanks 😊

@MegaChriz
Copy link
Contributor

docxs-with-svg.zip
Here are a few docx-files with a SVG in them. There is one with a highchart graph in it, and two with a simpler looking SVG file.

@simogeo
Copy link

simogeo commented Aug 12, 2024

Thanks to all of you. I also provide a doc sample with EPS image embedded. It should not be so different from SVG file and also interesting to implement.

embedded-EPS-file.docx

thanks @Progi1984 and @MegaChriz

@Progi1984 Progi1984 added Status: Responded and removed Status: Waiting for feedback Question has been asked, waiting for response from PR author labels Aug 12, 2024
@Progi1984 Progi1984 added this to the 2.0.0 milestone Aug 12, 2024
@Progi1984 Progi1984 modified the milestones: 1.3.0, 1.3.1 Sep 1, 2024
@Progi1984 Progi1984 modified the milestones: 1.4.0, 1.5.0 Jun 5, 2025
@prog-klk1 prog-klk1 linked a pull request Jun 9, 2025 that will close this issue
4 tasks
# for free to join this conversation on GitHub. Already have an account? # to comment
4 participants