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

Cairosvg rendered pdf is slow and heavy #53

Closed
makmanalp opened this issue Jul 10, 2014 · 6 comments
Closed

Cairosvg rendered pdf is slow and heavy #53

makmanalp opened this issue Jul 10, 2014 · 6 comments
Labels

Comments

@makmanalp
Copy link

Hey Folks,

I realize this might be an upstream issue but I'm curious to see if you can help. I've been
using cairosvg to handle visualization downloads on my site: http://atlas.cid.harvard.edu/explore/tree_map/export/jpn/all/show/2012/ (hit the PDF link on the right toolbar).

So far it's been working great but I've received complaints about the generated pdfs being very slow to load and crashing preview on OSX. I've reproduced the issue in the firefox pdf.js renderer (very slow), the chrome builtin one (workable) and preview.app (hangs). I thought it may have something to do with the number of elements in the PDF since with smaller countries it seems to load much easier (e.g. try Fiji).

At that point I would chalk it up to the renderers being slow but someone I know took the SVG, loaded it up in illustrator and exported as PDF, and that renders almost instantly, and the file size is much smaller too. Is there any insight into what that might be doing that I am not? I know it's not rasterizing it, but it seems it's optimizing it somehow.

I'm really just looking for pointers here, and hoping you're more experienced at this than I am.

Here's a prettified version of one of the svgs for your convenience:
https://gist.github.com/makmanalp/f630625df0bdf40b61b5

Thanks,
~mali

@liZe
Copy link
Member

liZe commented Jul 14, 2014

You're right, the output file is really heavy and loads slowly in some clients. It seems to be caused by the opacity of the rectangles: without these opacities, the file size goes from 2 MB to 230 kB. I'm trying to find an optimization to fix this problem, I hope that the problem is in CairoSVG and not in Cairo!

@liZe liZe closed this as completed in 9e07d51 Jul 14, 2014
@liZe liZe added the Feature label Jul 14, 2014
@liZe
Copy link
Member

liZe commented Jul 14, 2014

I finally have a 238 kB file with this fix, that's ~12% of the original file size, and that's better in my clients.

For the record and the curious people: when a tag has an "opacity" attribute, there's no need to create an atomic layer for the compositing if there's only one element to draw. This pre-compositing optimization is really useful when there are lots of semi-transparent elements, this gist is a really good example.

@makmanalp
Copy link
Author

Wow, I had honestly given up hope on this one, thank you so much!

I'll test it out on the site to see the improvement and let you know how it goes. Would you be opposed to doing a new release for CairoSVG on pypi after that?

@makmanalp
Copy link
Author

Just tested it out locally - drastic improvement! Loads instantly and files are much smaller like you said.

@liZe
Copy link
Member

liZe commented Jul 14, 2014

1.0.8 released!

@makmanalp
Copy link
Author

👍

liZe added a commit that referenced this issue Mar 11, 2020
We have to take care of fill-opacity and stroke-opacity for patterns and
gradients. These values also include the opacity property for tags with no
children.

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

No branches or pull requests

2 participants