Skip to content

Import SVG files #494

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

Closed
jakeg opened this issue Dec 17, 2014 · 31 comments
Closed

Import SVG files #494

jakeg opened this issue Dec 17, 2014 · 31 comments

Comments

@jakeg
Copy link
Contributor

jakeg commented Dec 17, 2014

Would love to be able to include SVG files in node-canvas documents (input/drawImage() them, not output to SVG) similar to JPG/PNGs etc.

@enagorny
Copy link

enagorny commented Mar 5, 2015

+1

@mltucker
Copy link
Contributor

mltucker commented Mar 5, 2015

Cairo doesn't read SVG files, so this would require adding another dependency like librsvg. Since librsvg works with cairo, it might be not that much work beyond adding the dependency. Feel free to submit a patch!

@mori-dev
Copy link

fabric.js can help your goal.

@syadykin
Copy link

+1 to this feature

@alex88
Copy link

alex88 commented Jul 29, 2015

Maybe with http://cairosvg.org/ it can read them?

@marie-x
Copy link

marie-x commented Oct 16, 2015

+1

@jakeg jakeg mentioned this issue Mar 25, 2016
@jakeg
Copy link
Contributor Author

jakeg commented Jun 21, 2016

@mltucker are you sure cairo can't read svg files, or is it just that node-canvas doesn't yet have bindings to support them? I think surely the latter:

... is importing SVGs not just a matter of requiring librsvg and for someone to add the necessary bindings as shown at https://developer.gnome.org/rsvg/2.40/rsvg-Using-RSVG-with-cairo.html ?

@jakeg
Copy link
Contributor Author

jakeg commented Jun 27, 2016

Happy to pay a bounty if anyone would like to work on this?

@scott113341
Copy link

I too would love this functionality. @jakeg, does that bounty offer still stand? 😉

@jakeg
Copy link
Contributor Author

jakeg commented Jun 30, 2016

It sure does. I've emailed you to discuss :)

@marie-x
Copy link

marie-x commented Jul 2, 2016

I'd be willing to kick in.

On Jun 29, 2016, at 10:28 PM, Jake Gordon [email protected] wrote:

It sure does. I've emailed you to discuss :)


You are receiving this because you commented.
Reply to this email directly, view it on GitHub, or mute the thread.

@scott113341
Copy link

To keep everyone updated, I am currently working on adding this functionality: https://github.com/scott113341/node-canvas/tree/svg

I have a couple demos in the svg-demo/ directory: https://github.com/scott113341/node-canvas/tree/svg/svg-demo

@chearon
Copy link
Collaborator

chearon commented Jul 3, 2016

@scott113341 nice, bummer we have to add another dependency though ☹️

@scott113341
Copy link

Another option is to do something like this, where SVG geometry is parsed and then drawn onto the canvas. My guess is that this approach wouldn't work for all SVGs, but it's worth investigating.

@jakeg
Copy link
Contributor Author

jakeg commented Jul 4, 2016

@scott113341 Yeah I was thinking about using canvg before and it's still a backup idea. However, from the progress you've made so far I've got my fingers crossed we'll be able to properly import svgs soon :)

@chearon is it really a problem to have another dependency?

@chearon
Copy link
Collaborator

chearon commented Jul 4, 2016

@jakeg most of the issues posted here are with dependencies. I'm working on distributed static builds though so if that gets merged it would be less of a problem. In the meantime it's worth considering having the SVG library be built only if it's found on the system, the same way Pango currently is on master

@jakeg
Copy link
Contributor Author

jakeg commented Sep 10, 2016

Just noticed pull request #406 from 2 years ago which claims to add this functionality I think?

@foysavas
Copy link
Contributor

@scott113341 what's outstanding from your branch? I am planning to reapply #406 over current master and create a new PR, but want to check in with you about your work first.

@jakeg
Copy link
Contributor Author

jakeg commented Sep 10, 2016

If I remember correctly from a couple of months ago, Scott's fork was working for some SVGs but mangling other ones. Also, if outputting as PDF, the SVGs were unfortunately placed as low res rasters in the PDF output, rather than being placed as-is in their original vector form.

@foysavas
Copy link
Contributor

Thanks for the insight @jakeg.

  • Mangling SVGs is definitely bad. I will look to confirm why and when this happens.
  • Low-res raster renderings of SVGs in PDF output, however, may be acceptable. After all, the SVG is first rendered within an img element contained within a canvas of set size.
  • As also requested, I will also make sure the next PR doesn't impose any new hard dependency requirements on node-canvas

As a next step, I will assess both @scott113341 and @petarov approaches and report back with any serious issues.

Do we have someone with merge privileges watching this discussion? I want to make sure the next PR doesn't lose our momentum.

Thanks again all!

@jakeg
Copy link
Contributor Author

jakeg commented Sep 11, 2016

@foysavas if the SVG is inserted in the PDF at a good resolution (maybe adding an option somehow to specify this?), then this may just be good enough. However, the current code puts them in at a ghastly low resolution with a lot of JPG artifacts (or similar), if I remember correctly.

Ideally for my use though, the SVG would be inserted as-is. I'm printing around 50,000 books a year, using html5 canvas on the front end for users to design pages, and node-canvas on the server to produce PDFs which I then send to print. Clearly I'm going to need the SVGs to end up at a printable quality!

@foysavas
Copy link
Contributor

Hi all, please try out #811 and tell me if there are any issues.

@jakeg
Copy link
Contributor Author

jakeg commented Sep 13, 2016

This works fine with tree.svg, but not with the attached ok.svg (gah, github doesn't allow svg attach, so please rename from ok.png to ok.svg and then try it)

Here's the ok.svg file (saved as ok.png to get it to upload):

ok

Here's the mangled output:

demo-1

Also can confirm that PDF output is same as per Scott's code (low-quality raster version of the svg rather than original vector file placed as-is in the pdf)

@foysavas
Copy link
Contributor

@jakeg What OS and version of librsvg and node are you using? I managed to render ok.svg without issue.

ok

@jakeg
Copy link
Contributor Author

jakeg commented Sep 14, 2016

@foysavas Ubuntu 14.04 (in a VM on Windows 10), Node v4.4.5 and librsvg 2 (installed with sudo apt-get install librsvg2-dev)

@petarov
Copy link

petarov commented Sep 14, 2016

Just for the record, it looks good on my Arch Linux w/h node 6.5.0.

@jakeg Does the mangling occur only with this particular SVG or also with other ones you got?

@foysavas
Copy link
Contributor

I can confirm the bad rendering of ok.svg on Ubuntu 14.04, using Node v4 or v6, and the packaged librsvg version 2.40.2.

In contrast, I can confirm a correct rendering of ok.svg on Ubuntu 16.04, using Node v4 or v6, and the packaged librsvg version 2.40.13.

I believe that librsvg around version 2.40.2 has a path parsing bug and that by 2.40.13 it was fixed. This abandoned bug post seems to support that: https://bugzilla.gnome.org/show_bug.cgi?id=767919

@jakeg Ubuntu 16.04 is also a LTS, hopefully you can transition to it. If you still run into issues, tell me.

@jakeg
Copy link
Contributor Author

jakeg commented Sep 15, 2016

@foysavas fantastic! Yes, I am upgrading the main server to Ubuntu 16.04 today, and was going to run a new test after the upgrade, so wonderful to hear that :)

Has anyone had a look at PDF output at all yet? See below re the quality of the SVG in the PDF (although it says it does, github doesn't allow me to add a PDF so again just change file extension from png to pdf)

tree pdf

@foysavas
Copy link
Contributor

foysavas commented Oct 3, 2016

@jakeg Glad to hear it! Native SVGs embedded in PDFs would be nice. Maybe the best way to implement it would be to use MODE_MIME as can be done with JPEGs. I'll spend some cycles to make it happen, but let's get PR #811 merged in first. I'm afraid deviating too much from master will put the PR at risk of going stale.

@zbjornson
Copy link
Collaborator

Closing since the corresponding PR for this landed in 2.x! 🎉

@usr-ein
Copy link

usr-ein commented Sep 6, 2022

Just linking the PR mentionned above, if anyone ends up on this issue and wants to know what the PR was:

#811

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

14 participants