简体   繁体   English

将SVG元素导出为PDF?

[英]Export SVG elements to PDF?

I have a visualization generated by d3 (a javascript visualization library similar to Protovis or Raphael, which draws stuff using SVG elements). 我有一个由d3生成的可视化(类似于Protovis或Raphael的javascript可视化库,它使用SVG元素绘制内容)。 The vis is interactive, so the user can interact with and edit it. vis是交互式的,因此用户可以与之交互并对其进行编辑。 Once the user is satisfied with his/her visualization, I would like the user to be able to export this visualization as a PDF. 一旦用户对他/她的可视化感到满意,我希望用户能够将该可视化导出为PDF。 I've tried several HTML to PDF libraries and they don't work with SVG elements. 我已经尝试了几个HTML到PDF库,它们不能与SVG元素一起使用。

It is okay if the solution is either client side or server side. 如果解决方案是客户端或服务器端,那也没关系。 I'm using PHP server side but Python or Java implementations might also work. 我正在使用PHP服务器端,但Python或Java实现也可以工作。

Browser support: Ideally it would support all modern browsers, but minimally I'd like to support latest versions of both Firefox and webkit browsers. 浏览器支持:理想情况下它会支持所有现代浏览器,但最低限度我想支持Firefox和webkit浏览器的最新版本。

I do not know of any strong PDF libraries on the client side. 我不知道客户端有任何强大的PDF库。

A quick possible way would be to send the svg content to a server, and use something like batik for java to turn the svg to pdf and then send the response to the client again. 一种快速的方法是将svg内容发送到服务器,并使用像batik for java之类的东西将svg转换为pdf,然后再将响应发送到客户端。

Here is a related SO for the converstion . 这是与转换相关的SO

There's also wkhtml2pdf, which can render anything webkit can as a PDF. 还有wkhtml2pdf,可以将任何webkit渲染为PDF格式。 If you want to render a combination of SVG and HTML, or want to have some JavaScript run before the PDF snapshot is taken, it's great for that. 如果您想要渲染SVG和HTML的组合,或者想要在拍摄PDF快照之前运行一些JavaScript,那么它非常适合。

PhantomJS can also rasterize url/html to PDF. PhantomJS还可以将url / html光栅化为PDF。 Same backend (QTWebKit) with wkhtml2pdf. 与wkhtml2pdf相同的后端(QTWebKit)。

I did not try d3, but I achieved the effect you are looking for like this in Python3.6: 我没有尝试过d3,但是我在Python3.6中实现了你想要的效果:

# Pdf library
from reportlab.pdfgen import canvas
from reportlab.graphics import renderPDF, renderPM

# Svg library
import svgwrite

# Svg to reportlab
from svglib.svglib import svg2rlg, SvgRenderer

# Xml parser
from lxml import etree

# Create the svg
dwg = svgwrite.Drawing('test.svg', profile='tiny')
dwg.add(dwg.line((0, 0), (10, 10), stroke=svgwrite.rgb(10, 10, 16, '%')))
dwg.add(dwg.text('Test', insert=(0, 0.2)))

# Create canvas for pdf
c = canvas.Canvas("output.pdf")

# Parse the xml of the svg
parser = etree.XMLParser(remove_comments=True, recover=True)
root = etree.fromstring(dwg.tostring())

# Render the svg itself
svgRenderer = SvgRenderer()
drawing = svgRenderer.render(root)

# Now render the drawing in the pdf
renderPDF.draw(drawing , c, 10, 10)

# End page and save pdf file
c.showPage()
c.save()

# Or render to a seperate png
renderPM.drawToFile(drawing, "file.png", fmt="PNG")

Reportlab is an open source pdf library and svglib is a library that is able to convert svg's to reportlab Drawings. Reportlab是一个开源的pdf库,svglib是一个能够将svg转换为reportlab Drawings的库。 Rendering svg's directly from the xml is not supported out of the box, that is why I use the SvgRenderer. 直接从xml渲染svg不支持开箱即用,这就是我使用SvgRenderer的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM