Export d3js drawings as SVG/PDF/PNG

14,563 views
Skip to first unread message

gordon

unread,
Oct 5, 2012, 6:21:56 PM10/5/12
to d3...@googlegroups.com
Hello,

I was missing one feature of exporting the d3js drawings to PDF/PNG (needed for users to be able to embed graphics in their presentations or documents).

I couldn't find any existing examples, so I developed one.

online demo is here: http://d3export.cancan.cshl.edu/


Currently it uses a server-side script to convert SVG to PDF, but perhaps in the future this could be client-side.

Comments are welcomed,
 -gordon

Bob Monteverde

unread,
Oct 5, 2012, 6:54:22 PM10/5/12
to d3...@googlegroups.com
Interesting.  I'll have to look further at this.  Currently SVG exportation is essentially automatic (just copy the DOM) but for PDF/PNG generation we use phantomJS successfully here.

Bob

Lars Kotthoff

unread,
Oct 6, 2012, 7:07:02 AM10/6/12
to d3...@googlegroups.com, b...@novus.com
> ...for PDF/PNG generation we use phantomJS successfully here.

If you're on a Mac, you can use wkpdf (http://plessl.github.com/wkpdf/) instead
of phantomjs to get "proper" PDFs. My gripe with phantomjs is that it really
only renders to a bitmap and then includes that in the generated PDF. wkpdf
preserves the HTML document better, i.e. text is still text in the PDF.

Lars

Chris Viau

unread,
Oct 6, 2012, 10:55:46 AM10/6/12
to d3...@googlegroups.com
You can do it client-side like this: http://bl.ocks.org/1209499
Chris Viau

Chris Viau

unread,
Oct 6, 2012, 11:07:55 AM10/6/12
to d3...@googlegroups.com, b...@novus.com
I used wkhtmltopdf with great success. The wkpdf cited here is a Ruby script, but there is another wkpdf project which is just a PHP wrapper for wkhtmltopdf. Some also use Batik, a bit old but convenient when you already use a Java environment.

Bob Monteverde

unread,
Oct 9, 2012, 2:29:12 PM10/9/12
to d3...@googlegroups.com, b...@novus.com
Strange, I believe text is text for me when using phantom with svg.  I'll have to double check, but it appears to preserve the vector shapes and text correctly.

Bob

Praveen Kumar

unread,
Oct 15, 2012, 5:50:23 AM10/15/12
to d3...@googlegroups.com
Hi Bob, can give any example code for converting the svg tag embed in html to a png using phantomjs. 

Thank you.

lionel Lioninho

unread,
Jun 20, 2013, 12:31:05 AM6/20/13
to d3...@googlegroups.com
Hi Chris,
I tried with Chrome to copy as HTML an svg tag and in the inspector console did  what you said : 

d3.select("body").append("div")
        .attr("id", "download")
        .style("top", event.clientY+20+"px")
        .style("left", event.clientX+"px")
        .html("Right-click on this preview and choose Save as<br />Left-Click to dismiss<br />")
        .append("img")
        .attr("src", "data:image/svg+xml;base64,"+ btoa(html));
with "html" being the copy of the "svg" code i had copied before but it isn't working though.
Any idea why it isn't working ? 
Thanks 
Reply all
Reply to author
Forward
0 new messages